1313from psycopg2 import OperationalError , errorcodes
1414from werkzeug .exceptions import BadRequest , Forbidden
1515
16- from odoo import SUPERUSER_ID , _ , api , http , registry , tools
16+ from odoo import SUPERUSER_ID , _ , api , http , tools
1717from odoo .service .model import PG_CONCURRENCY_ERRORS_TO_RETRY
18+ from odoo .tools import config
1819
1920from ..delay import chain , group
2021from ..exception import FailedJobError , NothingToDoJob , RetryableJobError
@@ -38,8 +39,10 @@ def _prevent_commit(cr):
3839 def forbidden_commit (* args , ** kwargs ):
3940 raise RuntimeError (
4041 "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."
42+ 'You may want to enable the "Allow Commit" option on the Job '
43+ "Function. Alternatively, if the current job is a cron running as "
44+ "queue job, you can modify it to run as a normal cron. More details on: "
45+ "https://github.com/OCA/queue/wiki/Upgrade-warning:-commits-inside-jobs"
4346 )
4447
4548 original_commit = cr .commit
@@ -103,7 +106,8 @@ def _try_perform_job(cls, env, job):
103106 job .set_done ()
104107 job .store ()
105108 env .flush_all ()
106- env .cr .commit ()
109+ if not config ["test_enable" ]:
110+ env .cr .commit ()
107111 _logger .debug ("%s done" , job )
108112
109113 @classmethod
@@ -141,8 +145,7 @@ def _enqueue_dependent_jobs(cls, env, job):
141145 def _runjob (cls , env : api .Environment , job : Job ) -> None :
142146 def retry_postpone (job , message , seconds = None ):
143147 job .env .clear ()
144- with registry (job .env .cr .dbname ).cursor () as new_cr :
145- job .env = api .Environment (new_cr , SUPERUSER_ID , {})
148+ with job .in_temporary_env ():
146149 job .postpone (result = message , seconds = seconds )
147150 job .set_pending (reset_retry = False )
148151 job .store ()
@@ -178,15 +181,15 @@ def retry_postpone(job, message, seconds=None):
178181 # traceback in the logs we should have the traceback when all
179182 # retries are exhausted
180183 env .cr .rollback ()
184+ return
181185
182186 except (FailedJobError , Exception ) as orig_exception :
183187 buff = StringIO ()
184188 traceback .print_exc (file = buff )
185189 traceback_txt = buff .getvalue ()
186190 _logger .error (traceback_txt )
187191 job .env .clear ()
188- with registry (job .env .cr .dbname ).cursor () as new_cr :
189- job .env = job .env (cr = new_cr )
192+ with job .in_temporary_env ():
190193 vals = cls ._get_failure_values (job , traceback_txt , orig_exception )
191194 job .set_failed (** vals )
192195 job .store ()
@@ -240,6 +243,7 @@ def create_test_job(
240243 failure_rate = 0 ,
241244 job_duration = 0 ,
242245 commit_within_job = False ,
246+ failure_retry_seconds = 0 ,
243247 ):
244248 """Create test jobs
245249
@@ -287,6 +291,12 @@ def create_test_job(
287291 except ValueError :
288292 max_retries = None
289293
294+ if failure_retry_seconds is not None :
295+ try :
296+ failure_retry_seconds = int (failure_retry_seconds )
297+ except ValueError :
298+ failure_retry_seconds = 0
299+
290300 if size == 1 :
291301 return self ._create_single_test_job (
292302 priority = priority ,
@@ -296,6 +306,7 @@ def create_test_job(
296306 failure_rate = failure_rate ,
297307 job_duration = job_duration ,
298308 commit_within_job = commit_within_job ,
309+ failure_retry_seconds = failure_retry_seconds ,
299310 )
300311
301312 if size > 1 :
@@ -308,6 +319,7 @@ def create_test_job(
308319 failure_rate = failure_rate ,
309320 job_duration = job_duration ,
310321 commit_within_job = commit_within_job ,
322+ failure_retry_seconds = failure_retry_seconds ,
311323 )
312324 return ""
313325
@@ -321,6 +333,7 @@ def _create_single_test_job(
321333 failure_rate = 0 ,
322334 job_duration = 0 ,
323335 commit_within_job = False ,
336+ failure_retry_seconds = 0 ,
324337 ):
325338 delayed = (
326339 http .request .env ["queue.job" ]
@@ -334,6 +347,7 @@ def _create_single_test_job(
334347 failure_rate = failure_rate ,
335348 job_duration = job_duration ,
336349 commit_within_job = commit_within_job ,
350+ failure_retry_seconds = failure_retry_seconds ,
337351 )
338352 )
339353 return "job uuid: %s" % (delayed .db_record ().uuid ,)
@@ -350,6 +364,7 @@ def _create_graph_test_jobs(
350364 failure_rate = 0 ,
351365 job_duration = 0 ,
352366 commit_within_job = False ,
367+ failure_retry_seconds = 0 ,
353368 ):
354369 model = http .request .env ["queue.job" ]
355370 current_count = 0
@@ -376,6 +391,7 @@ def _create_graph_test_jobs(
376391 failure_rate = failure_rate ,
377392 job_duration = job_duration ,
378393 commit_within_job = commit_within_job ,
394+ failure_retry_seconds = failure_retry_seconds ,
379395 )
380396 )
381397
0 commit comments