From f1a72da8f41834c607b4ed242d65c2d6587864f7 Mon Sep 17 00:00:00 2001 From: Roberto Salas Date: Tue, 24 Nov 2026 19:28:10 -0300 Subject: [PATCH] [FIX] queue_job_cron_jobrunner: do not use deprecated tools.ustr() tools.ustr() is deprecated since Odoo 18 and emits a DeprecationWarning on every call. psycopg2 exposes pgerror as a str (or None), and for those values ustr() only returned str(value), so use str() directly. Add a test covering the PostgreSQL concurrency error branch of _process(): the job is postponed by PG_RETRY seconds with the error text as result, without deprecation warnings. --- queue_job_cron_jobrunner/models/queue_job.py | 6 ++- .../tests/test_queue_job.py | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/queue_job_cron_jobrunner/models/queue_job.py b/queue_job_cron_jobrunner/models/queue_job.py index 3213d28d0b..e978f3990b 100644 --- a/queue_job_cron_jobrunner/models/queue_job.py +++ b/queue_job_cron_jobrunner/models/queue_job.py @@ -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 @@ -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. + message = str(err.pgerror) job.postpone(result=message, seconds=PG_RETRY) job.set_pending(reset_retry=False) job.store() diff --git a/queue_job_cron_jobrunner/tests/test_queue_job.py b/queue_job_cron_jobrunner/tests/test_queue_job.py index 54800b792c..ac36d4abe2 100644 --- a/queue_job_cron_jobrunner/tests/test_queue_job.py +++ b/queue_job_cron_jobrunner/tests/test_queue_job.py @@ -2,14 +2,21 @@ # @author Iván Todorovich # 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 @@ -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"""