Package: rq-scheduler-ng
RQ SchedulerRQ Scheduleris a small package that
adds job scheduling capabilities toRQ,
aRedisbased Python queuing library.RequirementsRQInstallationYou can installRQ Schedulervia pip:pip install rq-schedulerOr you can download the latest stable package fromPyPI.UsageSchedule a job involves doing two different things:Putting a job in the schedulerRunning a scheduler that will move scheduled jobs into queues when the time comesScheduling a JobThere are two ways you can schedule a job. The first is using RQ Scheduler’senqueue_at:from redis import Redis
from rq_scheduler import Scheduler
from datetime import datetime

scheduler = Scheduler(connection=Redis()) # Get a scheduler for the "default" 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)

# 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 usingenqueue_in. Instead of taking adatetimeobject,
this method expects atimedeltaand 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)Periodic & Repeated JobsAs of version 0.3,RQ Scheduleralso supports creating periodic and repeated jobs.
You can do this via theschedulemethod. Note that this feature needsRQ>= 0.3.1.This is how you do it:scheduler.schedule(
    scheduled_time=datetime.now(), # 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)
)Retrieving scheduled jobsSometimes you need to know which jobs have already been scheduled. You can get a
list of enqueued jobs with theget_jobsmethod: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 argumentsuntilandwith_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 boolen 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 scheduledYou can check whether a specific job instance or job id is scheduled for
execution using the familiar pythoninoperator:if job_instance in scheduler:
    # Do something
# or
if job_id in scheduler:
    # Do somethingCanceling a jobTo cancel a job, simply do:scheduler.cancel(job)Running the schedulerRQ Schedulercomes with a scriptrqschedulerthat 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
rqschedulerIf you want to use a different Redis server you could also do:rqscheduler --host localhost --port 6379 --db 0The script accepts these arguments:-Hor--host: Redis server to connect to-por--port: port to connect to-dor--db: Redis db to use-Por--password: password to connect to RedisThe arguments pull default values from environment variables with the
same names but with a prefix ofRQ_REDIS_.ChangelogVersion 0.5.1Travis CI fixes. Thanks Steven Kryskalla!Modified default logging configuration. You can pass in the-vor--verboseargument
torqschedulerscript for more verbose logging.RQ Scheduler now registers Queue name when a new job is scheduled. Thanks @alejandrodob !You can now schedule jobs with string references likescheduler.schedule(scheduled_time=now,func='foo.bar').
Thanks @SirScott !rqschedulerscript now accepts floating point intervals. Thanks Alexander Pikovsky!Version 0.5.0IMPORTANT! Job timestamps are now stored and interpreted in UTC format.
If you have existing scheduled jobs, you should probably change their timestamp
to UTC before upgrading to 0.5.0. Thanks @michaelbrooks!You can now configure Redis connection via environment variables. Thanks @malthe!rqschedulerscript now accepts--pidargument. Thanks @jsoncorwin!Version 0.4.0Supports Python 3!Scheduler.schedulenow allows jobtimeoutto be specifiedrqschedulerallows Redis connection to be specified via--urlargumentrqschedulernow accepts--pathargumentVersion 0.3.6Scheduler key is not set to expire a few seconds after the next scheduling
operation. This solves the issue ofrqschedulerrefusing to start after
an unexpected shut down.Version 0.3.5SupportStrictRedisVersion 0.3.4Scheduler related job attributes (intervalandrepeat) are now stored
injob.metaintroduced in RQ 0.3.4Version 0.3.3You can now check whether a job is scheduled for execution usingjob in schedulersyntaxAddedscheduler.get_jobsmethodscheduler.enqueueandscheduler.enqueue_periodicwill now raise a
DeprecationWarning, please usescheduler.scheduleinsteadVersion 0.3.2Periodic jobs now requireRQ>= 0.3.1Version 0.3Added the capability to create periodic (cron) and repeated job usingscheduler.enqueue