def __del__(self): """ Don't keep the process as a zombie if it isn't running anymore.""" try: if util.get_state_code_of_pid(self.pid) in ('Z', ''): os.waitpid(self.pid, 0) except OSError: pass
def update_list(): # Update programs dict #print("Checking which programs are alive...") for name, program in RUNNING_PROGRAMS.items(): if not program.is_alive(): del RUNNING_PROGRAMS[name] #print("\t%s is terminated, removed." % name) else: pass #print("\t%s is still alive, leaving alone." % name) # Get log files directory = '%s/process_info' % common.ENV['VINEYARDPATH'] program_list = sorted(os.listdir(directory)) # Run through list of process info files and get the ones that aren't running anymore #print("Checking which info files are obsolete and adding still running programs...") old_programs = [] pids_of_users_processes = util.get_pids_of_user_processes() for info_name in program_list: if info_name not in RUNNING_PROGRAMS: discard = True # Get the info contained in the info file info = get_program_info(info_name) if 'pid' in info and info['pid'] in pids_of_users_processes: # If the process with this PID was started at the same time # as our the process info's says, they are the same # process. # The matching of start time is done +-3 seconds # to account for any differences in logging between Vineyard # and the system. #print("We have a process with PID %s in our logs, checking if it's alive and ours..." % info['pid']) pid_stime = util.get_start_time_of_pid(info['pid']) if pid_stime != None: (pid_stime_month, pid_stime_day, pid_stime_hour, pid_stime_minute, pid_stime_second) = pid_stime our_stime = _get_start_time_from_program_info(info) (our_stime_month, our_stime_day, our_stime_hour, our_stime_minute, our_stime_second) = our_stime if ( pid_stime_month == 0 and pid_stime_hour == our_stime_hour and pid_stime_minute == our_stime_minute and ( pid_stime_second == our_stime_second or pid_stime_second == our_stime_second-1 or pid_stime_second == our_stime_second+1 or pid_stime_second == our_stime_second-2 or pid_stime_second == our_stime_second+2 or pid_stime_second == our_stime_second-3 or pid_stime_second == our_stime_second+3 ) ) or ( pid_stime_month == our_stime_month and pid_stime_day == our_stime_day ): #print("PID %s is ours." % info['pid']) if util.get_state_code_of_pid(info['pid']) in ('Z', ''): # This process is a zombie, let it rest in peace #print("Let PID %s die." % info['pid']) try: os.waitpid(info['pid'], 0) except OSError: # Already exited pass else: # This process is alive, add it to the list RUNNING_PROGRAMS[info_name] = AdoptedProgram(info) discard = False if discard: old_programs.append(info_name) # Remove the info files that aren't running and are more than is to be kept #print("Cleaning up old info files...") for info_name in old_programs[:-5]: try: #print("\t%s is no longer needed, removing...") os.remove('%s/%s' % (directory, info_name)) except OSError: print("Couldn't remove process info file \"%s\"." % info_name, file=sys.stderr)
def is_alive(self): return (util.get_state_code_of_pid(self.pid) not in ('X', 'Z', ''))