### Related Action Method Example Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Example of a 'related_action_partner' method within a 'QueueJob' class. This method defines the action to be performed when a job's related action is triggered. ```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 ``` -------------------------------- ### Queue Job Runner Startup Log Messages Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/CONFIGURE.md Check these log messages to confirm that the queue job runner is starting correctly and initializing database connections. ```log ...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 ...INFO...queue_job.jobrunner.runner: database connections ready ``` -------------------------------- ### Queue Job Runner Log Messages Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Observe these log messages to confirm the queue job runner is starting correctly. ```log ...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 ...INFO...queue_job.jobrunner.runner: database connections ready ``` -------------------------------- ### Queue Job Configuration via Environment Variables Source: https://github.com/oca/queue/blob/18.0/queue_job/static/description/index.html Set environment variables to customize queue job channels, port, scheme, host, and authentication. Ensure Odoo is started with appropriate workers. ```bash export ODOO_QUEUE_JOB_CHANNELS=root:4 export ODOO_QUEUE_JOB_PORT=8069 export ODOO_QUEUE_JOB_SCHEME=https export ODOO_QUEUE_JOB_HOST=load-balancer export ODOO_QUEUE_JOB_HTTP_AUTH_USER=jobrunner export ODOO_QUEUE_JOB_HTTP_AUTH_PASSWORD=s3cr3t odoo --load=web,queue_job --workers=2 ``` -------------------------------- ### Split Delayable Job with Chaining Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Splits a delayable job into multiple jobs that execute sequentially. The next job only starts after the previous one is completed. ```python def button_increment_var(self): ( self .delayable() .increment_counter() .split(1, chain=True) # Will exceute the jobs one after the other .delay() ) ``` -------------------------------- ### Customize Related Action for Queue Jobs Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Provide a dictionary to customize the 'Related Action' button on the Job's view. This example shows how to disable it or specify a custom method. ```python { "enable": False, "func_name": "related_action_partner", "kwargs": {"name": "Partner"}, } ``` -------------------------------- ### Split Jobs with Chaining Enabled Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md When `split()` is called with `chain=True`, the resulting jobs are executed sequentially, meaning each job only starts after the previous one has successfully completed. ```python def button_increment_var(self): ( self .delayable() .increment_counter() .split(1, chain=True) # Will exceute the jobs one after the other .delay() ) ``` -------------------------------- ### Define Job Dependencies with on_done Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Use `.on_done(job)` on a `Delayable` object to specify that a subsequent job should only start after the preceding one is completed. This allows for sequential job execution. ```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() ``` -------------------------------- ### Build Jobs Dynamically with Delayable Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Demonstrates using the builder pattern with `delayable()` to chain jobs or set properties before enqueuing. The `delay()` method finalizes and stores the jobs. ```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=_("generate xxx")) .delay() ) ``` -------------------------------- ### Schedule Multiple Jobs within a Batch Source: https://github.com/oca/queue/blob/18.0/queue_job_batch/static/description/index.html Demonstrates how to create a new job batch and schedule multiple calls to a model's method within that batch using `with_delay()`. This allows for efficient processing of numerous similar tasks. ```python class MyOtherModel(models.Model): _name = 'my.other.model' @api.multi def button_do_stuff(self): batch = self.env['queue.job.batch'].get_new_batch('Group') model = self.env['my.model'].with_context(job_batch=batch) for i in range(1, 100): model.with_delay().my_method('a', k=i) ``` -------------------------------- ### Enable Queue Job Debug Logging Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/CONFIGURE.md Use this command-line argument to enable debug logging for the queue job module, which can be helpful for troubleshooting. ```bash --log-handler=odoo.addons.queue_job:DEBUG ``` -------------------------------- ### Create and Schedule Batched Jobs Source: https://github.com/oca/queue/blob/18.0/queue_job_batch/README.rst This snippet demonstrates how to create a new job batch and schedule multiple jobs to be executed within that batch. Jobs are deferred using `with_delay()` and associated with the batch via `with_context(job_batch=batch)`. ```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' @api.multi def button_do_stuff(self): batch = self.env['queue.job.batch'].get_new_batch('Group') model = self.env['my.model'].with_context(job_batch=batch) for i in range(1, 100): model.with_delay().my_method('a', k=i) ``` -------------------------------- ### Define a Queue Job Channel Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Use this XML record to define a channel for queue jobs. Ensure the parent channel is correctly referenced. ```xml sale ``` -------------------------------- ### Configure Queue Job with Odoo Configuration File Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/CONFIGURE.md Use the [options] and [queue_job] sections in the Odoo configuration file to set parameters like workers, server-wide modules, channels, scheme, host, port, and HTTP authentication. ```ini [options] (...) workers = 6 server_wide_modules = web,queue_job (...) [queue_job] channels = root:2 scheme = https host = load-balancer port = 443 http_auth_user = jobrunner http_auth_password = s3cr3t ``` -------------------------------- ### Define a Model with a Method for Queue Jobs Source: https://github.com/oca/queue/blob/18.0/queue_job_batch/static/description/index.html Defines a sample Odoo model and a method that can be delayed and executed by the queue system. This method logs its execution with provided arguments. ```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) ``` -------------------------------- ### Using `chain()` and `group()` for Job Graphs Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Combine `chain()` for sequential execution and `group()` for parallel execution to form complex job dependency graphs. `chain()` provides a more readable alternative to nested `on_done()`. ```python from odoo.addons.queue_job.delay import group, chain ``` -------------------------------- ### Create Job Graphs with group() and chain() Source: https://github.com/oca/queue/blob/18.0/queue_job/static/description/index.html Combine group() for parallel execution and chain() for sequential execution to build complex job graphs. The delay() method must be called on the top-level group or chain to initiate the execution. ```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({"state": "done"}) return True ``` -------------------------------- ### Odoo Configuration File Settings Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Configure queue job settings like channels, scheme, host, port, and authentication using the Odoo configuration file. ```ini [options] (...) workers = 6 server_wide_modules = web,queue_job (...) [queue_job] channels = root:2 scheme = https host = load-balancer port = 443 http_auth_user = jobrunner http_auth_password = s3cr3t ``` -------------------------------- ### Define a Queue Job Function Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Configure a specific job function with its model, method, channel, related action, and retry pattern using this XML record. ```xml action_done ``` -------------------------------- ### Queue Job Function Configuration Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Configures a specific job function with its associated model, method, channel, and retry pattern. This allows for fine-grained control over job execution. ```XML action_done ``` -------------------------------- ### Implement Custom Related Action Method Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Define a method within the 'queue.job' model to handle custom 'Related Actions'. This method should return an action dictionary. ```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 ``` -------------------------------- ### Queue Job Channel Configuration Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Defines a custom channel for queue jobs. This allows for organizing and routing jobs to specific processing queues. ```XML sale ``` -------------------------------- ### Create Job Graphs with Chain and Group Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Utilize `chain()` for sequential job execution and `group()` for parallel execution. These can be combined to form complex job graphs. The `delay()` method must be called on the top-level object of the graph. ```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({"state": "done"}) return True ``` -------------------------------- ### Configure Retry Pattern for Queue Jobs Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Define a dictionary to specify a custom retry pattern for jobs that fail with a retryable error. Keys represent the number of retries, and values are the seconds to postpone. ```python { 1: 10, 5: 20, 10: 30, 15: 300, } ``` -------------------------------- ### Bypass Queue Jobs in Development Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Set the environment variable QUEUE_JOB__NO_DELAY to 1 to run jobs immediately instead of queuing them. This is useful during development. ```bash export QUEUE_JOB__NO_DELAY=1 ``` -------------------------------- ### Enqueue a Job using Delayable (Long Form) Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md The `delayable()` method provides a more verbose way to enqueue jobs, allowing for builder-pattern usage. Call `delay()` to actually store the jobs in the queue. ```python def button_done(self): delayable = self.delayable() delayable.print_confirmation_document(self.state) delayable.delay() self.write({"state": "done"}) return True ``` -------------------------------- ### Delaying a Job with `with_delay()` Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Enqueue a job for a method asynchronously using `with_delay()` on a record or model. The method's arguments, including `self`, are stored and passed when the job executes. ```python def button_done(self): self.with_delay().print_confirmation_document(self.state) self.write({"state": "done"}) return True ``` -------------------------------- ### Job Retry Pattern Configuration Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Configure a retry pattern for jobs that fail with retryable errors. The dictionary maps the number of retries to the seconds to postpone the next retry. ```python { 1: 10, 5: 20, 10: 30, 15: 300, } ``` -------------------------------- ### Trap Jobs for Testing Enqueuing Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Use 'trap_jobs()' to mock jobs and verify they are enqueued with the expected arguments. This separates testing job enqueuing from testing job logic. ```python 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["model"].method_to_test() expected_count = 12 ``` -------------------------------- ### Enqueue a Job with Delay Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Use `with_delay()` to execute a method asynchronously. Arguments passed to the method are stored and passed to the job execution. The current record (`self`) is maintained, but the context is not. ```python def button_done(self): self.with_delay().print_confirmation_document(self.state) self.write({"state": "done"}) return True ``` -------------------------------- ### Bypass Queue Jobs in Tests Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Add 'queue_job__no_delay=True' to the environment context within your test cases to execute job methods synchronously without delay. ```python @classmethod def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, queue_job__no_delay=True, # no jobs thanks )) ``` -------------------------------- ### Test Enqueued Jobs and Perform Execution Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Combine job enqueuing assertions with immediate execution of enqueued jobs within a single test using `trap_jobs` and `perform_enqueued_jobs`. ```python def test_method_to_test(self): with trap_jobs() as trap: result = self.env["model"].method_to_test() expected_count = 12 trap.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!") ``` -------------------------------- ### Split a Job into Multiple Jobs Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md The `split()` method allows dividing a single job into multiple smaller jobs, useful for avoiding long-running tasks or enabling parallel processing. The number passed to `split()` determines the size of each chunk. ```python def button_split_delayable(self): ( self # Can be a big recordset, let's say 1000 records .delayable() .generate_thumbnail((50, 50)) .set(priority=30) .set(description=_("generate xxx")) .split(50) # Split the job in 20 jobs of 50 records each .delay() ) ``` -------------------------------- ### Customize Related Action Source: https://github.com/oca/queue/blob/18.0/queue_job/static/description/index.html Provide a dictionary to customize the 'related_action' for a job function. 'enable' controls visibility, 'func_name' specifies the method, and 'kwargs' pass additional arguments. ```json { "enable": False, "func_name": "related_action_partner", "kwargs": {"name": "Partner"} } ``` -------------------------------- ### Test Enqueued Jobs with trap_jobs Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/USAGE.md Use `trap_jobs` to assert the correct enqueuing of jobs with expected arguments and properties. This allows for testing job enqueuing separately from job execution. ```python # code def my_job_method(self, name, count): self.write({"name": " ".join([name] * count)}) 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 # 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["model"].method_to_test() expected_count = 12 trap.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) ``` ```python # second test to validate the behavior of the job unitarily 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!") ``` -------------------------------- ### Bypass Jobs in Tests with Context Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Set 'queue_job__no_delay=True' in the context to bypass queue jobs and run them synchronously during testing. This is useful for simplifying test logic. ```python @classmethod def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict( cls.env.context, queue_job__no_delay=True, # no jobs thanks )) ``` -------------------------------- ### Bypass Jobs in Tests Source: https://github.com/oca/queue/blob/18.0/queue_job/static/description/index.html Set queue_job__no_delay=True in the context to execute job methods synchronously without delaying any jobs during testing. ```python self.env = self.env(context=dict( cls.env.context, queue_job__no_delay=True, # no jobs thanks )) ``` -------------------------------- ### Delayable Job Graph Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Defines a graph of delayable jobs using group and chain. Ensure delay() is called on the top-level group or chain. ```python 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({"state": "done"}) return True ``` -------------------------------- ### Postpone Method Calls with Job Queue Source: https://github.com/oca/queue/blob/18.0/queue_job/readme/DESCRIPTION.md Use `with_delay()` to postpone a method call, which will be executed asynchronously by the job queue. Ensure the target model and method are correctly defined. ```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) ``` -------------------------------- ### Split Delayable Job Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Splits a delayable job into multiple smaller jobs, each processing a subset of records. Useful for parallelizing tasks and avoiding long-running jobs. ```python def button_split_delayable(self): ( self # Can be a big recordset, let's say 1000 records .delayable() .generate_thumbnail((50, 50)) .set(priority=30) .set(description=_("generate xxx")) .split(50) # Split the job in 20 jobs of 50 records each .delay() ) ``` -------------------------------- ### Delaying a Method Call Source: https://github.com/oca/queue/blob/18.0/queue_job/static/description/index.html Postpone a method call to be executed asynchronously. The job captures the method and its arguments and is executed when a Jobrunner becomes available. ```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) ``` -------------------------------- ### Disable Related Action Source: https://github.com/oca/queue/blob/18.0/queue_job/README.rst Configure job to disable its related action by setting 'enable' to False. 'func_name' specifies the method to call, and 'kwargs' provides arguments. ```python { "enable": False, "func_name": "related_action_partner", "kwargs": {"name": "Partner"}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.