Search 1.9 billion lines of Odoo code on GitHub

queue_job

Author: Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)
License: LGPL-3
Branch: 14.0-fix-queue_job-dependency
Repository: Tecnativa/queue
Dependencies: base_sparse_field, mail, and web
Languages: CSS (1, 0.0%), HTML (848, 12.7%), JavaScript (117, 1.7%), PO File (795, 11.9%), Python (3666, 54.8%), SVG (149, 2.2%), Sass (5, 0.1%), XML (615, 9.2%), and reStructuredText (496, 7.4%)
Other branches: 10.0, and 14.0-fix-queue_job-readme
Other repositories: 001101/queue, 2FM/queue, ACCOMODATA-BVBA/queue, AITIC/queue, Alfa-90/queue, App247/queue, Callino/queue, Change2improve/queue, ChinaShrimp/queue, Comunitea/queue, DamonLiu0814/queue, Dhirendrasinh-SerpentCS/queue, Dhrupesh/queue, Digital5-Odoo/queue, ERPLibre/queue, ForgeFlow/queue, GSLabIt/queue, Gabinete-Digital/queue, Ingeos/queue, Jarsa/queue, KolushovAlexandr/queue, Leuan12/queue, MadsackMediaStore/queue, MathBenTech/queue, MediaJockey/queue, Miller-Media/connector, MjAbuz/queue, NachoAlesLopez/queue, NeatNerdPrime/queue, Numigi/queue, OCA-MUK/queue, OCA/queue, OpenAT/queue, PrenorSistemas/queue, RRRoger/queue, RSC-TECHNOLOGY-CI/queue, RealSolutionsLux/queue, SanteLibre/queue, SequarSrl/queue, SeuMarco/queue, SuperMeepBoy/queue, Syci-Consulting/queue, TDu/queue, TRESCLOUD/queue, Tatider/queue, Trust-Code/queue, VCLS-org/queue, Vauxoo/queue, VisiionSolucionesTecnologicas/queue, Vkuruganti/queue, acsone/queue, acysos/odoo-addons, akretion/queue, algiopensource/queue, alsor62/queue, altanmur/queue, amendoncabh/queue, anggadjava/queue, anhvu-sg/queue, apachesep/queue, apertoso/queue, apetbiz/queue, apineux/queue, atchuthan/queue, aurestic/queue, bishalgit/queue, blueoceandevops/queue, bmya/queue, bobslee/queue, boskowski/odoo_oca_queue, brain-tec/queue, bvkl/queue, camptocamp/queue, celm1990/queue, cocox/queue, cogitoweb/queue, consultingerp/odoo-addons-2, contineolabs/queue, coopdevs/odoo-addons, coopiteasy/queue, csveda/queue, dave-prosee/queue-from-magnus, dev00007/queue, diggy128/queue, dndoanh/queue, dong-z/queue, druidoo-dev/queue, dynapps/queue, ecosoft-odoo/queue, ecosoft-odoo/rjc, eezee-it/queue, egbonrelu/queue, ehsu0407/queue, em230418/queue, emiproenterprise/queue, eneldoserrata/queue, eslammohamed13/odoo-addons, etobella/queue, exosoftware/oca-queue, factorlibre/queue, fengzoo2018/queue, florian-dacosta/queue, fr33co/queue, glovebx/queue, guewen/connector, guewen/queue, guwenfeng/queue, haroldtamo/queue, hibou-io/oca-queue, hinfo506/queue, hpatelserpentcs/queue, hq8399/queue, hustlercoder/queue, hzwzw/queue, igor-mitin/queue, ilyasbahridudak/queue, isoscl/queue, jsilvestar/queue, kevin070982/odoo-project-10.0, kingofsevens/queue, kit9/odoo-addons-1, leanhtuan1996/queue, lepistone/queue, libregob/queue, lideritjnma/queue, limingsichuan/queue, lincersolucoes/oca-queue, liweijie0812/queue, loftwah/queue, lonelyleaves/queue, lonelysun/queue, magnuscolors/queue, marcelsavegnago/odoo-addons, marcelsavegnago/queue, mauraco-group/queue, maykonAguiar/queue, mozaik-association/queue, nadiaafa/queue, nilshamerlinck/queue, nodoo/queue, omalbastin/queue, one2pret/queue, ontruck/queue, phatdo-novobi/queue, popcomputadores-desenv/queue, praxigento/oca-queue, profumang/queue, pscloud/queue, qtheuret/queue, raizs/queue, rlfss/queue, rodrig92/odoo-addons-1, royalline1/queue, sahil-navadiya/queue, samuel-fringeli/queue, sanube/queue, sc4you/queue, sewisoft/queue, shengyf1/queue, shepilov-vladislav/queue, simahawk/queue, sotsam/queue, steingabelgaard/queue, stephen144/queue, subteno-it/queue, sunflowerit/queue, sunshineLhj/queue, tajawal/queue, takinobori/oca-queue, tandrieuxx/queue, tarteo/queue, tegin/queue, telesoho/queue, thanhchatvn/queue, thanhchatvn/saas-addons, themreza/queue, thinkopensolutions/oca-queue, thoongnv/queue, tirma-sa/queue, traviswaelbro/queue, tuannguyen-itit/queue, unitek-solusi/OCA-queue, ursais/queue, versada/queue, vijay-erpharbor/queue, vinaybhawsar/queue, vjd8866/queue, vnsofthe/queue, vrenaville/queue, wahello/acysos-odoo-addons, westlyou/queue-1, wjshan/queue, wty0512/queue, x0rzkov/odoo-queue, xcgd/queue, xwh123807/queue, yangshii/queue, yucer/queue, zakiuu/queue, and zhaowenzhe/queue

<h1 class="title">Job Queue</h1> <p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Mature" src="https://img.shields.io/badge/maturity-Mature-brightgreen.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/queue/tree/14.0/queue_job"><img alt="OCA/queue" src="https://img.shields.io/badge/github-OCA%2Fqueue-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/queue-14-0/queue-14-0-queue_job"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/230/14.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p> <p>This addon adds an integrated Job Queue to Odoo.</p> <p>It allows to postpone method calls executed asynchronously.</p> <p>Jobs are executed in the background by a <code>Jobrunner</code>, in their own transaction.</p> <p>Example:</p> <pre> <code lang="python">from odoo import models, fields, api class MyModel(models.Model): _name = 'my.model' def my_method(self, a, k=None): _logger.info('executed with a: %s and k: %s', a, k) class MyOtherModel(models.Model): _name = 'my.other.model' def button_do_stuff(self): self.env['my.model'].with_delay().my_method('a', k=2)</code> </pre> <p>In the snippet of code above, when we call <code>button_do_stuff</code>, a job <strong>capturing the method and arguments</strong> will 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.</p> <p>Features:</p> <ul class="simple"> <li>Views for jobs, jobs are stored in PostgreSQL</li> <li>Jobrunner: execute the jobs, highly efficient thanks to PostgreSQL's NOTIFY</li> <li>Channels: 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.</li> <li>Retries: Ability to retry jobs by raising a type of exception</li> <li>Retry Pattern: the 3 first tries, retry after 10 seconds, the 5 next tries, retry after 1 minutes, ...</li> <li>Job properties: priorities, estimated time of arrival (ETA), custom description, number of retries</li> <li>Related Actions: link an action on the job view, such as open the record concerned by the job</li> </ul> <p><strong>Table of contents</strong></p> <div class="contents local topic" id="contents"> <ul class="simple"> <li><a class="reference internal" href="#installation" id="id3">Installation</a></li> <li><a class="reference internal" href="#configuration" id="id4">Configuration</a></li> <li><a class="reference internal" href="#usage" id="id5">Usage</a><ul> <li><a class="reference internal" href="#developers" id="id6">Developers</a><ul> <li><a class="reference internal" href="#delaying-jobs" id="id7">Delaying jobs</a></li> <li><a class="reference internal" href="#enqueing-job-options" id="id8">Enqueing Job Options</a></li> <li><a class="reference internal" href="#configure-default-options-for-jobs" id="id9">Configure default options for jobs</a></li> <li><a class="reference internal" href="#testing" id="id10">Testing</a></li> <li><a class="reference internal" href="#tips-and-tricks" id="id11">Tips and tricks</a></li> <li><a class="reference internal" href="#patterns" id="id12">Patterns</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#known-issues-roadmap" id="id13">Known issues / Roadmap</a></li> <li><a class="reference internal" href="#changelog" id="id14">Changelog</a></li> <li><a class="reference internal" href="#bug-tracker" id="id15">Bug Tracker</a></li> <li><a class="reference internal" href="#credits" id="id16">Credits</a><ul> <li><a class="reference internal" href="#authors" id="id17">Authors</a></li> <li><a class="reference internal" href="#contributors" id="id18">Contributors</a></li> <li><a class="reference internal" href="#maintainers" id="id19">Maintainers</a></li> </ul> </li> </ul> </div> <a name="installation"></a> <h2><a class="toc-backref" href="#id3">Installation</a></h2> <p>Be sure to have the <code>requests</code> library.</p> <a name="configuration"></a> <h2><a class="toc-backref" href="#id4">Configuration</a></h2> <ul class="simple"> <li>Using environment variables and command line:<ul> <li>Adjust environment variables (optional):<ul> <li><code>ODOO_QUEUE_JOB_CHANNELS=root:4</code> or any other channels configuration. The default is <code>root:1</code></li> <li>if <code>xmlrpc_port</code> is not set: <code>ODOO_QUEUE_JOB_PORT=8069</code></li> </ul> </li> <li>Start Odoo with <code>--load=web,queue_job</code> and <code>--workers</code> greater than 1. <a class="footnote-reference" href="#id2" id="id1">[1]</a></li> </ul> </li> <li>Using the Odoo configuration file:</li> </ul> <pre> <code lang="ini">[options] (...) workers = 6 server_wide_modules = web,queue_job,base_sparse_field (...) [queue_job] channels = root:2</code> </pre> <ul class="simple"> <li>Confirm the runner is starting correctly by checking the odoo log file:</li> </ul> <pre class="code"> <code class="code">...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 &lt;dbname&gt; ...INFO...queue_job.jobrunner.runner: database connections ready</code> </pre> <ul class="simple"> <li>Create jobs (eg using <code>base_import_async</code>) and observe they start immediately and in parallel.</li> <li>Tip: to enable debug logging for the queue job, use <code>--log-handler=odoo.addons.queue_job:DEBUG</code></li> </ul> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>It works with the threaded Odoo server too, although this way of running Odoo is obviously not for production purposes.</td></tr> </tbody> </table> <a name="usage"></a> <h2><a class="toc-backref" href="#id5">Usage</a></h2> <p>To use this module, you need to:</p> <ol class="arabic simple"> <li>Go to <code>Job Queue</code> menu</li> </ol> <a name="developers"></a> <h3><a class="toc-backref" href="#id6">Developers</a></h3> <a name="delaying-jobs"></a> <h4><a class="toc-backref" href="#id7">Delaying jobs</a></h4> <p>The fast way to enqueue a job for a method is to use <code>with_delay()</code> on a record or model:</p> <pre> <code lang="python">def button_done(self): self.with_delay().print_confirmation_document(self.state) self.write({&quot;state&quot;: &quot;done&quot;}) return True</code> </pre> <p>Here, the method <code>print_confirmation_document()</code> will be executed asynchronously as a job. <code>with_delay()</code> can take several parameters to define more precisely how the job is executed (priority, ...).</p> <p>All the arguments passed to the method being delayed are stored in the job and passed to the method when it is executed asynchronously, including <code>self</code>, so the current record is maintained during the job execution (warning: the context is not kept).</p> <p>Dependencies can be expressed between jobs. To start a graph of jobs, use <code>delayable()</code> on a record or model. The following is the equivalent of <code>with_delay()</code> but using the long form:</p> <pre> <code lang="python">def button_done(self): delayable = self.delayable() delayable.print_confirmation_document(self.state) delayable.delay() self.write({&quot;state&quot;: &quot;done&quot;}) return True</code> </pre> <p>Methods of Delayable objects return itself, so it can be used as a builder pattern, which in some cases allow to build the jobs dynamically:</p> <pre> <code lang="python">def button_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=_(&quot;generate xxx&quot;)) .delay() )</code> </pre> <p>The simplest way to define a dependency is to use <code>.on_done(job)</code> on a Delayable:</p> <pre> <code lang="python">def button_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 done job1.on_done(job2.on_done(job3)).delay()</code> </pre> <p>Delayables can be chained to form more complex graphs using the <code>chain()</code> and <code>group()</code> primitives. A chain represents a sequence of jobs to execute in order, a group represents jobs which can be executed in parallel. Using <code>chain()</code> has the same effect as using several nested <code>on_done()</code> 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:</p> <pre> <code lang="python">from odoo.addons.queue_job.delay import group, chain def button_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({&quot;state&quot;: &quot;done&quot;}) return True</code> </pre> <p>When a failure happens in a graph of jobs, the execution of the jobs that depend on the failed job stops. They remain in a state <code>wait_dependencies</code> until their &quot;parent&quot; 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 &quot;set to done&quot; 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.</p> <p>Note: <code>delay()</code> 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 on <code>group_a</code>, then <code>group_b</code> would never be delayed (but a warning would be shown).</p> <a name="enqueing-job-options"></a> <h4><a class="toc-backref" href="#id8">Enqueing Job Options</a></h4> <ul class="simple"> <li>priority: default is 10, the closest it is to 0, the faster it will be executed</li> <li>eta: Estimated Time of Arrival of the job. It will not be executed before this date/time</li> <li>max_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.</li> <li>description: human description of the job. If not set, description is computed from the function doc or method name</li> <li>channel: the complete name of the channel to use to process the function. If specified it overrides the one defined on the function</li> <li>identity_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 created</li> </ul> <a name="configure-default-options-for-jobs"></a> <h4><a class="toc-backref" href="#id9">Configure default options for jobs</a></h4> <p>In earlier versions, jobs could be configured using the <code>&#64;job</code> decorator. This is now obsolete, they can be configured using optional <code>queue.job.function</code> and <code>queue.job.channel</code> XML records.</p> <p>Example of channel:</p> <pre> <code lang="XML">&lt;record id=&quot;channel_sale&quot; model=&quot;queue.job.channel&quot;&gt; &lt;field name=&quot;name&quot;&gt;sale&lt;/field&gt; &lt;field name=&quot;parent_id&quot; ref=&quot;queue_job.channel_root&quot; /&gt; &lt;/record&gt;</code> </pre> <p>Example of job function:</p> <pre> <code lang="XML">&lt;record id=&quot;job_function_sale_order_action_done&quot; model=&quot;queue.job.function&quot;&gt; &lt;field name=&quot;model_id&quot; ref=&quot;sale.model_sale_order&quot; /&gt; &lt;field name=&quot;method&quot;&gt;action_done&lt;/field&gt; &lt;field name=&quot;channel_id&quot; ref=&quot;channel_sale&quot; /&gt; &lt;field name=&quot;related_action&quot; eval='{&quot;func_name&quot;: &quot;custom_related_action&quot;}' /&gt; &lt;field name=&quot;retry_pattern&quot; eval=&quot;{1: 60, 2: 180, 3: 10, 5: 300}&quot; /&gt; &lt;/record&gt;</code> </pre> <p>The general form for the <code>name</code> is: <code>&lt;model.name&gt;.method</code>.</p> <p>The channel, related action and retry pattern options are optional, they are documented below.</p> <p>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.</p> <p><strong>Job function: model</strong></p> <p>If the function is defined in an abstract model, you can not write <code>&lt;field name=&quot;model_id&quot; ref=&quot;xml_id_of_the_abstract_model&quot;&lt;/field&gt;</code> but you have to define a function for each model that inherits from the abstract model.</p> <p><strong>Job function: channel</strong></p> <p>The channel where the job will be delayed. The default channel is <code>root</code>.</p> <p><strong>Job function: related action</strong></p> <p>The <em>Related Action</em> appears as a button on the Job's view. The button will execute the defined action.</p> <p>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:</p> <pre> <code lang="python">{ &quot;enable&quot;: False, &quot;func_name&quot;: &quot;related_action_partner&quot;, &quot;kwargs&quot;: {&quot;name&quot;: &quot;Partner&quot;}, }</code> </pre> <ul class="simple"> <li><code>enable</code>: when <code>False</code>, the button has no effect (default: <code>True</code>)</li> <li><code>func_name</code>: name of the method on <code>queue.job</code> that returns an action</li> <li><code>kwargs</code>: extra arguments to pass to the related action method</li> </ul> <p>Example of related action code:</p> <pre> <code lang="python">class QueueJob(models.Model): _inherit = 'queue.job' def related_action_partner(self, name): self.ensure_one() model = self.model_name partner = self.records action = { 'name': name, 'type': 'ir.actions.act_window', 'res_model': model, 'view_type': 'form', 'view_mode': 'form', 'res_id': partner.id, } return action</code> </pre> <p><strong>Job function: retry pattern</strong></p> <p>When a job fails with a retryable error type, it is automatically retried later. By default, the retry is always 10 minutes later.</p> <p>A retry pattern can be configured on the job function. What a pattern represents is &quot;from X tries, postpone to Y seconds&quot;. It is expressed as a dictionary where keys are tries and values are seconds to postpone as integers:</p> <pre> <code lang="python">{ 1: 10, 5: 20, 10: 30, 15: 300, }</code> </pre> <p>Based on this configuration, we can tell that:</p> <ul class="simple"> <li>5 first retries are postponed 10 seconds later</li> <li>retries 5 to 10 postponed 20 seconds later</li> <li>retries 10 to 15 postponed 30 seconds later</li> <li>all subsequent retries postponed 5 minutes later</li> </ul> <p><strong>Job Context</strong></p> <p>The 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.</p> <p>The default allow-list is empty for backward compatibility. The allow-list can be customized in <code>Base._job_prepare_context_before_enqueue_keys</code>.</p> <p>Example:</p> <pre> <code lang="python">class Base(models.AbstractModel): _inherit = &quot;base&quot; &#64;api.model def _job_prepare_context_before_enqueue_keys(self): &quot;&quot;&quot;Keys to keep in context of stored jobs Empty by default for backward compatibility. &quot;&quot;&quot; return (&quot;tz&quot;, &quot;lang&quot;, &quot;allowed_company_ids&quot;, &quot;force_company&quot;, &quot;active_test&quot;)</code> </pre> <p><strong>Bypass jobs on running Odoo</strong></p> <p>When you are developing (ie: connector modules) you might want to bypass the queue job and run your code immediately.</p> <p>To do so you can set <cite>TEST_QUEUE_JOB_NO_DELAY=1</cite> in your enviroment.</p> <p><strong>Bypass jobs in tests</strong></p> <p>When writing tests on job-related methods is always tricky to deal with delayed recordsets. To make your testing life easier you can set <cite>test_queue_job_no_delay=True</cite> in the context.</p> <p>Tip: you can do this at test case level like this</p> <pre> <code lang="python">&#64;classmethod def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, test_queue_job_no_delay=True, # no jobs thanks ))</code> </pre> <p>Then all your tests execute the job methods synchronously without delaying any jobs.</p> <a name="testing"></a> <h4><a class="toc-backref" href="#id10">Testing</a></h4> <p><strong>Asserting enqueued jobs</strong></p> <p>The recommended way to test jobs, rather than running them directly and synchronously is to split the tests in two parts:</p> <blockquote> <ul class="simple"> <li>one test where the job is mocked (trap jobs with <code>trap_jobs()</code> and the test only verifies that the job has been delayed with the expected arguments</li> <li>one test that only calls the method of the job synchronously, to validate the proper behavior of this method only</li> </ul> </blockquote> <p>Proceeding 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.</p> <p>The best way to run such assertions on the enqueued jobs is to use <code>odoo.addons.queue_job.tests.common.trap_jobs()</code>.</p> <p>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 in <code>odoo.addons.queue_job.tests.common.JobsTester</code>.</p> <p>A very small example (more details in <code>tests/common.py</code>):</p> <pre> <code lang="python"># code def my_job_method(self, name, count): self.write({&quot;name&quot;: &quot; &quot;.join([name] * count) def method_to_test(self): count = self.env[&quot;other.model&quot;].search_count([]) self.with_delay(priority=15).my_job_method(&quot;Hi!&quot;, count=count) return count # tests from odoo.addons.queue_job.tests.common import trap_jobs # first test only check the expected behavior of the method and the proper # enqueuing of jobs def test_method_to_test(self): with trap_jobs() as trap: result = self.env[&quot;model&quot;].method_to_test() expected_count = 12 trap.assert_jobs_count(1, only=self.env[&quot;model&quot;].my_job_method) trap.assert_enqueued_job( self.env[&quot;model&quot;].my_job_method, args=(&quot;Hi!&quot;,), kwargs=dict(count=expected_count), properties=dict(priority=15) ) self.assertEqual(result, expected_count) # second test to validate the behavior of the job unitarily def test_my_job_method(self): record = self.env[&quot;model&quot;].browse(1) record.my_job_method(&quot;Hi!&quot;, count=12) self.assertEqual(record.name, &quot;Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi!&quot;)</code> </pre> <p>If you prefer, you can still test the whole thing in a single test, by calling <code>jobs_tester.perform_enqueued_jobs()</code> in your test.</p> <pre> <code lang="python">def test_method_to_test(self): with trap_jobs() as trap: result = self.env[&quot;model&quot;].method_to_test() expected_count = 12 trap.assert_jobs_count(1, only=self.env[&quot;model&quot;].my_job_method) trap.assert_enqueued_job( self.env[&quot;model&quot;].my_job_method, args=(&quot;Hi!&quot;,), kwargs=dict(count=expected_count), properties=dict(priority=15) ) self.assertEqual(result, expected_count) trap.perform_enqueued_jobs() record = self.env[&quot;model&quot;].browse(1) record.my_job_method(&quot;Hi!&quot;, count=12) self.assertEqual(record.name, &quot;Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi!&quot;)</code> </pre> <p><strong>Execute jobs synchronously when running Odoo</strong></p> <p>When you are developing (ie: connector modules) you might want to bypass the queue job and run your code immediately.</p> <p>To do so you can set <code>TEST_QUEUE_JOB_NO_DELAY=1</code> in your environment.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Do not do this in production</p> </div> <p><strong>Execute jobs synchronously in tests</strong></p> <p>You should use <code>trap_jobs</code>, 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 setting <code>test_queue_job_no_delay=True</code> in the context.</p> <p>Tip: you can do this at test case level like this</p> <pre> <code lang="python">&#64;classmethod def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, test_queue_job_no_delay=True, # no jobs thanks ))</code> </pre> <p>Then all your tests execute the job methods synchronously without delaying any jobs.</p> <p>In tests you'll have to mute the logger like:</p> <blockquote> &#64;mute_logger('odoo.addons.queue_job.models.base')</blockquote> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">in graphs of jobs, the <code>test_queue_job_no_delay</code> context key must be in at least one job's env of the graph for the whole graph to be executed synchronously</p> </div> <a name="tips-and-tricks"></a> <h4><a class="toc-backref" href="#id11">Tips and tricks</a></h4> <ul class="simple"> <li><strong>Idempotency</strong> (<a class="reference external" href="https://www.restapitutorial.com/lessons/idempotency.html">https://www.restapitutorial.com/lessons/idempotency.html</a>): The queue_job should be idempotent so they can be retried several times without impact on the data.</li> <li><strong>The job should test at the very beginning its relevance</strong>: 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.</li> </ul> <a name="patterns"></a> <h4><a class="toc-backref" href="#id12">Patterns</a></h4> <p>Through the time, two main patterns emerged:</p> <ol class="arabic simple"> <li>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 users</li> <li>For 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.</li> </ol> <a name="known-issues-roadmap"></a> <h2><a class="toc-backref" href="#id13">Known issues / Roadmap</a></h2> <ul class="simple"> <li>After creating a new database or installing <code>queue_job</code> on an existing database, Odoo must be restarted for the runner to detect it.</li> <li>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 in <code>started</code> or <code>enqueued</code> state 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 statement <em>before starting Odoo</em>:</li> </ul> <pre> <code lang="sql">update queue_job set state='pending' where state in ('started', 'enqueued')</code> </pre> <a name="changelog"></a> <h2 class="with-subtitle"><a class="toc-backref" href="#id14">Changelog</a></h2> <h2 class="section-subtitle" id="next"><span class="section-subtitle">Next</span></h2> <ul class="simple"> <li>[ADD] Run jobrunner as a worker process instead of a thread in the main process (when running with --workers &gt; 0)</li> <li>[REF] <code>&#64;job</code> and <code>&#64;related_action</code> deprecated, any method can be delayed, and configured using <code>queue.job.function</code> records</li> <li>[MIGRATION] from 13.0 branched at rev. e24ff4b</li> </ul> <a name="bug-tracker"></a> <h2><a class="toc-backref" href="#id15">Bug Tracker</a></h2> <p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/queue/issues">GitHub Issues</a>. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed <a class="reference external" href="https://github.com/OCA/queue/issues/new?body=module:%20queue_job%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p> <p>Do not contact contributors directly about support or help with technical issues.</p> <a name="credits"></a> <h2><a class="toc-backref" href="#id16">Credits</a></h2> <a name="authors"></a> <h3><a class="toc-backref" href="#id17">Authors</a></h3> <ul class="simple"> <li>Camptocamp</li> <li>ACSONE SA/NV</li> </ul> <a name="contributors"></a> <h3><a class="toc-backref" href="#id18">Contributors</a></h3> <ul class="simple"> <li>Guewen Baconnier &lt;<a class="reference external" href="mailto:guewen.baconnier&#64;camptocamp.com">guewen.baconnier&#64;camptocamp.com</a>&gt;</li> <li>Stéphane Bidoul &lt;<a class="reference external" href="mailto:stephane.bidoul&#64;acsone.eu">stephane.bidoul&#64;acsone.eu</a>&gt;</li> <li>Matthieu Dietrich &lt;<a class="reference external" href="mailto:matthieu.dietrich&#64;camptocamp.com">matthieu.dietrich&#64;camptocamp.com</a>&gt;</li> <li>Jos De Graeve &lt;<a class="reference external" href="mailto:Jos.DeGraeve&#64;apertoso.be">Jos.DeGraeve&#64;apertoso.be</a>&gt;</li> <li>David Lefever &lt;<a class="reference external" href="mailto:dl&#64;taktik.be">dl&#64;taktik.be</a>&gt;</li> <li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li> <li>Laetitia Gangloff &lt;<a class="reference external" href="mailto:laetitia.gangloff&#64;acsone.eu">laetitia.gangloff&#64;acsone.eu</a>&gt;</li> <li>Cédric Pigeon &lt;<a class="reference external" href="mailto:cedric.pigeon&#64;acsone.eu">cedric.pigeon&#64;acsone.eu</a>&gt;</li> <li>Tatiana Deribina &lt;<a class="reference external" href="mailto:tatiana.deribina&#64;avoin.systems">tatiana.deribina&#64;avoin.systems</a>&gt;</li> <li>Souheil Bejaoui &lt;<a class="reference external" href="mailto:souheil.bejaoui&#64;acsone.eu">souheil.bejaoui&#64;acsone.eu</a>&gt;</li> <li>Eric Antones &lt;<a class="reference external" href="mailto:eantones&#64;nuobit.com">eantones&#64;nuobit.com</a>&gt;</li> <li>Simone Orsi &lt;<a class="reference external" href="mailto:simone.orsi&#64;camptocamp.com">simone.orsi&#64;camptocamp.com</a>&gt;</li> </ul> <a name="maintainers"></a> <h3><a class="toc-backref" href="#id19">Maintainers</a></h3> <p>This module is maintained by the OCA.</p> <a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a> <p>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.</p> <p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p> <p><a class="reference external" href="https://github.com/guewen"><img alt="guewen" src="https://github.com/guewen.png?size=40px" /></a></p> <p>This module is part of the <a class="reference external" href="https://github.com/OCA/queue/tree/14.0/queue_job">OCA/queue</a> project on GitHub.</p> <p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>