RQ Scheduler
RQ Scheduler provides a job scheduling layer for RQ (Redis Queue), allowing users to schedule jobs to run at specific times or on recurring cron-like schedules. It is an active project, currently at version 0.14.0, with releases coinciding with major RQ updates and feature additions.
Warnings
- breaking RQ Scheduler v0.14.0 and later support RQ 2.0+. Using older versions of `rq-scheduler` with RQ 2.0+ or newer `rq-scheduler` with older RQ versions can lead to compatibility issues or errors.
- breaking Starting with v0.13.0, `rq-scheduler` requires Python 3.6 or newer. Older Python versions are no longer supported.
- gotcha The `rq-scheduler` process is separate from `rq` worker processes. You must run `rq-scheduler` via its command-line tool (`rq-scheduler`) and `rq` workers via `rq worker` for scheduled jobs to be added to the queue and then processed.
- gotcha The `rq-scheduler` instance must connect to the same Redis database as your `rq` workers and application. Using different Redis connections will result in jobs not being scheduled or processed correctly.
Install
-
pip install rq-scheduler
Imports
- Scheduler
from rq_scheduler import Scheduler
Quickstart
import os
from redis import Redis
from rq_scheduler import Scheduler
from datetime import datetime, timedelta
def my_job(arg1, arg2):
print(f'Running job with args: {arg1}, {arg2}')
return arg1 + arg2
# Ensure a Redis connection is available. Use a default if REDIS_URL not set.
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379')
redis_conn = Redis.from_url(redis_url)
# Instantiate the scheduler with the Redis connection
scheduler = Scheduler(connection=redis_conn)
# Example 1: Schedule a job to run once at a specific time
job_at_time = datetime.utcnow() + timedelta(seconds=10)
scheduler.enqueue_at(
job_at_time, # Time to run the job
my_job, # Function to be called
'hello', 'world' # Arguments for the function
)
print(f"Scheduled 'my_job' to run at {job_at_time} UTC")
# Example 2: Schedule a recurring job using cron syntax (every minute)
# Note: This will repeatedly add the job to the queue based on the cron string.
# Ensure 'rq-scheduler' process is running for this to work.
scheduler.cron(
'*/1 * * * *', # Cron string (every minute)
func=my_job, # Function to be called
args=('cron_arg1', 'cron_arg2'), # Arguments for the function
repeat=None, # Run indefinitely
queue_name='default' # Queue to add the job to
)
print("Scheduled 'my_job' to run every minute via cron")
# To run the scheduler, you would execute `rq-scheduler` in your terminal.
# To process jobs, you would execute `rq worker` in your terminal.