RQ Scheduler

0.14.0 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `rq_scheduler.Scheduler` and schedule a job to run at a specific future time using `enqueue_at` and a recurring job using `cron`. Ensure your Redis server is running and that both `rq-scheduler` and `rq worker` processes are started separately for the scheduled jobs to be processed.

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.

view raw JSON →