|
6 | 6 | import random |
7 | 7 | import time |
8 | 8 | import traceback |
| 9 | +from contextlib import contextmanager |
9 | 10 | from io import StringIO |
10 | 11 |
|
11 | 12 | from psycopg2 import OperationalError, errorcodes |
|
26 | 27 | DEPENDS_MAX_TRIES_ON_CONCURRENCY_FAILURE = 5 |
27 | 28 |
|
28 | 29 |
|
| 30 | +@contextmanager |
| 31 | +def _prevent_commit(cr): |
| 32 | + """Context manager to prevent commits on a cursor. |
| 33 | +
|
| 34 | + Commiting while the job is not finished would release the job lock, causing |
| 35 | + it to be started again by the dead jobs requeuer. |
| 36 | + """ |
| 37 | + def forbidden_commit(*args, **kwargs): |
| 38 | + raise RuntimeError("Commit is forbidden in queue jobs") |
| 39 | + |
| 40 | + original_commit = cr.commit |
| 41 | + cr.commit = forbidden_commit |
| 42 | + try: |
| 43 | + yield |
| 44 | + finally: |
| 45 | + cr.commit = original_commit |
| 46 | + |
| 47 | + |
29 | 48 | class RunJobController(http.Controller): |
30 | 49 | @classmethod |
31 | 50 | def _acquire_job(cls, env: api.Environment, job_uuid: str) -> Job | None: |
@@ -69,13 +88,15 @@ def _acquire_job(cls, env: api.Environment, job_uuid: str) -> Job | None: |
69 | 88 | def _try_perform_job(cls, env, job): |
70 | 89 | """Try to perform the job, mark it done and commit if successful.""" |
71 | 90 | _logger.debug("%s started", job) |
72 | | - job.perform() |
73 | | - # Triggers any stored computed fields before calling 'set_done' |
74 | | - # so that will be part of the 'exec_time' |
75 | | - env.flush_all() |
76 | | - job.set_done() |
77 | | - job.store() |
78 | | - env.flush_all() |
| 91 | + assert env is job.env # TODO refactor |
| 92 | + with _prevent_commit(env.cr): |
| 93 | + job.perform() |
| 94 | + # Triggers any stored computed fields before calling 'set_done' |
| 95 | + # so that will be part of the 'exec_time' |
| 96 | + env.flush_all() |
| 97 | + job.set_done() |
| 98 | + job.store() |
| 99 | + env.flush_all() |
79 | 100 | env.cr.commit() |
80 | 101 | _logger.debug("%s done", job) |
81 | 102 |
|
|
0 commit comments