Skip to content

Question: How can one self-re-enqueue a job when using _job_id? #416

@joshwilson-dbx

Description

@joshwilson-dbx

I have a use case where once a job completes, i would like it to continuously re-schedule itself for another run at some point in the future. I would also like to ensure that only one instance is queued/running at any given time, so I'm using the _job_id parameter when enqueuing. I cannot use the cron functionality as the delay time is somewhat dynamic and not easily translated to cron.

Options that I've explored so far:

  1. Simply call redis.enqueue_job(..., _job_id=ctx['job_id']) from within the job itself
    • This doesn't work since the current job is still active and prevents the new enqueuing
  2. Raise a Retry exception from within the job after the work has completed
    • This seems like an abuse of this functionality
    • Will likely run into trouble with the _expires and max_tries settings
  3. Set keep_result=0 and enqueue a second job (different name) with a small delay that in turn re-enqueues the original job again
    • Works but is cumbersome and may introduce a race condition
    • Needs a second job function just to enqueue the primary job again
  4. Use keep_result=0 and re-enqueue in the after_job_end function to be sure the job and result keys are no longer present so the re-enqueue can occur.
    • Will probably need to dedicate a specific queue and workers for these jobs
    • Risks the programmer error of enqueuing in the wrong queue

Is there a better way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions