From 70c7cf653391238c2e27baf5811562a8c71f5c3a Mon Sep 17 00:00:00 2001
From: Florent Xicluna This addon adds an integrated Job Queue to Odoo.Job Queue
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-!! source digest: sha256:2b47ea2c9a7ab9a2b5ec87defad309384cfbc8d5640544a0233c4c7510c5bd4e
+!! source digest: sha256:457b3cda8ba43e54d1aa390b5e3be8291b6f58bb375d2fde4bc07b97634432df
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
Job Queue
transaction.
Example:
-from odoo import models, fields, api +from odoo import models, fields, api -class MyModel(models.Model): +class MyModel(models.Model): _name = 'my.model' - def my_method(self, a, k=None): + def my_method(self, a, k=None): _logger.info('executed with a: %s and k: %s', a, k) -class MyOtherModel(models.Model): +class MyOtherModel(models.Model): _name = 'my.other.model' - def button_do_stuff(self): + def button_do_stuff(self): self.env['my.model'].with_delay().my_method('a', k=2)
In the snippet of code above, when we call button_do_stuff, a job @@ -511,7 +511,7 @@
The fast way to enqueue a job for a method is to use with_delay() on a record or model:
-def button_done(self): +def button_done(self): self.with_delay().print_confirmation_document(self.state) self.write({"state": "done"}) return True @@ -527,7 +527,7 @@Delaying jobs
use delayable() on a record or model. The following is the equivalent of with_delay() but using the long form:-def button_done(self): +def button_done(self): delayable = self.delayable() delayable.print_confirmation_document(self.state) delayable.delay() @@ -538,7 +538,7 @@Delaying jobs
builder pattern, which in some cases allow to build the jobs dynamically:-def button_generate_simple_with_delayable(self): +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 @@ -555,7 +555,7 @@Delaying jobs
The simplest way to define a dependency is to use .on_done(job) on a Delayable:
-def button_chain_done(self): +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)) @@ -573,9 +573,9 @@Delaying jobs
executed, the jobs of the group [B] are executed. The code would look like:-from odoo.addons.queue_job.delay import group, chain +from odoo.addons.queue_job.delay import group, chain -def button_done(self): +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() @@ -601,7 +601,7 @@Delaying jobs
jobs, parallelize some task and get more specific errors. Usage is as follows:-def button_split_delayable(self): +def button_split_delayable(self): ( self # Can be a big recordset, let's say 1000 records .delayable() @@ -616,7 +616,7 @@Delaying jobs
set to True, the jobs will be chained, meaning that the next job will only start when the previous one is done:-def button_increment_var(self): +def button_increment_var(self): ( self .delayable() @@ -706,10 +706,10 @@Configure default options for job
Example of related action code:
-class QueueJob(models.Model): +class QueueJob(models.Model): _inherit = 'queue.job' - def related_action_partner(self, name): + def related_action_partner(self, name): self.ensure_one() model = self.model_name partner = self.records @@ -763,7 +763,7 @@Configure default options for job
Tip: you can do this at test case level like this
@classmethod -def setUpClass(cls): +def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, @@ -800,20 +800,20 @@Testing
A very small example (more details in tests/common.py):
# code -def my_job_method(self, name, count): +def my_job_method(self, name, count): self.write({"name": " ".join([name] * count) -def method_to_test(self): +def method_to_test(self): count = self.env["other.model"].search_count([]) self.with_delay(priority=15).my_job_method("Hi!", count=count) return count # tests -from odoo.addons.queue_job.tests.common import trap_jobs +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): +def test_method_to_test(self): with trap_jobs() as trap: result = self.env["model"].method_to_test() expected_count = 12 @@ -829,7 +829,7 @@Testing
# second test to validate the behavior of the job unitarily - def test_my_job_method(self): + def test_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!") @@ -837,7 +837,7 @@Testing
If you prefer, you can still test the whole thing in a single test, by calling jobs_tester.perform_enqueued_jobs() in your test.
-def test_method_to_test(self): +def test_method_to_test(self): with trap_jobs() as trap: result = self.env["model"].method_to_test() expected_count = 12 @@ -871,7 +871,7 @@Testing
Tip: you can do this at test case level like this
@classmethod -def setUpClass(cls): +def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, diff --git a/test_queue_job/__manifest__.py b/test_queue_job/__manifest__.py index 9424e31cd3..fff09e64d6 100644 --- a/test_queue_job/__manifest__.py +++ b/test_queue_job/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Queue Job Tests", - "version": "18.0.1.0.0", + "version": "18.0.1.0.1", "author": "Camptocamp,Odoo Community Association (OCA)", "license": "LGPL-3", "category": "Generic Modules",