1313from werkzeug .exceptions import BadRequest , Forbidden
1414
1515from odoo import SUPERUSER_ID , _ , api , http
16- from odoo .modules .registry import Registry
1716from odoo .service .model import PG_CONCURRENCY_ERRORS_TO_RETRY
17+ from odoo .tools import config
1818
1919from ..delay import chain , group
2020from ..exception import FailedJobError , RetryableJobError
@@ -38,8 +38,10 @@ def _prevent_commit(cr):
3838 def forbidden_commit (* args , ** kwargs ):
3939 raise RuntimeError (
4040 "Commit is forbidden in queue jobs. "
41- "If the current job is a cron running as queue job, "
42- "modify it to run as a normal cron."
41+ 'You may want to enable the "Allow Commit" option on the Job '
42+ "Function. Alternatively, if the current job is a cron running as "
43+ "queue job, you can modify it to run as a normal cron. More details on: "
44+ "https://github.com/OCA/queue/wiki/Upgrade-warning:-commits-inside-jobs"
4345 )
4446
4547 original_commit = cr .commit
@@ -103,7 +105,8 @@ def _try_perform_job(cls, env, job):
103105 job .set_done ()
104106 job .store ()
105107 env .flush_all ()
106- env .cr .commit ()
108+ if not config ["test_enable" ]:
109+ env .cr .commit ()
107110 _logger .debug ("%s done" , job )
108111
109112 @classmethod
@@ -146,8 +149,7 @@ def _enqueue_dependent_jobs(cls, env, job):
146149 def _runjob (cls , env : api .Environment , job : Job ) -> None :
147150 def retry_postpone (job , message , seconds = None ):
148151 job .env .clear ()
149- with Registry (job .env .cr .dbname ).cursor () as new_cr :
150- job .env = api .Environment (new_cr , SUPERUSER_ID , {})
152+ with job .in_temporary_env ():
151153 job .postpone (result = message , seconds = seconds )
152154 job .set_pending (reset_retry = False )
153155 job .store ()
@@ -180,8 +182,7 @@ def retry_postpone(job, message, seconds=None):
180182 traceback_txt = buff .getvalue ()
181183 _logger .error (traceback_txt )
182184 job .env .clear ()
183- with Registry (job .env .cr .dbname ).cursor () as new_cr :
184- job .env = job .env (cr = new_cr )
185+ with job .in_temporary_env ():
185186 vals = cls ._get_failure_values (job , traceback_txt , orig_exception )
186187 job .set_failed (** vals )
187188 job .store ()
@@ -233,6 +234,7 @@ def create_test_job(
233234 failure_rate = 0 ,
234235 job_duration = 0 ,
235236 commit_within_job = False ,
237+ failure_retry_seconds = 0 ,
236238 ):
237239 if not http .request .env .user .has_group ("base.group_erp_manager" ):
238240 raise Forbidden (_ ("Access Denied" ))
@@ -270,6 +272,12 @@ def create_test_job(
270272 except ValueError :
271273 max_retries = None
272274
275+ if failure_retry_seconds is not None :
276+ try :
277+ failure_retry_seconds = int (failure_retry_seconds )
278+ except ValueError :
279+ failure_retry_seconds = 0
280+
273281 if size == 1 :
274282 return self ._create_single_test_job (
275283 priority = priority ,
@@ -279,6 +287,7 @@ def create_test_job(
279287 failure_rate = failure_rate ,
280288 job_duration = job_duration ,
281289 commit_within_job = commit_within_job ,
290+ failure_retry_seconds = failure_retry_seconds ,
282291 )
283292
284293 if size > 1 :
@@ -291,6 +300,7 @@ def create_test_job(
291300 failure_rate = failure_rate ,
292301 job_duration = job_duration ,
293302 commit_within_job = commit_within_job ,
303+ failure_retry_seconds = failure_retry_seconds ,
294304 )
295305 return ""
296306
@@ -304,6 +314,7 @@ def _create_single_test_job(
304314 failure_rate = 0 ,
305315 job_duration = 0 ,
306316 commit_within_job = False ,
317+ failure_retry_seconds = 0 ,
307318 ):
308319 delayed = (
309320 http .request .env ["queue.job" ]
@@ -317,6 +328,7 @@ def _create_single_test_job(
317328 failure_rate = failure_rate ,
318329 job_duration = job_duration ,
319330 commit_within_job = commit_within_job ,
331+ failure_retry_seconds = failure_retry_seconds ,
320332 )
321333 )
322334 return f"job uuid: { delayed .db_record ().uuid } "
@@ -333,6 +345,7 @@ def _create_graph_test_jobs(
333345 failure_rate = 0 ,
334346 job_duration = 0 ,
335347 commit_within_job = False ,
348+ failure_retry_seconds = 0 ,
336349 ):
337350 model = http .request .env ["queue.job" ]
338351 current_count = 0
@@ -359,6 +372,7 @@ def _create_graph_test_jobs(
359372 failure_rate = failure_rate ,
360373 job_duration = job_duration ,
361374 commit_within_job = commit_within_job ,
375+ failure_retry_seconds = failure_retry_seconds ,
362376 )
363377 )
364378
0 commit comments