How to use the arq.jobs.Job function in arq

To help you get started, we’ve selected a few arq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github samuelcolvin / arq / tests / test_worker.py View on Github external
async def test_repeat_job_result(arq_redis: ArqRedis, worker):
    j1 = await arq_redis.enqueue_job('foobar', _job_id='job_id')
    assert isinstance(j1, Job)
    assert await j1.status() == JobStatus.queued

    assert await arq_redis.enqueue_job('foobar', _job_id='job_id') is None

    await worker(functions=[foobar]).run_check()
    assert await j1.status() == JobStatus.complete

    assert await arq_redis.enqueue_job('foobar', _job_id='job_id') is None
github samuelcolvin / arq / tests / test_jobs.py View on Github external
async def test_result_timeout(arq_redis: ArqRedis):
    j = Job('foobar', arq_redis)
    with pytest.raises(asyncio.TimeoutError):
        await j.result(0.1, pole_delay=0)
github samuelcolvin / arq / tests / test_main.py View on Github external
async def test_repeat_job(arq_redis: ArqRedis):
    j1 = await arq_redis.enqueue_job('foobar', _job_id='job_id')
    assert isinstance(j1, Job)
    j2 = await arq_redis.enqueue_job('foobar', _job_id='job_id')
    assert j2 is None
github samuelcolvin / arq / tests / test_customisation.py View on Github external
async def test_switch_job_class(loop):
    worker = DatetimeWorker(loop=loop, burst=True, shadows=[RealJobActor])
    assert worker.job_class is None
    await worker.run()
    assert worker.job_class == Job
    await worker.close()
github samuelcolvin / arq / tests / test_jobs.py View on Github external
async def test_unknown(arq_redis: ArqRedis):
    j = Job('foobar', arq_redis)
    assert JobStatus.not_found == await j.status()
    info = await j.info()
    assert info is None
github samuelcolvin / arq / tests / test_main.py View on Github external
async def test_defer_until(arq_redis: ArqRedis):
    j1 = await arq_redis.enqueue_job('foobar', _job_id='job_id', _defer_until=datetime(2032, 1, 1))
    assert isinstance(j1, Job)
    score = await arq_redis.zscore(default_queue_name, 'job_id')
    assert score == 1_956_528_000_000
github samuelcolvin / arq / arq / connections.py View on Github external
async def _get_job_result(self, key) -> JobResult:
        job_id = key[len(result_key_prefix) :]
        job = Job(job_id, self, _deserializer=self.job_deserializer)
        r = await job.result_info()
        r.job_id = job_id
        return r
github samuelcolvin / arq / arq / connections.py View on Github external
score = enqueue_time_ms

            expires_ms = expires_ms or score - enqueue_time_ms + expires_extra_ms

            job = serialize_job(function, args, kwargs, _job_try, enqueue_time_ms, serializer=self.job_serializer)
            tr = conn.multi_exec()
            tr.psetex(job_key, expires_ms, job)
            tr.zadd(_queue_name, score, job_id)
            try:
                await tr.execute()
            except MultiExecError:
                # job got enqueued since we checked 'job_exists'
                # https://github.com/samuelcolvin/arq/issues/131, avoid warnings in log
                await asyncio.gather(*tr._results, return_exceptions=True)
                return
        return Job(job_id, redis=self, _queue_name=_queue_name, _deserializer=self.job_deserializer)
github samuelcolvin / arq / arq / connections.py View on Github external
async def enqueue_job(
        self,
        function: str,
        *args: Any,
        _job_id: Optional[str] = None,
        _queue_name: str = default_queue_name,
        _defer_until: Optional[datetime] = None,
        _defer_by: Union[None, int, float, timedelta] = None,
        _expires: Union[None, int, float, timedelta] = None,
        _job_try: Optional[int] = None,
        **kwargs: Any,
    ) -> Optional[Job]:
        """
        Enqueue a job.

        :param function: Name of the function to call
        :param args: args to pass to the function
        :param _job_id: ID of the job, can be used to enforce job uniqueness
        :param _queue_name: queue of the job, can be used to create job in different queue
        :param _defer_until: datetime at which to run the job
        :param _defer_by: duration to wait before running the job
        :param _expires: if the job still hasn't started after this duration, do not run it
        :param _job_try: useful when re-enqueueing jobs within a job
        :param kwargs: any keyword arguments to pass to the function
        :return: :class:`arq.jobs.Job` instance or ``None`` if a job with this ID already exists
        """
        job_id = _job_id or uuid4().hex
        job_key = job_key_prefix + job_id