Queue

Job

class odoo.addons.queue_job.job.DelayableRecordset(recordset, priority=None, eta=None, max_retries=None, description=None, channel=None)[source]

Bases: object

Allow to delay a method for a recordset

Usage:

delayable = DelayableRecordset(recordset, priority=20)
delayable.method(args, kwargs)

method must be a method of the recordset’s Model, decorated with job().

The method call will be processed asynchronously in the job queue, with the passed arguments.

class odoo.addons.queue_job.job.Job(func, args=None, kwargs=None, priority=None, eta=None, job_uuid=None, max_retries=None, description=None, channel=None)[source]

Bases: object

A Job is a task to execute.

uuid

Id (UUID) of the job.

state

State of the job, can pending, enqueued, started, done or failed. The start state is pending and the final state is done.

retry

The current try, starts at 0 and each time the job is executed, it increases by 1.

max_retries

The maximum number of retries allowed before the job is considered as failed.

args

Arguments passed to the function when executed.

kwargs

Keyword arguments passed to the function when executed.

description

Human description of the job.

func

The python function itself.

model_name

Odoo model on which the job will run.

priority

Priority of the job, 0 being the higher priority.

date_created

Date and time when the job was created.

date_enqueued

Date and time when the job was enqueued.

date_started

Date and time when the job was started.

date_done

Date and time when the job was done.

result

A description of the result (for humans).

exc_info

Exception information (traceback) when the job failed.

user_id

Odoo user id which created the job

eta

Estimated Time of Arrival of the job. It will not be executed before this date/time.

recordset

Model recordset when we are on a delayed Model method

classmethod load(env, job_uuid)[source]

Read a job from the Database

classmethod enqueue(func, args=None, kwargs=None, priority=None, eta=None, max_retries=None, description=None, channel=None)[source]

Create a Job and enqueue it in the queue. Return the job uuid.

This expects the arguments specific to the job to be already extracted from the ones to pass to the job function.

static db_record_from_uuid(env, job_uuid)[source]
perform()[source]

Execute the job.

The job is executed with the user which has initiated it.

store()[source]

Store the Job

db_record()[source]
func
description
uuid

Job ID, this is an UUID

eta
set_pending(result=None, reset_retry=True)[source]
set_enqueued()[source]
set_started()[source]
set_done(result=None)[source]
set_failed(exc_info=None)[source]
postpone(result=None, seconds=None)[source]

Write an estimated time arrival to n seconds later than now. Used when an retryable exception want to retry a job later.

related_action()[source]
odoo.addons.queue_job.job.job(func=None, default_channel='root', retry_pattern=None)[source]

Decorator for jobs.

Optional argument:

Parameters:
  • default_channel – the channel wherein the job will be assigned. This channel is set at the installation of the module and can be manually changed later using the views.
  • retry_pattern (dict(retry_count,retry_eta_seconds)) – The retry pattern to use for postponing a job. If a job is postponed and there is no eta specified, the eta will be determined from the dict in retry_pattern. When no retry pattern is provided, jobs will be retried after RETRY_INTERVAL seconds.

Indicates that a method of a Model can be delayed in the Job Queue.

When a method has the @job decorator, its calls can then be delayed with:

recordset.with_delay(priority=10).the_method(args, **kwargs)

Where the_method is the method decorated with @job. Its arguments and keyword arguments will be kept in the Job Queue for its asynchronous execution.

default_channel indicates in which channel the job must be executed

retry_pattern is a dict where keys are the count of retries and the values are the delay to postpone a job.

Example:

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.multi
    @job
    def export_one_thing(self, one_thing):
        # work
        # export one_thing

# [...]

env['a.model'].export_one_thing(the_thing_to_export)
# => normal and synchronous function call

env['a.model'].with_delay().export_one_thing(the_thing_to_export)
# => the job will be executed as soon as possible

delayable = env['a.model'].with_delay(priority=30, eta=60*60*5)
delayable.export_one_thing(the_thing_to_export)
# => the job will be executed with a low priority and not before a
# delay of 5 hours from now

@job(default_channel='root.subchannel')
def export_one_thing(one_thing):
    # work
    # export one_thing

@job(retry_pattern={1: 10 * 60,
                    5: 20 * 60,
                    10: 30 * 60,
                    15: 12 * 60 * 60})
def retryable_example():
    # 5 first retries postponed 10 minutes later
    # retries 5 to 10 postponed 20 minutes later
    # retries 10 to 15 postponed 30 minutes later
    # all subsequent retries postponed 12 hours later
    raise RetryableJobError('Must be retried later')

env['a.model'].with_delay().retryable_example()

See also: related_action() a related action can be attached to a job

odoo.addons.queue_job.job.related_action(action=None, **kwargs)[source]

Attach a Related Action to a job.

A Related Action will appear as a button on the Odoo view. The button will execute the action, usually it will open the form view of the record related to the job.

The action must be a method on the queue.job model.

Example usage:

class QueueJob(models.Model):
    _inherit = 'queue.job'

    @api.multi
    def related_action_partner(self):
        self.ensure_one()
        model = self.model_name
        partner = self.env[model].browse(self.record_ids)
        # possibly get the real ID if partner_id is a binding ID
        action = {
            'name': _("Partner"),
            'type': 'ir.actions.act_window',
            'res_model': model,
            'view_type': 'form',
            'view_mode': 'form',
            'res_id': partner.id,
        }
        return action

class ResPartner(models.Model):
    _inherit = 'res.partner'

    @api.multi
    @job
    @related_action(action='related_action_partner')
    def export_partner(self):
        # ...

The kwargs are transmitted to the action:

class QueueJob(models.Model):
    _inherit = 'queue.job'

    @api.multi
    def related_action_product(self, extra_arg=1):
        assert extra_arg == 2
        model = self.model_name
        ...

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.multi
    @job
    @related_action(action='related_action_product', extra_arg=2)
    def export_product(self):
        # ...

Models

class odoo.addons.queue_job.models.base.Base(pool, cr)[source]

Bases: odoo.models.BaseModel

The base model, which is implicitly inherited by all models.

with_delay(priority=None, eta=None, max_retries=None, description=None, channel=None)[source]

Return a DelayableRecordset

The returned instance allow to enqueue any method of the recordset’s Model which is decorated by job().

Usage:

self.env['res.users'].with_delay().write({'name': 'test'})

In the line above, in so far write is allowed to be delayed with @job, the write will be executed in an asynchronous job.

Parameters:
  • priority – Priority of the job, 0 being the higher priority. Default is 10.
  • eta – Estimated Time of Arrival of the job. It will not be executed before this date/time.
  • max_retries – maximum number of retries before giving up and set the job state to ‘failed’. A value of 0 means infinite retries. Default is 5.
  • description – human description of the job. If None, description is computed from the function doc or name
  • channel – the complete name of the channel to use to process the function. If specified it overrides the one defined on the function
Returns:

instance of a DelayableRecordset

Return type:

odoo.addons.queue_job.job.DelayableRecordset

odoo.addons.queue_job.models.queue_job.channel_func_name(method)[source]
class odoo.addons.queue_job.models.queue_job.QueueJob(pool, cr)[source]

Bases: odoo.models.Model

Job status and result

uuid

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
user_id

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

Parameters:
  • comodel_name – name of the target model (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

company_id

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

Parameters:
  • comodel_name – name of the target model (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
model_name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
method_name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
record_ids

Serialized fields provide the storage for sparse fields.

args

Serialized fields provide the storage for sparse fields.

kwargs

Serialized fields provide the storage for sparse fields.

func_string

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
state
Parameters:
  • selection – specifies the possible values for this field. It is given as either a list of pairs (value, string), or a model method, or a method name.
  • selection_add – provides an extension of the selection in the case of an overridden field. It is a list of pairs (value, string).

The attribute selection is mandatory except in the case of related fields or field extensions.

priority
exc_info

Very similar to Char but used for longer contents, does not have a size and usually displayed as a multiline text box.

Parameters:translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
result

Very similar to Char but used for longer contents, does not have a size and usually displayed as a multiline text box.

Parameters:translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
date_created
date_started
date_enqueued
date_done
eta
retry
max_retries
channel_method_name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
job_function_id

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

Parameters:
  • comodel_name – name of the target model (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

channel

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.

Open the related action associated to the job

button_done()[source]
requeue()[source]
write(vals)[source]
autovacuum()[source]

Delete all jobs done since more than _removal_interval days.

Called from a cron.

class odoo.addons.queue_job.models.queue_job.RequeueJob(pool, cr)[source]

Bases: odoo.models.TransientModel

job_ids

Many2many field; the value of such a field is the recordset.

Parameters:comodel_name – name of the target model (string)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

Parameters:
  • relation – optional name of the table that stores the relation in the database (string)
  • column1 – optional name of the column referring to “these” records in the table relation (string)
  • column2 – optional name of the column referring to “those” records in the table relation (string)

The attributes relation, column1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!

Parameters:
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • limit – optional limit to use upon read (integer)
requeue()[source]
class odoo.addons.queue_job.models.queue_job.JobChannel(pool, cr)[source]

Bases: odoo.models.Model

name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
complete_name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
parent_id

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

Parameters:
  • comodel_name – name of the target model (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

job_function_ids

One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.

Parameters:
  • comodel_name – name of the target model (string)
  • inverse_name – name of the inverse Many2one field in comodel_name (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • limit – optional limit to use upon read (integer)

The attributes comodel_name and inverse_name are mandatory except in the case of related fields or field extensions.

parent_required()[source]
write(values)[source]
name_get()[source]
class odoo.addons.queue_job.models.queue_job.JobFunction(pool, cr)[source]

Bases: odoo.models.Model

name

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.
channel_id

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

Parameters:
  • comodel_name – name of the target model (string)
  • domain – an optional domain to set on candidate values on the client side (domain or string)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

channel

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

Parameters:
  • size (int) – the maximum size of values stored for that field
  • translate – enable the translation of the field’s values; use translate=True to translate field values as a whole; translate may also be a callable such that translate(callback, value) translates value by using callback(term) to retrieve the translation of terms.