def kill_stalled_processes(dryrun=True): """ Due to a bug in the Django|Postgres backend, occassionally the `manage.py cron` process will hang even through all processes have been marked completed. We compare all recorded PIDs against those still running, and kill any associated with complete jobs. """ pids = set(map(int, Job.objects\ .filter(is_running=False, current_pid__isnull=False)\ .exclude(current_pid='')\ .values_list('current_pid', flat=True))) for pid in pids: try: if utils.pid_exists(pid): # and not utils.get_cpu_usage(pid): p = psutil.Process(pid) cmd = ' '.join(p.cmdline()) if 'manage.py cron' in cmd: jobs = Job.objects.filter(current_pid=pid) job = None if jobs: job = jobs[0] utils.smart_print('Killing process %s associated with %s.' % (pid, job)) if not dryrun: utils.kill_process(pid) else: print('PID not cron.') else: print('PID dead.') except psutil.NoSuchProcess: print('PID does not exist.')
def kill_stalled_processes(dryrun=True): """ Due to a bug in the Django|Postgres backend, occassionally the `manage.py cron` process will hang even through all processes have been marked completed. We compare all recorded PIDs against those still running, and kill any associated with complete jobs. """ pids = set(map(int, Job.objects\ .filter(is_running=False, current_pid__isnull=False)\ .exclude(current_pid='')\ .values_list('current_pid', flat=True))) for pid in pids: try: if utils.pid_exists(pid):# and not utils.get_cpu_usage(pid): p = psutil.Process(pid) cmd = ' '.join(p.cmdline()) if 'manage.py cron' in cmd: jobs = Job.objects.filter(current_pid=pid) job = None if jobs: job = jobs[0] utils.smart_print('Killing process %s associated with %s.' % (pid, job)) if not dryrun: utils.kill_process(pid) else: print('PID not cron.') else: print('PID dead.') except psutil.NoSuchProcess: print('PID does not exist.')