Exemplo n.º 1
0
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}
Exemplo n.º 2
0
def test_cancel(assert_atomic, connection):
    q = QueueFactory()
    task = q.enqueue_call()
    with assert_atomic():
        task.cancel()
    assert q.get_task_ids() == []
    assert task.status == TaskStatus.CANCELED
    assert not connection.exists(task.key)

    w = WorkerFactory()
    w.startup()
    task = q.enqueue_call()
    q.dequeue(w)
    with pytest.raises(InvalidOperation):
        with assert_atomic():
            task.cancel()
    assert worker_registry.get_running_tasks() == {w.id: task.id}
    assert connection.exists(task.key)
Exemplo n.º 3
0
def test_process_task(mocker):
    q = QueueFactory()
    wp = WorkerProcess([q])
    wp.worker.startup()
    q.enqueue_call()
    task = q.dequeue(wp.worker)

    def my_execute(task):
        assert wp.worker.state == WorkerState.BUSY
        assert task.status == TaskStatus.RUNNING
        return TaskOutcome("success")

    execute = mocker.patch.object(WorkerProcess, 'execute_task', side_effect=my_execute)
    wp.process_task(task)
    assert task.status == TaskStatus.FINISHED

    q.enqueue_call()
    task = q.dequeue(wp.worker)
    execute.side_effect = ArithmeticError()
    wp.process_task(task)
    assert task.status == TaskStatus.FAILED
    assert 'ArithmeticError' in task.error_message
Exemplo n.º 4
0
def test_signal_shutdown_in_task(suprocess_socket):
    queue = QueueFactory()
    task = queue.enqueue_call(taskwait)
    wp = WorkerProcess([queue])
    process = multiprocessing.Process(target=wp.run)
    process.start()

    with suprocess_socket.accept() as taskconn:
        assert taskconn.poll(1)
        assert taskconn.recv() == "A"
        os.kill(process.pid, signal.SIGTERM)
        assert taskconn.poll(1)
        assert taskconn.recv() == "B"
        process.join(1)
        assert not process.is_alive()
    task.refresh()
    assert task.status == TaskStatus.FAILED
    assert 'Worker shutdown' in task.error_message.splitlines()[-1]
Exemplo n.º 5
0
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