Provides job scheduling capabilities to RQ (Redis Queue)
Project description
RQ Scheduler
RQ Scheduler is a small package that adds job scheduling capabilities to RQ, a Redis based Python queuing library.
Requirements
Installation
You can install RQ Scheduler via pip:
pip install rq-scheduler
Or you can download the latest stable package from PyPI.
Usage
Schedule a job involves doing two different things:
Putting a job in the scheduler
Running a scheduler that will move scheduled jobs into queues when the time comes
Scheduling a Job
There are two ways you can schedule a job. The first is using RQ Scheduler’s enqueue_at
from redis import Redis
from rq import Queue
from rq_scheduler import Scheduler
from datetime import datetime
scheduler = Scheduler(connection=Redis()) # Get a scheduler for the "default" queue
# You can also instantiate a Scheduler using an RQ Queue
queue = Queue('foo', connection=Redis())
scheduler = Scheduler(queue=queue)
# Puts a job into the scheduler. The API is similar to RQ except that it
# takes a datetime object as first argument. So for example to schedule a
# job to run on Jan 1st 2020 we do:
scheduler.enqueue_at(datetime(2020, 1, 1), func) # Date time should be in UTC
# Here's another example scheduling a job to run at a specific date and time (in UTC),
# complete with args and kwargs.
scheduler.enqueue_at(datetime(2020, 1, 1, 3, 4), func, foo, bar=baz)
The second way is using enqueue_in. Instead of taking a datetime object, this method expects a timedelta and schedules the job to run at X seconds/minutes/hours/days/weeks later. For example, if we want to monitor how popular a tweet is a few times during the course of the day, we could do something like
from datetime import timedelta
# Schedule a job to run 10 minutes, 1 hour and 1 day later
scheduler.enqueue_in(timedelta(minutes=10), count_retweets, tweet_id)
scheduler.enqueue_in(timedelta(hours=1), count_retweets, tweet_id)
scheduler.enqueue_in(timedelta(days=1), count_retweets, tweet_id)
IMPORTANT: You should always use UTC datetime when working with RQ Scheduler.
Periodic & Repeated Jobs
As of version 0.3, RQ Scheduler also supports creating periodic and repeated jobs. You can do this via the schedule method. Note that this feature needs RQ >= 0.3.1.
This is how you do it
scheduler.schedule(
scheduled_time=datetime.utcnow(), # Time for first execution, in UTC timezone
func=func, # Function to be queued
args=[arg1, arg2], # Arguments passed into function when executed
kwargs={'foo': 'bar'}, # Keyword arguments passed into function when executed
interval=60, # Time before the function is called again, in seconds
repeat=10, # Repeat this number of times (None means repeat forever)
meta={'foo': 'bar'} # Arbitrary pickleable data on the job itself
)
IMPORTANT NOTE: If you set up a repeated job, you must make sure that you either do not set a result_ttl value or you set a value larger than the interval. Otherwise, the entry with the job details will expire and the job will not get re-scheduled.
Cron Jobs
As of version 0.6.0, RQ Scheduler also supports creating Cron Jobs, which you can use for repeated jobs to run periodically at fixed times, dates or intervals, for more info check https://en.wikipedia.org/wiki/Cron. You can do this via the cron method.
This is how you do it
scheduler.cron(
cron_string, # A cron string (e.g. "0 0 * * 0")
func=func, # Function to be queued
args=[arg1, arg2], # Arguments passed into function when executed
kwargs={'foo': 'bar'}, # Keyword arguments passed into function when executed
repeat=10, # Repeat this number of times (None means repeat forever)
queue_name=queue_name, # In which queue the job should be put in
meta={'foo': 'bar'} # Arbitrary pickleable data on the job itself
)
Retrieving scheduled jobs
Sometimes you need to know which jobs have already been scheduled. You can get a list of enqueued jobs with the get_jobs method
list_of_job_instances = scheduler.get_jobs()
In it’s simplest form (as seen in the above example) this method returns a list of all job instances that are currently scheduled for execution.
Additionally the method takes two optional keyword arguments until and with_times. The first one specifies up to which point in time scheduled jobs should be returned. It can be given as either a datetime / timedelta instance or an integer denoting the number of seconds since epoch (1970-01-01 00:00:00). The second argument is a boolean that determines whether the scheduled execution time should be returned along with the job instances.
Example
# get all jobs until 2012-11-30 10:00:00
list_of_job_instances = scheduler.get_jobs(until=datetime(2012, 10, 30, 10))
# get all jobs for the next hour
list_of_job_instances = scheduler.get_jobs(until=timedelta(hours=1))
# get all jobs with execution times
jobs_and_times = scheduler.get_jobs(with_times=True)
# returns a list of tuples:
# [(<rq.job.Job object at 0x123456789>, datetime.datetime(2012, 11, 25, 12, 30)), ...]
Checking if a job is scheduled
You can check whether a specific job instance or job id is scheduled for execution using the familiar python in operator
if job_instance in scheduler:
# Do something
# or
if job_id in scheduler:
# Do something
Canceling a job
To cancel a job, simply pass a Job or a job id to scheduler.cancel
scheduler.cancel(job)
Note that this method returns None whether the specified job was found or not.
Running the scheduler
RQ Scheduler comes with a script rqscheduler that runs a scheduler process that polls Redis once every minute and move scheduled jobs to the relevant queues when they need to be executed
# This runs a scheduler process using the default Redis connection
rqscheduler
If you want to use a different Redis server you could also do
rqscheduler --host localhost --port 6379 --db 0
The script accepts these arguments:
-H or --host: Redis server to connect to
-p or --port: port to connect to
-d or --db: Redis db to use
-P or --password: password to connect to Redis
-b or --burst: runs in burst mode (enqueue scheduled jobs whose execution time is in the past and quit)
-i INTERVAL or --interval INTERVAL: How often the scheduler checks for new jobs to add to the queue (in seconds, can be floating-point for more precision).
-j or --job-class: specify custom job class for rq to use (python module.Class)
-q or --queue-class: specify custom queue class for rq to use (python module.Class)
The arguments pull default values from environment variables with the same names but with a prefix of RQ_REDIS_.
Running the Scheduler as a Service on Ubuntu
sudo /etc/systemd/system/rqscheduler.service
[Unit]
Description=RQScheduler
After=network.target
[Service]
ExecStart=/home/<<User>>/.virtualenvs/<<YourVirtualEnv>>/bin/python \
/home/<<User>>/.virtualenvs/<<YourVirtualEnv>>/lib/<<YourPythonVersion>>/site-packages/rq_scheduler/scripts/rqscheduler.py
[Install]
WantedBy=multi-user.target
You will also want to add any command line parameters if your configuration is not localhost or not set in the environmnt variabes.
Start, check Status and Enable the service
sudo systemctl start rqscheduler.service
sudo systemctl status rqscheduler.service
sudo systemctl enable rqscheduler.service
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rq-scheduler-0.9.tar.gz
.
File metadata
- Download URL: rq-scheduler-0.9.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff7d45b34a8a39c9c83634f642aeef950641c75c4eeea3fa140bf574bfc6aca2 |
|
MD5 | 3f2fc1e69fc8c7e6f78d938d73127654 |
|
BLAKE2b-256 | 847024126f656da4e2845bd6ee09f7d149891ba64ec587e9b5f26109613e10f8 |
File details
Details for the file rq_scheduler-0.9-py2.py3-none-any.whl
.
File metadata
- Download URL: rq_scheduler-0.9-py2.py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90eb3915e31cc2032c301e5ab1fd5ad6f23d9500435046995e009f098e18efbe |
|
MD5 | 1de592a8f8d2658285be207116341781 |
|
BLAKE2b-256 | da970180f3c327b2102cf1cfd32a248e6496478c8bd1619a78218aac0db46844 |