Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions queue_job_cron_jobrunner/models/queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from psycopg2 import OperationalError

from odoo import api, models, tools
from odoo import api, models
from odoo.service.model import PG_CONCURRENCY_ERRORS_TO_RETRY

from odoo.addons.queue_job.controllers.main import PG_RETRY
Expand Down Expand Up @@ -69,7 +69,9 @@ def _process(self, commit=False):
# Automatically retry the typical transaction serialization errors
if err.pgcode not in PG_CONCURRENCY_ERRORS_TO_RETRY:
raise
message = tools.ustr(err.pgerror, errors="replace")
# pgerror is already a str (or None): tools.ustr() is
# deprecated since Odoo 18 and only called str() here.
Comment thread
robsalasco marked this conversation as resolved.
Resolved
message = str(err.pgerror)
job.postpone(result=message, seconds=PG_RETRY)
job.set_pending(reset_retry=False)
job.store()
Expand Down
40 changes: 40 additions & 0 deletions queue_job_cron_jobrunner/tests/test_queue_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
# @author Iván Todorovich <ivan.todorovich@camptocamp.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import warnings
from datetime import timedelta
from unittest.mock import patch

from freezegun import freeze_time
from psycopg2 import OperationalError

from odoo import fields
from odoo.service.model import PG_CONCURRENCY_ERRORS_TO_RETRY
from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger

from odoo.addons.queue_job.controllers.main import PG_RETRY
from odoo.addons.queue_job.job import Job


class TestQueueJob(TransactionCase):
@classmethod
Expand All @@ -36,6 +43,39 @@ def test_queue_job_cron_trigger(self):
job_record.eta = fields.Datetime.now() + timedelta(hours=1)
self.assertTriggerAt(job_record.eta, "A new trigger should've been created")

@freeze_time("2022-02-22 22:22:22")
@mute_logger("odoo.addons.queue_job_cron_jobrunner.models.queue_job")
def test_queue_job_process_concurrency_error(self):
"""A PostgreSQL concurrency error postpones the job without using
deprecated Odoo APIs (e.g. tools.ustr)"""
job_record = (
self.env["res.partner"].with_delay().create({"name": "test"}).db_record()
)

# pgcode/pgerror are read-only on psycopg2 errors: override them
# in a subclass instead.
class SerializationFailure(OperationalError):
pgcode = list(PG_CONCURRENCY_ERRORS_TO_RETRY)[0]
pgerror = "could not serialize access"

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always", DeprecationWarning)
with patch.object(Job, "perform", side_effect=SerializationFailure("boom")):
job_record._process()

own_warnings = [
str(w.message)
for w in caught
if issubclass(w.category, DeprecationWarning)
and w.filename.endswith("queue_job_cron_jobrunner/models/queue_job.py")
]
self.assertFalse(own_warnings)
self.assertEqual(job_record.state, "pending")
self.assertEqual(job_record.result, "could not serialize access")
self.assertEqual(
job_record.eta, fields.Datetime.now() + timedelta(seconds=PG_RETRY)
)

@mute_logger("odoo.addons.queue_job_cron_jobrunner.models.queue_job")
def test_queue_job_process(self):
"""Test that jobs are processed by the queue job cron"""
Expand Down
Loading