API¶
flask_rq2.app¶
The core interface of Flask-RQ2.
-
class
flask_rq2.app.
RQ
(app=None, default_timeout=None, is_async=None, **kwargs)¶ The main RQ object to be used in user apps.
-
__init__
(app=None, default_timeout=None, is_async=None, **kwargs)¶ Initialize the RQ interface.
Parameters: - app (
flask.Flask
) – Flask application - default_timeout (int) – The default timeout in seconds to use for jobs, defaults to RQ’s default of 180 seconds per job
- is_async (bool) – Whether or not to run jobs asynchronously or
in-process, defaults to
True
- app (
-
default_queue
= 'default'¶ Name of the default queue.
-
default_result_ttl
= 500¶ The fallback default result TTL.
New in version 17.1.
-
redis_url
= 'redis://localhost:6379/0'¶ The DSN (URL) of the Redis connection.
Changed in version 17.1: Renamed from
url
toredis_url
.
-
connection_class
= 'redis.StrictRedis'¶ The Redis client class to use.
New in version 17.1.
-
queues
= ['default']¶ List of queue names for RQ to work on.
-
queue_class
= 'rq.queue.Queue'¶ Dotted import path to RQ Queue class to use as base class.
Changed in version 17.1: Renamed from
queue_path
toqueue_class
.
-
worker_class
= 'rq.worker.Worker'¶ Dotted import path to RQ Workers class to use as base class.
Changed in version 17.1: Renamed from
worker_path
toworker_class
.
-
job_class
= 'flask_rq2.job.FlaskJob'¶ Dotted import path to RQ Job class to use as base class.
Changed in version 17.1: Renamed from
job_path
tojob_class
.
-
scheduler_class
= 'flask_rq2.scheduler.FlaskScheduler'¶ Dotted import path to RQ Scheduler class.
Changed in version 17.1: Renamed from
scheduler_path
toscheduler_class
.Changed in version 18.0: Changed to use own scheduler class.
-
scheduler_queue
= 'default'¶ Name of RQ queue to schedule jobs in by rq-scheduler.
-
scheduler_interval
= 60¶ Time in seconds the scheduler checks for scheduled jobs periodicically.
-
functions_class
= 'flask_rq2.functions.JobFunctions'¶ The default job functions class.
Changed in version 17.1: Renamed from
functions_path
tofunctions_class
. Moved fromflask_rq2.helpers.JobFunctions
to
-
default_timeout
= 180¶ The fallback default timeout value.
-
init_app
(app)¶ Initialize the app, e.g. can be used if factory pattern is used.
-
init_cli
(app)¶ Initialize the Flask CLI support in case it was enabled for the app.
Works with both Flask>=1.0’s CLI support as well as the backport in the Flask-CLI package for Flask<1.0.
-
exception_handler
(callback)¶ Decorator to add an exception handler to the worker, e.g.:
rq = RQ() @rq.exception_handler def my_custom_handler(job, *exc_info): # do custom things here ...
-
job
(func_or_queue=None, timeout=None, result_ttl=None, ttl=None, depends_on=None, at_front=None, meta=None, description=None)¶ Decorator to mark functions for queuing via RQ, e.g.:
rq = RQ() @rq.job def add(x, y): return x + y
or:
@rq.job(timeout=60, result_ttl=60 * 60) def add(x, y): return x + y
Adds various functions to the job as documented in
JobFunctions
.Changed in version 18.0: Adds
depends_on
,at_front
,meta
anddescription
parameters.Parameters: - queue (str) – Name of the queue to add job to, defaults to
flask_rq2.app.RQ.default_queue
. - timeout (int) – The maximum runtime in seconds of the job before it’s considered ‘lost’, defaults to 180.
- result_ttl (int) – Time to persist the job results in Redis, in seconds.
- ttl (int) – The maximum queued time of the job before it’ll be cancelled.
- depends_on (FlaskJob or str) – A job instance or id that the new job depends on.
- at_front (bool) – Whether or not the job is queued in front of all other enqueued jobs.
- meta (dict) – Additional meta data about the job.
- description (str) – Description of the job.
- queue (str) – Name of the queue to add job to, defaults to
-
get_scheduler
(interval=None, queue=None)¶ When installed returns a
rq_scheduler.Scheduler
instance to schedule job execution, e.g.:scheduler = rq.get_scheduler(interval=10)
Parameters:
-
get_queue
(name=None)¶ Returns an RQ queue instance with the given name, e.g.:
default_queue = rq.get_queue() low_queue = rq.get_queue('low')
Parameters: name (str) – Name of the queue to return, defaults to default_queue
.Returns: An RQ queue instance. Return type: rq.queue.Queue
-
get_worker
(*queues)¶ Returns an RQ worker instance for the given queue names, e.g.:
configured_worker = rq.get_worker() default_worker = rq.get_worker('default') default_low_worker = rq.get_worker('default', 'low')
Parameters: *queues – Names of queues the worker should act on, falls back to the configured queues.
-
flask_rq2.cli¶
Support for the Click based Flask CLI via Flask-CLI.
-
flask_rq2.cli.
empty
(*args, **kwargs)¶ Empty given queues.
-
flask_rq2.cli.
info
(*args, **kwargs)¶ RQ command-line monitor.
-
flask_rq2.cli.
requeue
(*args, **kwargs)¶ Requeue failed jobs.
-
flask_rq2.cli.
resume
(*args, **kwargs)¶ Resumes all workers.
-
flask_rq2.cli.
scheduler
(*args, **kwargs)¶ Periodically checks for scheduled jobs.
Default class options to pass to the CLI commands.
-
flask_rq2.cli.
suspend
(*args, **kwargs)¶ Suspends all workers.
-
flask_rq2.cli.
worker
(*args, **kwargs)¶ Starts an RQ worker.
flask_rq2.functions¶
-
class
flask_rq2.functions.
JobFunctions
(rq, wrapped, queue_name, timeout, result_ttl, ttl, depends_on, at_front, meta, description)¶ Some helper functions that are added to a function decorated with a
job()
decorator.-
functions
= ['queue', 'schedule', 'cron']¶ the methods to add to jobs automatically
-
queue
(*args, **kwargs)¶ A function to queue a RQ job, e.g.:
@rq.job(timeout=60) def add(x, y): return x + y add.queue(1, 2, timeout=30)
Parameters: - *args – The positional arguments to pass to the queued job.
- **kwargs – The keyword arguments to pass to the queued job.
- queue (str) – Name of the queue to queue in, defaults to
queue of of job or
default_queue
. - timeout (int) – The job timeout in seconds.
If not provided uses the job’s timeout or
default_timeout
. - description (str) – Description of the job.
- result_ttl (int) – The result TTL in seconds. If not provided
uses the job’s result TTL or
default_result_ttl
. - ttl (int) – The job TTL in seconds. If not provided uses the job’s TTL or no TTL at all.
- depends_on (FlaskJob or str) – A job instance or id that the new job depends on.
- job_id (str) – A custom ID for the new job. Defaults to an
UUID
. - at_front (bool) – Whether or not the job is queued in front of all other enqueued jobs.
- meta (dict) – Additional meta data about the job.
Returns: An RQ job instance.
Return type:
-
schedule
(time_or_delta, *args, **kwargs)¶ A function to schedule running a RQ job at a given time or after a given timespan:
@rq.job def add(x, y): return x + y add.schedule(timedelta(hours=2), 1, 2, timeout=10) add.schedule(datetime(2016, 12, 31, 23, 59, 59), 1, 2) add.schedule(timedelta(days=14), 1, 2, repeat=1)
Parameters: - *args – The positional arguments to pass to the queued job.
- **kwargs – The keyword arguments to pass to the queued job.
- queue (str) – Name of the queue to queue in, defaults to
queue of of job or
default_queue
. - timeout (int) – The job timeout in seconds.
If not provided uses the job’s timeout or
default_timeout
. - description (str) – Description of the job.
- result_ttl (int) – The result TTL in seconds. If not provided
uses the job’s result TTL or
default_result_ttl
. - ttl (int) – The job TTL in seconds. If not provided uses the job’s TTL or no TTL at all.
- repeat (int) – The number of times the job needs to be repeatedly
queued. Requires setting the
interval
parameter. - interval (int) – The interval of repetition as defined by the
repeat
parameter in seconds. - job_id (str) – A custom ID for the new job. Defaults to a UUID.
Returns: An RQ job instance.
Return type:
-
cron
(pattern, name, *args, **kwargs)¶ A function to setup a RQ job as a cronjob:
@rq.job('low', timeout=60) def add(x, y): return x + y add.cron('* * * * *', 'add-some-numbers', 1, 2, timeout=10)
Parameters: - *args – The positional arguments to pass to the queued job.
- **kwargs – The keyword arguments to pass to the queued job.
- pattern (str) – A Crontab pattern.
- name (str) – The name of the cronjob.
- queue (str) – Name of the queue to queue in, defaults to
queue of of job or
default_queue
. - timeout (int) – The job timeout in seconds.
If not provided uses the job’s timeout or
default_timeout
. - description (str) – Description of the job.
- repeat (int) – The number of times the job needs to be repeatedly queued via the cronjob. Take care only using this for cronjob that don’t already repeat themselves natively due to their crontab.
Returns: An RQ job instance.
Return type:
-