Example #1
0
async def test_job_deferrer_defer_async(job_store, connector):

    job = jobs.Job(
        queue="marsupilami",
        lock="sher",
        queueing_lock="houba",
        task_name="mytask",
        task_kwargs={"a": "b"},
    )

    deferrer = jobs.JobDeferrer(job=job, job_store=job_store)
    id = await deferrer.defer_async(c=3)

    assert id == 1

    assert connector.jobs == {
        1: {
            "args": {"a": "b", "c": 3},
            "attempts": 0,
            "id": 1,
            "lock": "sher",
            "queueing_lock": "houba",
            "queue_name": "marsupilami",
            "scheduled_at": None,
            "status": "todo",
            "task_name": "mytask",
        }
    }
Example #2
0
def configure_task(
    *,
    name: str,
    job_store: store.BaseJobStore,
    lock: Optional[str] = None,
    task_kwargs: Optional[types.JSONDict] = None,
    schedule_at: Optional[datetime.datetime] = None,
    schedule_in: Optional[Dict[str, int]] = None,
    queue: str = jobs.DEFAULT_QUEUE,
) -> jobs.JobDeferrer:
    if schedule_at and schedule_in is not None:
        raise ValueError("Cannot set both schedule_at and schedule_in")

    if schedule_in is not None:
        schedule_at = pendulum.now("UTC").add(**schedule_in)

    lock = lock or str(uuid.uuid4())
    task_kwargs = task_kwargs or {}
    return jobs.JobDeferrer(
        job=jobs.Job(
            id=None,
            lock=lock,
            task_name=name,
            queue=queue,
            task_kwargs=task_kwargs,
            scheduled_at=schedule_at,
        ),
        job_store=job_store,
    )
Example #3
0
def configure_task(
    *,
    name: str,
    job_store: store.JobStore,
    lock: Optional[str] = None,
    queueing_lock: Optional[str] = None,
    task_kwargs: Optional[types.JSONDict] = None,
    schedule_at: Optional[datetime.datetime] = None,
    schedule_in: Optional[Dict[str, int]] = None,
    queue: str = jobs.DEFAULT_QUEUE,
) -> jobs.JobDeferrer:
    if schedule_at and schedule_in is not None:
        raise ValueError("Cannot set both schedule_at and schedule_in")

    if schedule_in is not None:
        schedule_at = utils.utcnow() + datetime.timedelta(**schedule_in)

    task_kwargs = task_kwargs or {}
    return jobs.JobDeferrer(
        job=jobs.Job(
            id=None,
            lock=lock,
            queueing_lock=queueing_lock,
            task_name=name,
            queue=queue,
            task_kwargs=task_kwargs,
            scheduled_at=schedule_at,
        ),
        job_store=job_store,
    )
Example #4
0
def test_job_deferrer_defer(job_store):

    job = jobs.Job(
        queue="marsupilami", lock="sher", task_name="mytask", task_kwargs={"a": "b"}
    )

    id = jobs.JobDeferrer(job=job, job_store=job_store).defer(c=3)

    assert id == 1

    assert job_store.jobs == {
        1: {
            "args": {"a": "b", "c": 3},
            "attempts": 0,
            "id": 1,
            "lock": "sher",
            "queue_name": "marsupilami",
            "scheduled_at": None,
            "started_at": None,
            "status": "todo",
            "task_name": "mytask",
        }
    }