API

flask_rq2.app

The core interface of Flask-RQ2.

class flask_rq2.app.RQ(app=None, default_timeout=None, async=None)

The main RQ object to be used in user apps.

__init__(app=None, default_timeout=None, async=None)

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
  • async (bool) – Whether or not to run jobs asynchronously or in-process, defaults to True
default_queue = 'default'

Name of the default queue.

url = 'redis://localhost:6379/0'

The DSN (URL) of the Redis connection.

queues = ['default']

List of queue names for RQ to work on.

queue_path = 'rq.queue.Queue'

Dotted import path to RQ Queue class to use as base class.

worker_path = 'rq.worker.Worker'

Dotted import path to RQ Workers class to use as base class.

job_path = 'rq.job.Job'

Dotted import path to RQ Job class to use as base 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_path = 'flask_rq2.helpers.JobFunctions'

The default job functions class.

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.

init_backends(app)

Initialize the RQ backends with a closure so the RQ job class is aware of the Flask app context.

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)

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, results_ttl=60*60)
def add(x, y):
    return x + y

Adds various helpers to the job function as documented in JobFunctions.

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.
get_scheduler(interval=None)

When installed returns a rq_scheduler.Scheduler instance to schedule job execution, e.g.:

scheduler = rq.get_scheduler(interval=10)
Parameters:interval (int) – Time in seconds of the periodic check for scheduled jobs.
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.helpers

class flask_rq2.helpers.JobFunctions(rq, wrapped, queue_name, timeout, result_ttl, ttl)

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
def add(x, y):
    return x + y

add.queue(1, 2)
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)
add.schedule(datetime(2016, 12, 31, 23, 59, 59), 1, 2)
add.schedule(timedelta(days=14), 1, 2, repeat=1)
cron(pattern, name, *args, **kwargs)

A function to setup a RQ job as a cronjob:

@rq.job
def add(x, y):
    return x + y

add.cron('* * * * *', 'add-some-numbers', 1, 2)

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.

flask_rq2.cli.suspend(*args, **kwargs)

Suspends all workers.

flask_rq2.cli.worker(*args, **kwargs)

Starts an RQ worker.

flask_rq2.script

Support for the argparse based Flask-Script CLI. It wraps the CLI of RQ with the properly configured worker, queue and job classes.

class flask_rq2.script.RQManager(rq, *args, **kwargs)

To use in your own management script, add the following:

from flask_script import Manager
from flask_rq2.script import RQManager

from app import create_app
from jobs import rq

app = create_app()

manager = Manager(app)
manager.add_command('rq', RQManager(rq))

Then you’ll be able to call python manage.py rq info etc.

Please call python manage.py rq --help for more information.