rq / rq-scheduler

A lightweight library that adds job scheduling capabilities to RQ (Redis Queue)
MIT License
1.45k stars 229 forks source link

Add `get_job` function to retrieve a job instance by ID #265

Closed davidalber closed 3 years ago

davidalber commented 3 years ago

Hello. Thank you for RQ Scheduler!

I have been using RQ Scheduler and have some situations where I fetch a scheduled job, primarily to find when it is scheduled to be queued for execution by RQ. I have been using Scheduler.get_jobs and then looking through the results to see if the job I'm interested in is present. Of course, the cost of doing that scales with the number of jobs that are scheduled. I have a dashboard where the cost of doing this has become noticeable.

I am interested in having a fast operation to look for the job of interest, but I did not find an existing function to do this in RQ Scheduler. If I missed it, please point me to it. If not, this PR is an attempt at implementing get_job to retrieve a job instance using a job ID. It uses the ZSCORE function to quickly retrieve the job's "schedule at" time.

If this looks like a reasonable way to introduce this, great. I'm happy to incorporate your feedback. Thanks for your time!

selwin commented 3 years ago

This feature is already there. You can do if job_id in scheduler or if job_instance in scheduler:. Take a look at contains.

davidalber commented 3 years ago

@selwin: Hello, and thanks for your feedback!

The situation that I am attempting to improve is retrieving a single job. Using the in operator is useful when I want to know if the job exists, but it returns a boolean, not the job itself. The only existing way I have found to get a job from the queue is to call get_jobs and then iterate through the results. That's really expensive when there's a lot of queued jobs. The get_job function I have added is designed to retrieve a single job by ID quickly.

Is there currently a way to quickly get a single job by ID (not just whether it exists) in RQ Scheduler?