def test_worker_reg_running_tasks(): registry = registries.worker_registry queue = QueueFactory() t1 = queue.enqueue_call() t2 = queue.enqueue_call() worker1 = WorkerFactory(queues=[queue]) worker2 = WorkerFactory(queues=[queue]) worker1.startup() worker2.startup() assert registry.get_running_tasks() == dict() queue.push(t1) queue.dequeue(worker1) assert registry.get_running_tasks() == {worker1.id: t1.id} worker1.start_task(t1) queue.push(t2) queue.dequeue(worker2) worker2.start_task(t2) assert registry.get_running_tasks() == { worker1.id: t1.id, worker2.id: t2.id } worker1.end_task(t1, TaskOutcome("success")) assert registry.get_running_tasks() == {worker2.id: t2.id}
def test_is_enqueued(self, stub): schedule = CrontabSchedule('* * * * *') queue = Queue() worker = WorkerFactory() worker.startup() se = SchedulerEntry("a", {"task": stub.path, "schedule": schedule}) assert se.is_enqueued() is False task = se.enqueue() assert se.is_enqueued() is True task.cancel() assert se.is_enqueued() is False se.enqueue() assert se.is_enqueued() is True task = queue.dequeue(worker) assert se.is_enqueued() is True worker.start_task(task) assert se.is_enqueued() is True worker.end_task(task, TaskOutcome("success")) assert se.is_enqueued() is False se.enqueue() task = queue.dequeue(worker) worker.start_task(task) assert se.is_enqueued() is True worker.end_task(task, TaskOutcome("failure")) assert se.is_enqueued() is False
def test_state_transistions(assert_atomic, connection, time_mocker, stub): time = time_mocker('redis_tasks.task.utcnow') task = Task(reentrant_stub) q = QueueFactory() w = WorkerFactory() w.startup() # enqueue time.step() assert not connection.exists(task.key) with assert_atomic(): task.enqueue(q) assert q.get_task_ids() == [task.id] assert connection.exists(task.key) for t in [task, Task.fetch(task.id)]: assert t.enqueued_at == time.now assert t.status == TaskStatus.QUEUED assert t.origin == q.name # dequeue task = q.dequeue(w) assert q.get_task_ids() == [] assert worker_registry.get_running_tasks() == {w.id: task.id} # set_running time.step() with assert_atomic(): w.start_task(task) assert worker_registry.get_running_tasks() == {w.id: task.id} for t in [task, Task.fetch(task.id)]: assert t.status == TaskStatus.RUNNING assert t.started_at == time.now # requeue time.step() with assert_atomic(): w.end_task(task, TaskOutcome("requeue")) assert worker_registry.get_running_tasks() == dict() assert q.get_task_ids() == [task.id] for t in [task, Task.fetch(task.id)]: assert t.status == TaskStatus.QUEUED assert t.started_at is None # set_finished task = q.dequeue(w) w.start_task(task) time.step() with assert_atomic(): w.end_task(task, TaskOutcome("success")) assert q.get_task_ids() == [] assert finished_task_registry.get_task_ids() == [task.id] for t in [task, Task.fetch(task.id)]: assert t.status == TaskStatus.FINISHED assert t.ended_at == time.now # set_failed task = q.enqueue_call(stub) task = q.dequeue(w) w.start_task(task) assert worker_registry.get_running_tasks() == {w.id: task.id} time.step() with assert_atomic(): w.end_task(task, TaskOutcome("failure", message="my error")) assert worker_registry.get_running_tasks() == dict() assert failed_task_registry.get_task_ids() == [task.id] for t in [task, Task.fetch(task.id)]: assert t.status == TaskStatus.FAILED assert t.error_message == "my error" assert t.ended_at == time.now