-
-
Notifications
You must be signed in to change notification settings - Fork 545
[18.0] Add 'Allow Commit' option on job functions #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 18.0
Are you sure you want to change the base?
Changes from all commits
e59e789
b4f3bec
6f50739
3b0619e
b7e0c83
bd2b1ff
33ca9e7
7ae3a60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import sys | ||
| import uuid | ||
| import weakref | ||
| from contextlib import contextmanager, nullcontext | ||
| from datetime import datetime, timedelta | ||
| from random import randint | ||
|
|
||
|
|
@@ -402,14 +403,9 @@ def __init__( | |
| raise TypeError("Job accepts only methods of Models") | ||
|
|
||
| recordset = func.__self__ | ||
| env = recordset.env | ||
| self.method_name = func.__name__ | ||
| self.recordset = recordset | ||
|
|
||
| self.env = env | ||
| self.job_model = self.env["queue.job"] | ||
| self.job_model_name = "queue.job" | ||
|
|
||
| self.job_config = ( | ||
| self.env["queue.job.function"].sudo().job_config(self.job_function_name) | ||
| ) | ||
|
|
@@ -459,10 +455,10 @@ def __init__( | |
| self.exc_message = None | ||
| self.exc_info = None | ||
|
|
||
| if "company_id" in env.context: | ||
| company_id = env.context["company_id"] | ||
| if "company_id" in self.env.context: | ||
| company_id = self.env.context["company_id"] | ||
| else: | ||
| company_id = env.company.id | ||
| company_id = self.env.company.id | ||
| self.company_id = company_id | ||
| self._eta = None | ||
| self.eta = eta | ||
|
|
@@ -487,7 +483,12 @@ def perform(self): | |
| """ | ||
| self.retry += 1 | ||
| try: | ||
| self.result = self.func(*tuple(self.args), **self.kwargs) | ||
| if self.job_config.allow_commit: | ||
| env_context_manager = self.in_temporary_env() | ||
| else: | ||
| env_context_manager = nullcontext() | ||
| with env_context_manager: | ||
| self.result = self.func(*tuple(self.args), **self.kwargs) | ||
| except RetryableJobError as err: | ||
| if err.ignore_retry: | ||
| self.retry -= 1 | ||
|
|
@@ -507,6 +508,16 @@ def perform(self): | |
|
|
||
| return self.result | ||
|
|
||
| @contextmanager | ||
| def in_temporary_env(self): | ||
| with self.env.registry.cursor() as new_cr: | ||
| env = self.env | ||
| self._env = env(cr=new_cr) | ||
| try: | ||
| yield | ||
| finally: | ||
| self._env = env | ||
|
|
||
| def _get_common_dependent_jobs_query(self): | ||
| return """ | ||
| UPDATE queue_job | ||
|
|
@@ -665,6 +676,14 @@ def __hash__(self): | |
| def db_record(self): | ||
| return self.db_records_from_uuids(self.env, [self.uuid]) | ||
|
|
||
| @property | ||
| def env(self): | ||
| return self.recordset.env | ||
|
|
||
| @env.setter | ||
| def _env(self, env): | ||
| self.recordset = self.recordset.with_env(env) | ||
|
|
||
| @property | ||
| def func(self): | ||
| recordset = self.recordset.with_context(job_uuid=self.uuid) | ||
|
|
@@ -729,7 +748,7 @@ def model_name(self): | |
|
|
||
| @property | ||
| def user_id(self): | ||
| return self.recordset.env.uid | ||
| return self.env.uid | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why changing this one in particular?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had 2 |
||
|
|
||
| @property | ||
| def eta(self): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.