Package: rq-scheduler-bcfg
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_atfromredisimportRedisfromrqimportQueuefromrq_schedulerimportSchedulerfromdatetimeimportdatetimescheduler=Scheduler(connection=Redis())# Get a scheduler for the "default" queue# You can also instantiate a Scheduler using an RQ Queuequeue=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 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 likefromdatetimeimporttimedelta# Schedule a job to run 10 minutes, 1 hour and 1 day laterscheduler.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 withRQ Scheduler.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 itscheduler.schedule(scheduled_time=datetime.utcnow(),# Time for first execution, in UTC timezonefunc=func,# Function to be queuedargs=[arg1,arg2],# Arguments passed into function when executedkwargs={'foo':'bar'},# Keyword arguments passed into function when executedinterval=60,# Time before the function is called again, in secondsrepeat=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 aresult_ttlvalue 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 JobsAs of version 0.6.0,RQ Scheduleralso supports creating Cron Jobs, which you can use for
repeated jobs to run periodically at fixed times, dates or intervals, for more info checkhttps://en.wikipedia.org/wiki/Cron. You can do this via thecronmethod.This is how you do itscheduler.cron(cron_string,# A cron string (e.g. "0 0 * * 0")func=func,# Function to be queuedargs=[arg1,arg2],# Arguments passed into function when executedkwargs={'foo':'bar'},# Keyword arguments passed into function when executedrepeat=10,# Repeat this number of times (None means repeat forever)queue_name=queue_name,# In which queue the job should be put inmeta={'foo':'bar'}# Arbitrary pickleable data on the job itself)Retrieving scheduled jobsSometimes you need to know which jobs have already been scheduled. You can get a
list of enqueued jobs with theget_jobsmethodlist_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:00list_of_job_instances=scheduler.get_jobs(until=datetime(2012,10,30,10))# get all jobs for the next hourlist_of_job_instances=scheduler.get_jobs(until=timedelta(hours=1))# get all jobs with execution timesjobs_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 pythoninoperatorifjob_instanceinscheduler:# Do something# orifjob_idinscheduler:# Do somethingCanceling a jobTo cancel a job, simply pass aJobor a job id toscheduler.cancelscheduler.cancel(job)Note that this method returnsNonewhether the specified job was found or not.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 connectionrqschedulerIf you want to use a different Redis server you could also dorqscheduler--hostlocalhost--port6379--db0The 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 Redis-bor--burst: runs in burst mode (enqueue scheduled jobs whose execution time is in the past and quit)-iINTERVALor--intervalINTERVAL: How often the scheduler checks for new jobs to add to the queue (in seconds, can be floating-point for more precision).-jor--job-class: specify custom job class for rq to use (python module.Class)-qor--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 ofRQ_REDIS_.