def test_abort(self): d = Dispatcher() job_ref_1 = d.dispatch_task(bg_jobs.test_sleep, args=(3, )) time.sleep(1.2) assert d.query_task(job_ref_1).status == 'started' workers = rq.Worker.all(connection=d._redis_conn) wk = [w for w in workers if w.state == 'busy'] assert len(wk) == 1, "There must be precisely one busy worker" job_pid = wk[0].get_current_job().meta['pid'] d.abort_task(job_ref_1) time.sleep(0.1) j = d.query_task(job_ref_1) # There should be no result, cause it was cancelled assert j.result is None # RQ should identify the task as failed assert j.status == "failed" # Assert the JOB pid is gone with pytest.raises(OSError): os.kill(int(job_pid), 0) # Now assert the worker pid is still alive (so it can be assigned something else) worker_pid = wk[0].pid try: os.kill(int(worker_pid), 0) assert True, "Worker process is still hanging around." except OSError: assert False, "Worker process is killed"
def mutate_and_get_payload(cls, root, info, owner, labbook_name, client_mutation_id=None): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name) d = Dispatcher() lb_jobs = d.get_jobs_for_labbook(lb.key) jobs = [ j for j in d.get_jobs_for_labbook(lb.key) if j.meta.get('method') == 'build_image' and j.status == 'started' ] if len(jobs) == 1: d.abort_task(jobs[0].job_key) ContainerOperations.delete_image(lb, username=username) return CancelBuild(build_stopped=True, message="Stopped build") elif len(jobs) == 0: logger.warning(f"No build_image tasks found for {str(lb)}") return CancelBuild(build_stopped=False, message="No build task found") else: logger.warning(f"Multiple build jobs found for {str(lb)}") return CancelBuild(build_stopped=False, message="Multiple builds found")