Skip to content

Suggested retry_stalled_jobs implementation fails on jobs with queueing lock #1446

@jakajancar

Description

@jakajancar

If another job with same queuing_lock is already queued, the suggested retry_stalled_jobs will fail:

[Log] Traceback (most recent call last):
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/sync_psycopg_connector.py", line 29, in wrap_exceptions
    yield
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/psycopg_connector.py", line 40, in wrap_exceptions
    yield
  File "/root/.local/share/uv/python/cpython-3.11.13-linux-aarch64-gnu/lib/python3.11/contextlib.py", line 97, in inner
    return await func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/psycopg_connector.py", line 207, in execute_query_async
    await cursor.execute(query, self._wrap_json(arguments))
  File "/code/.venv/lib/python3.11/site-packages/psycopg/cursor_async.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "procrastinate_jobs_queueing_lock_idx_v1"
DETAIL:  Key (queueing_lock)=(shop/rfq/27782868-bc7d-48c9-8dfa-40cbd59d894d) already exists.
CONTEXT:  SQL statement "UPDATE procrastinate_jobs
        SET status = 'todo'::procrastinate_job_status,
            attempts = attempts + 1,
            scheduled_at = retry_at,
            priority = COALESCE(new_priority, priority),
            queue_name = COALESCE(new_queue_name, queue_name),
            lock = COALESCE(new_lock, lock)
        WHERE id = job_id AND status = 'doing'
        RETURNING id"
PL/pgSQL function procrastinate_retry_job_v1(bigint,timestamp with time zone,integer,character varying,character varying) line 16 at SQL statement

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/worker.py", line 266, in _process_job
    job_result.result = await ensure_async()
                        ^^^^^^^^^^^^^^^^^^^^
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/worker.py", line 252, in ensure_async
    task_result = await await_func(*job_args, **job.task_kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/code/uptool/tasks.py", line 13, in retry_stalled_jobs
    await app.job_manager.retry_job(job)
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/manager.py", line 470, in retry_job
    await self.retry_job_by_id_async(
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/manager.py", line 506, in retry_job_by_id_async
    await self.connector.execute_query_async(
  File "/root/.local/share/uv/python/cpython-3.11.13-linux-aarch64-gnu/lib/python3.11/contextlib.py", line 96, in inner
    async with self._recreate_cm():
  File "/root/.local/share/uv/python/cpython-3.11.13-linux-aarch64-gnu/lib/python3.11/contextlib.py", line 231, in __aexit__
    await self.gen.athrow(typ, value, traceback)
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/psycopg_connector.py", line 39, in wrap_exceptions
    with sync_psycopg_connector.wrap_exceptions():
  File "/root/.local/share/uv/python/cpython-3.11.13-linux-aarch64-gnu/lib/python3.11/contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/code/.venv/lib/python3.11/site-packages/procrastinate/sync_psycopg_connector.py", line 40, in wrap_exceptions
    raise exceptions.UniqueViolation(
procrastinate.exceptions.UniqueViolation: 
    A unique constraint is violated. The constraint name is available in
    ``exception.constraint_name``.

No problem to catch UniqueViolation, but a user should really not have to worry about this stuff, the whole retrying stalled jobs should just be done automatically :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions