15.2.1.6. Job Queue

The following queue classes are known to privacyIDEA

privacyidea.lib.queue.JOB_COLLECTOR = <privacyidea.lib.queue.JobCollector object>

A singleton is fine here, because it is only used at import time and once when a new app is created. Afterwards, the object is unused.

class privacyidea.lib.queue.JobCollector[source]

For most third-party job queue modules, the jobs are discovered by tracking all functions decorated with a @job decorator. However, in order to invoke the decorator, one usually needs to provide the queue configuration (e.g. the redis server) already. In privacyIDEA, we cannot do that, because the app config is not known yet – it will be known when create_app is called! Thus, we cannot directly use the @job decorator, but need a job collector that collects jobs in privacyIDEA code and registers them with the job queue module when create_app has been called.

property jobs
register_app(app)[source]

Create an instance of a BaseQueue subclass according to the app config’s PI_JOB_QUEUE_CLASS option and store it in the job_queue config. Register all collected jobs with this application. This instance is shared between threads! This function should only be called once per process.

Parameters

app – privacyIDEA app

register_job(name, func, args, kwargs)[source]

Register a job with the collector.

Parameters
  • name – unique name of the job

  • func – function of the job

  • args – arguments passed to the job queue’s register_job method

  • kwargs – keyword arguments passed to the job queue’s register_job method

privacyidea.lib.queue.get_job_queue()[source]

Get the job queue registered with the current app. If no job queue is configured, raise a ServerError.

privacyidea.lib.queue.has_job_queue()[source]

Return a boolean describing whether the current app has an app queue configured.

privacyidea.lib.queue.job(name, *args, **kwargs)[source]

Decorator to mark a job to be collected by the job collector. All arguments are passed to register_job.

privacyidea.lib.queue.register_app(app)[source]

Register the app app with the global job collector, if a PI_JOB_QUEUE_CLASS is non-empty. Do nothing otherwise.

privacyidea.lib.queue.wrap_job(name, result)[source]

Wrap a job and return a function that can be used like the original function. The returned function will always return result. This assumes that a queue is configured! Otherwise, calling the resulting function will fail with a ServerError.

Returns

a function

15.2.1.6.2. Base class

class privacyidea.lib.queues.base.BaseQueue(options)[source]

A queue object represents an external job queue and is configured with a dictionary of options. It allows to register jobs, which are Python functions that may be executed outside of the request lifecycle. Every job is identified by a unique job name. It then allows to delegate (or “enqueue”) an invocation of a job (which is identified by its job name) to the external job queue. Currently, the queue only supports fire-and-forget jobs, i.e. jobs without any return value.

enqueue(name, args, kwargs)[source]

Schedule an invocation of a job on the external job queue.

Parameters
  • name – Unique job name

  • args – Tuple of positional arguments

  • kwargs – Dictionary of keyword arguments

Returns

None

register_job(name, func)[source]

Add a job to the internal registry.

Parameters
  • name – Unique job name

  • func – Function that should be executed by an external job queue