Package: odoo14-addon-queue-job
This addon adds an integrated Job Queue to Odoo.It allows to postpone method calls executed asynchronously.Jobs are executed in the background by aJobrunner, in their own transaction.Example:fromodooimportmodels,fields,apiclassMyModel(models.Model):_name='my.model'defmy_method(self,a,k=None):_logger.info('executed with a:%sand k:%s',a,k)classMyOtherModel(models.Model):_name='my.other.model'defbutton_do_stuff(self):self.env['my.model'].with_delay().my_method('a',k=2)In the snippet of code above, when we callbutton_do_stuff, a jobcapturing
the method and argumentswill be postponed.  It will be executed as soon as the
Jobrunner has a free bucket, which can be instantaneous if no other job is
running.Features:Views for jobs, jobs are stored in PostgreSQLJobrunner: execute the jobs, highly efficient thanks to PostgreSQL’s NOTIFYChannels: give a capacity for the root channel and its sub-channels and
segregate jobs in them. Allow for instance to restrict heavy jobs to be
executed one at a time while little ones are executed 4 at a times.Retries: Ability to retry jobs by raising a type of exceptionRetry Pattern: the 3 first tries, retry after 10 seconds, the 5 next tries,
retry after 1 minutes, …Job properties: priorities, estimated time of arrival (ETA), custom
description, number of retriesRelated Actions: link an action on the job view, such as open the record
concerned by the jobTable of contentsInstallationConfigurationUsageDevelopersDelaying jobsEnqueing Job OptionsConfigure default options for jobsTestingTips and tricksPatternsKnown issues / RoadmapChangelogBug TrackerCreditsAuthorsContributorsMaintainersInstallationBe sure to have therequestslibrary.ConfigurationUsing environment variables and command line:Adjust environment variables (optional):ODOO_QUEUE_JOB_CHANNELS=root:4or any other channels configuration.
The default isroot:1ifxmlrpc_portis not set:ODOO_QUEUE_JOB_PORT=8069Start Odoo with--load=web,queue_joband--workersgreater than 1.[1]Using the Odoo configuration file:[options](...)workers=6server_wide_modules=web,queue_job(...)[queue_job]channels=root:2Confirm the runner is starting correctly by checking the odoo log file:...INFO...queue_job.jobrunner.runner: starting
...INFO...queue_job.jobrunner.runner: initializing database connections
...INFO...queue_job.jobrunner.runner: queue job runner ready for db <dbname>
...INFO...queue_job.jobrunner.runner: database connections readyCreate jobs (eg usingbase_import_async) and observe they
start immediately and in parallel.Tip: to enable debug logging for the queue job, use--log-handler=odoo.addons.queue_job:DEBUG[1]It works with the threaded Odoo server too, although this way
of running Odoo is obviously not for production purposes.UsageTo use this module, you need to:Go toJob QueuemenuDevelopersDelaying jobsThe fast way to enqueue a job for a method is to usewith_delay()on a record
or model:defbutton_done(self):self.with_delay().print_confirmation_document(self.state)self.write({"state":"done"})returnTrueHere, the methodprint_confirmation_document()will be executed asynchronously
as a job.with_delay()can take several parameters to define more precisely how
the job is executed (priority, …).All the arguments passed to the method being delayed are stored in the job and
passed to the method when it is executed asynchronously, includingself, so
the current record is maintained during the job execution (warning: the context
is not kept).Dependencies can be expressed between jobs. To start a graph of jobs, usedelayable()on a record or model. The following is the equivalent ofwith_delay()but using the
long form:defbutton_done(self):delayable=self.delayable()delayable.print_confirmation_document(self.state)delayable.delay()self.write({"state":"done"})returnTrueMethods of Delayable objects return itself, so it can be used as a builder pattern,
which in some cases allow to build the jobs dynamically:defbutton_generate_simple_with_delayable(self):self.ensure_one()# Introduction of a delayable object, using a builder pattern# allowing to chain jobs or set properties. The delay() method# on the delayable object actually stores the delayable objects# in the queue_job table(self.delayable().generate_thumbnail((50,50)).set(priority=30).set(description=_("generate xxx")).delay())The simplest way to define a dependency is to use.on_done(job)on a Delayable:defbutton_chain_done(self):self.ensure_one()job1=self.browse(1).delayable().generate_thumbnail((50,50))job2=self.browse(1).delayable().generate_thumbnail((50,50))job3=self.browse(1).delayable().generate_thumbnail((50,50))# job 3 is executed when job 2 is done which is executed when job 1 is donejob1.on_done(job2.on_done(job3)).delay()Delayables can be chained to form more complex graphs using thechain()andgroup()primitives.
A chain represents a sequence of jobs to execute in order, a group represents
jobs which can be executed in parallel. Usingchain()has the same effect as
using several nestedon_done()but is more readable. Both can be combined to
form a graph, for instance we can group [A] of jobs, which blocks another group
[B] of jobs. When and only when all the jobs of the group [A] are executed, the
jobs of the group [B] are executed. The code would look like:fromodoo.addons.queue_job.delayimportgroup,chaindefbutton_done(self):group_a=group(self.delayable().method_foo(),self.delayable().method_bar())group_b=group(self.delayable().method_baz(1),self.delayable().method_baz(2))chain(group_a,group_b).delay()self.write({"state":"done"})returnTrueWhen a failure happens in a graph of jobs, the execution of the jobs that depend on the
failed job stops. They remain in a statewait_dependenciesuntil their “parent” job is
successful. This can happen in two ways: either the parent job retries and is successful
on a second try, either the parent job is manually “set to done” by a user. In these two
cases, the dependency is resolved and the graph will continue to be processed. Alternatively,
the failed job and all its dependent jobs can be canceled by a user. The other jobs of the
graph that do not depend on the failed job continue their execution in any case.Note:delay()must be called on the delayable, chain, or group which is at the top
of the graph. In the example above, if it was called ongroup_a, thengroup_bwould never be delayed (but a warning would be shown).Enqueing Job Optionspriority: default is 10, the closest it is to 0, the faster it will be
executedeta: Estimated Time of Arrival of the job. It will not be executed before this
date/timemax_retries: default is 5, maximum number of retries before giving up and set
the job state to ‘failed’. A value of 0 means infinite retries.description: human description of the job. If not set, description is computed
from the function doc or method namechannel: the complete name of the channel to use to process the function. If
specified it overrides the one defined on the functionidentity_key: key uniquely identifying the job, if specified and a job with
the same key has not yet been run, the new job will not be createdConfigure default options for jobsIn earlier versions, jobs could be configured using the@jobdecorator.
This is now obsolete, they can be configured using optionalqueue.job.functionandqueue.job.channelXML records.Example of channel:<recordid="channel_sale"model="queue.job.channel"><fieldname="name">sale</field><fieldname="parent_id"ref="queue_job.channel_root"/></record>Example of job function:<recordid="job_function_sale_order_action_done"model="queue.job.function"><fieldname="model_id"ref="sale.model_sale_order"/><fieldname="method">action_done</field><fieldname="channel_id"ref="channel_sale"/><fieldname="related_action"eval='{"func_name": "custom_related_action"}'/><fieldname="retry_pattern"eval="{1: 60, 2: 180, 3: 10, 5: 300}"/></record>The general form for thenameis:<model.name>.method.The channel, related action and retry pattern options are optional, they are
documented below.When writing modules, if 2+ modules add a job function or channel with the same
name (and parent for channels), they’ll be merged in the same record, even if
they have different xmlids. On uninstall, the merged record is deleted when all
the modules using it are uninstalled.Job function: modelIf the function is defined in an abstract model, you can not write<fieldname="model_id"ref="xml_id_of_the_abstract_model"</field>but you have to define a function for each model that inherits from the abstract model.Job function: channelThe channel where the job will be delayed. The default channel isroot.Job function: related actionTheRelated Actionappears as a button on the Job’s view.
The button will execute the defined action.The default one is to open the view of the record related to the job (form view
when there is a single record, list view for several records).
In many cases, the default related action is enough and doesn’t need
customization, but it can be customized by providing a dictionary on the job
function:{"enable":False,"func_name":"related_action_partner","kwargs":{"name":"Partner"},}enable: whenFalse, the button has no effect (default:True)func_name: name of the method onqueue.jobthat returns an actionkwargs: extra arguments to pass to the related action methodExample of related action code:classQueueJob(models.Model):_inherit='queue.job'defrelated_action_partner(self,name):self.ensure_one()model=self.model_namepartner=self.recordsaction={'name':name,'type':'ir.actions.act_window','res_model':model,'view_type':'form','view_mode':'form','res_id':partner.id,}returnactionJob function: retry patternWhen a job fails with a retryable error type, it is automatically
retried later. By default, the retry is always 10 minutes later.A retry pattern can be configured on the job function. What a pattern represents
is “from X tries, postpone to Y seconds”. It is expressed as a dictionary where
keys are tries and values are seconds to postpone as integers:{1:10,5:20,10:30,15:300,}Based on this configuration, we can tell that:5 first retries are postponed 10 seconds laterretries 5 to 10 postponed 20 seconds laterretries 10 to 15 postponed 30 seconds laterall subsequent retries postponed 5 minutes laterJob ContextThe context of the recordset of the job, or any recordset passed in arguments of
a job, is transferred to the job according to an allow-list.The default allow-list is empty for backward compatibility. The allow-list can
be customized inBase._job_prepare_context_before_enqueue_keys.Example:classBase(models.AbstractModel):_inherit="base"@api.modeldef_job_prepare_context_before_enqueue_keys(self):"""Keys to keep in context of stored jobs

        Empty by default for backward compatibility.
        """return("tz","lang","allowed_company_ids","force_company","active_test")Bypass jobs on running OdooWhen you are developing (ie: connector modules) you might want
to bypass the queue job and run your code immediately.To do so you can setTEST_QUEUE_JOB_NO_DELAY=1in your enviroment.Bypass jobs in testsWhen writing tests on job-related methods is always tricky to deal with
delayed recordsets. To make your testing life easier
you can settest_queue_job_no_delay=Truein the context.Tip: you can do this at test case level like this@classmethoddefsetUpClass(cls):super().setUpClass()cls.env=cls.env(context=dict(cls.env.context,test_queue_job_no_delay=True,# no jobs thanks))Then all your tests execute the job methods synchronously
without delaying any jobs.TestingAsserting enqueued jobsThe recommended way to test jobs, rather than running them directly and synchronously is to
split the tests in two parts:one test where the job is mocked (trap jobs withtrap_jobs()and the test
only verifies that the job has been delayed with the expected argumentsone test that only calls the method of the job synchronously, to validate the
proper behavior of this method onlyProceeding this way means that you can prove that jobs will be enqueued properly
at runtime, and it ensures your code does not have a different behavior in tests
and in production (because running your jobs synchronously may have a different
behavior as they are in the same transaction / in the middle of the method).
Additionally, it gives more control on the arguments you want to pass when
calling the job’s method (synchronously, this time, in the second type of
tests), and it makes tests smaller.The best way to run such assertions on the enqueued jobs is to useodoo.addons.queue_job.tests.common.trap_jobs().Inside this context manager, instead of being added in the database’s queue,
jobs are pushed in an in-memory list. The context manager then provides useful
helpers to verify that jobs have been enqueued with the expected arguments. It
even can run the jobs of its list synchronously! Details inodoo.addons.queue_job.tests.common.JobsTester.A very small example (more details intests/common.py):# codedefmy_job_method(self,name,count):self.write({"name":" ".join([name]*count)defmethod_to_test(self):count=self.env["other.model"].search_count([])self.with_delay(priority=15).my_job_method("Hi!",count=count)returncount# testsfromodoo.addons.queue_job.tests.commonimporttrap_jobs# first test only check the expected behavior of the method and the proper# enqueuing of jobsdeftest_method_to_test(self):withtrap_jobs()astrap:result=self.env["model"].method_to_test()expected_count=12trap.assert_jobs_count(1,only=self.env["model"].my_job_method)trap.assert_enqueued_job(self.env["model"].my_job_method,args=("Hi!",),kwargs=dict(count=expected_count),properties=dict(priority=15))self.assertEqual(result,expected_count)# second test to validate the behavior of the job unitarilydeftest_my_job_method(self):record=self.env["model"].browse(1)record.my_job_method("Hi!",count=12)self.assertEqual(record.name,"Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi!")If you prefer, you can still test the whole thing in a single test, by callingjobs_tester.perform_enqueued_jobs()in your test.deftest_method_to_test(self):withtrap_jobs()astrap:result=self.env["model"].method_to_test()expected_count=12trap.assert_jobs_count(1,only=self.env["model"].my_job_method)trap.assert_enqueued_job(self.env["model"].my_job_method,args=("Hi!",),kwargs=dict(count=expected_count),properties=dict(priority=15))self.assertEqual(result,expected_count)trap.perform_enqueued_jobs()record=self.env["model"].browse(1)record.my_job_method("Hi!",count=12)self.assertEqual(record.name,"Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi!")Execute jobs synchronously when running OdooWhen you are developing (ie: connector modules) you might want
to bypass the queue job and run your code immediately.To do so you can setTEST_QUEUE_JOB_NO_DELAY=1in your environment.WarningDo not do this in productionExecute jobs synchronously in testsYou should usetrap_jobs, really, but if for any reason you could not use it,
and still need to have job methods executed synchronously in your tests, you can
do so by settingtest_queue_job_no_delay=Truein the context.Tip: you can do this at test case level like this@classmethoddefsetUpClass(cls):super().setUpClass()cls.env=cls.env(context=dict(cls.env.context,test_queue_job_no_delay=True,# no jobs thanks))Then all your tests execute the job methods synchronously without delaying any
jobs.In tests you’ll have to mute the logger like:@mute_logger(‘odoo.addons.queue_job.models.base’)Notein graphs of jobs, thetest_queue_job_no_delaycontext key must be in at
least one job’s env of the graph for the whole graph to be executed synchronouslyTips and tricksIdempotency(https://www.restapitutorial.com/lessons/idempotency.html): The queue_job should be idempotent so they can be retried several times without impact on the data.The job should test at the very beginning its relevance: the moment the job will be executed is unknown by design. So the first task of a job should be to check if the related work is still relevant at the moment of the execution.PatternsThrough the time, two main patterns emerged:For data exposed to users, a model should store the data and the model should be the creator of the job. The job is kept hidden from the usersFor technical data, that are not exposed to the users, it is generally alright to create directly jobs with data passed as arguments to the job, without intermediary models.Known issues / RoadmapAfter creating a new database or installingqueue_jobon an
existing database, Odoo must be restarted for the runner to detect it.When Odoo shuts down normally, it waits for running jobs to finish.
However, when the Odoo server crashes or is otherwise force-stopped,
running jobs are interrupted while the runner has no chance to know
they have been aborted. In such situations, jobs may remain instartedorenqueuedstate after the Odoo server is halted.
Since the runner has no way to know if they are actually running or
not, and does not know for sure if it is safe to restart the jobs,
it does not attempt to restart them automatically. Such stale jobs
therefore fill the running queue and prevent other jobs to start.
You must therefore requeue them manually, either from the Jobs view,
or by running the following SQL statementbefore starting Odoo:updatequeue_jobsetstate='pending'wherestatein('started','enqueued')ChangelogNext[ADD] Run jobrunner as a worker process instead of a thread in the main
process (when running with –workers > 0)[REF]@joband@related_actiondeprecated, any method can be delayed,
and configured usingqueue.job.functionrecords[MIGRATION] from 13.0 branched at rev. e24ff4bBug TrackerBugs are tracked onGitHub Issues.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomedfeedback.Do not contact contributors directly about support or help with technical issues.CreditsAuthorsCamptocampACSONE SA/NVContributorsGuewen Baconnier <guewen.baconnier@camptocamp.com>Stéphane Bidoul <stephane.bidoul@acsone.eu>Matthieu Dietrich <matthieu.dietrich@camptocamp.com>Jos De Graeve <Jos.DeGraeve@apertoso.be>David Lefever <dl@taktik.be>Laurent Mignon <laurent.mignon@acsone.eu>Laetitia Gangloff <laetitia.gangloff@acsone.eu>Cédric Pigeon <cedric.pigeon@acsone.eu>Tatiana Deribina <tatiana.deribina@avoin.systems>Souheil Bejaoui <souheil.bejaoui@acsone.eu>Eric Antones <eantones@nuobit.com>Simone Orsi <simone.orsi@camptocamp.com>MaintainersThis module is maintained by the OCA.OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.Currentmaintainer:This module is part of theOCA/queueproject on GitHub.You are welcome to contribute. To learn how please visithttps://odoo-community.org/page/Contribute.