def jobs(): job1 = Job('task1', print, ('hello', ), {'arg2': 'world'}, 'schedule1', datetime(2020, 10, 10, tzinfo=timezone.utc), datetime(2020, 10, 10, 1, tzinfo=timezone.utc), frozenset()) job2 = Job('task2', print, ('hello', ), {'arg2': 'world'}, None, None, None, frozenset()) return [job1, job2]
async def test_run_deadline_missed(self, store): async def listener(worker_event): worker_events.append(worker_event) await event.set() scheduled_start_time = datetime(2020, 9, 14) worker_events = [] event = create_event() job = Job('task_id', fail_func, args=(), kwargs={}, schedule_id='foo', scheduled_fire_time=scheduled_start_time, start_deadline=datetime(2020, 9, 14, 1)) async with AsyncWorker(store) as worker: worker.subscribe(listener) await store.add_job(job) async with fail_after(5): await event.wait() assert len(worker_events) == 1 assert isinstance(worker_events[0], JobDeadlineMissed) assert worker_events[0].job_id == job.id assert worker_events[0].task_id == 'task_id' assert worker_events[0].schedule_id == 'foo' assert worker_events[0].scheduled_fire_time == scheduled_start_time
def test_run_deadline_missed(self, anyio_backend, sync_store, portal): def listener(worker_event): worker_events.append(worker_event) event.set() scheduled_start_time = datetime(2020, 9, 14) worker_events = [] event = threading.Event() job = Job('task_id', fail_func, args=(), kwargs={}, schedule_id='foo', scheduled_fire_time=scheduled_start_time, start_deadline=datetime(2020, 9, 14, 1)) with SyncWorker(sync_store, portal=portal) as worker: worker.subscribe(listener) portal.call(sync_store.add_job, job) event.wait(5) assert len(worker_events) == 1 assert isinstance(worker_events[0], JobDeadlineMissed) assert worker_events[0].job_id == job.id assert worker_events[0].task_id == 'task_id' assert worker_events[0].schedule_id == 'foo'
async def test_run_deadline_missed(): def func(): pytest.fail('This function should never be run') scheduled_start_time = datetime(2020, 9, 14) events = [] async with LocalExecutor() as worker: await worker.subscribe(events.append) job = Job('task_id', func, args=(), kwargs={}, schedule_id='foo', scheduled_start_time=scheduled_start_time, start_deadline=datetime(2020, 9, 14, 1)) await worker.submit_job(job) async with fail_after(1): while len(events) < 2: await sleep(0) assert isinstance(events[0], JobAdded) assert events[0].job_id == job.id assert events[0].task_id == 'task_id' assert events[0].schedule_id == 'foo' assert events[0].scheduled_start_time == scheduled_start_time assert isinstance(events[1], JobDeadlineMissed) assert events[1].job_id == job.id assert events[1].task_id == 'task_id' assert events[1].schedule_id == 'foo' assert events[1].scheduled_start_time == scheduled_start_time
async def test_run_job_nonscheduled_success(sync, fail): def sync_func(*args, **kwargs): nonlocal received_args, received_kwargs received_args = args received_kwargs = kwargs if fail: raise Exception('failing as requested') else: return 'success' async def async_func(*args, **kwargs): nonlocal received_args, received_kwargs received_args = args received_kwargs = kwargs if fail: raise Exception('failing as requested') else: return 'success' received_args = received_kwargs = None events = [] async with LocalExecutor() as worker: await worker.subscribe(events.append) job = Job('task_id', sync_func if sync else async_func, args=(1, 2), kwargs={'x': 'foo'}) await worker.submit_job(job) async with fail_after(1): while len(events) < 3: await sleep(0) assert received_args == (1, 2) assert received_kwargs == {'x': 'foo'} assert isinstance(events[0], JobAdded) assert events[0].job_id == job.id assert events[0].task_id == 'task_id' assert events[0].schedule_id is None assert events[0].scheduled_start_time is None assert isinstance(events[1], JobUpdated) assert events[1].job_id == job.id assert events[1].task_id == 'task_id' assert events[1].schedule_id is None assert events[1].scheduled_start_time is None assert events[2].job_id == job.id assert events[2].task_id == 'task_id' assert events[2].schedule_id is None assert events[2].scheduled_start_time is None if fail: assert isinstance(events[2], JobFailed) assert type(events[2].exception) is Exception assert isinstance(events[2].formatted_traceback, str) else: assert isinstance(events[2], JobSuccessful) assert events[2].return_value == 'success'
async def test_run_job_nonscheduled_success(self, target_func, fail, store): async def listener(worker_event): worker_events.append(worker_event) if len(worker_events) == 2: await event.set() worker_events = [] event = create_event() job = Job('task_id', func=target_func, args=(1, 2), kwargs={ 'x': 'foo', 'fail': fail }) async with AsyncWorker(store) as worker: worker.subscribe(listener) await store.add_job(job) async with fail_after(2): await event.wait() assert len(worker_events) == 2 assert isinstance(worker_events[0], JobUpdated) assert worker_events[0].job_id == job.id assert worker_events[0].task_id == 'task_id' assert worker_events[0].schedule_id is None assert worker_events[1].job_id == job.id assert worker_events[1].task_id == 'task_id' assert worker_events[1].schedule_id is None if fail: assert isinstance(worker_events[1], JobFailed) assert type(worker_events[1].exception) is Exception assert isinstance(worker_events[1].traceback, str) else: assert isinstance(worker_events[1], JobSuccessful) assert worker_events[1].return_value == ((1, 2), {'x': 'foo'})
def test_run_job_nonscheduled(self, anyio_backend, target_func, fail, sync_store, portal): def listener(worker_event): print('received event:', worker_event) worker_events.append(worker_event) if len(worker_events) == 2: event.set() worker_events = [] event = threading.Event() job = Job('task_id', func=target_func, args=(1, 2), kwargs={ 'x': 'foo', 'fail': fail }) with SyncWorker(sync_store, portal=portal) as worker: worker.subscribe(listener) portal.call(sync_store.add_job, job) event.wait(2) assert len(worker_events) == 2 assert isinstance(worker_events[0], JobUpdated) assert worker_events[0].job_id == job.id assert worker_events[0].task_id == 'task_id' assert worker_events[0].schedule_id is None assert worker_events[1].job_id == job.id assert worker_events[1].task_id == 'task_id' assert worker_events[1].schedule_id is None if fail: assert isinstance(worker_events[1], JobFailed) assert type(worker_events[1].exception) is Exception assert isinstance(worker_events[1].traceback, str) else: assert isinstance(worker_events[1], JobSuccessful) assert worker_events[1].return_value == ((1, 2), {'x': 'foo'})