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
16 changes: 8 additions & 8 deletions queue_job/jobrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import selectors
import threading
import time
from contextlib import closing, contextmanager
from contextlib import contextmanager

import psycopg2
import requests
Expand Down Expand Up @@ -145,14 +145,14 @@ def close(self):

def _acquire_master_lock(self):
"""Acquire the master runner lock or raise MasterElectionLost"""
with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
cr.execute("SELECT pg_try_advisory_lock(%s)", (PG_ADVISORY_LOCK_ID,))
if not cr.fetchone()[0]:
msg = f"could not acquire master runner lock on {self.db_name}"
raise MasterElectionLost(msg)

def _has_queue_job(self):
with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
cr.execute(
"SELECT 1 FROM pg_tables WHERE tablename=%s", ("ir_module_module",)
)
Expand Down Expand Up @@ -181,7 +181,7 @@ def _has_queue_job(self):
return True

def _initialize(self):
with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
cr.execute("LISTEN queue_job")

@contextmanager
Expand All @@ -195,17 +195,17 @@ def select_jobs(self, where, args):
"priority, EXTRACT(EPOCH FROM eta), state "
f"FROM queue_job WHERE {where}"
)
with closing(self.conn.cursor("select_jobs", withhold=True)) as cr:
with self.conn.cursor("select_jobs", withhold=True) as cr:
cr.execute(query, args)
yield cr

def keep_alive(self):
query = "SELECT 1"
with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
cr.execute(query)

def set_job_enqueued(self, uuid):
with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
cr.execute(
"UPDATE queue_job SET state=%s, "
"date_enqueued=date_trunc('seconds', "
Expand Down Expand Up @@ -305,7 +305,7 @@ def requeue_dead_jobs(self):
the controller receiving the request).
"""

with closing(self.conn.cursor()) as cr:
with self.conn.cursor() as cr:
query = self._query_requeue_dead_jobs()

cr.execute(query)
Expand Down
2 changes: 1 addition & 1 deletion queue_job/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import test_run_job_controller_http
from . import test_run_rob_controller
from . import test_run_job_controller
from . import test_runner_channels
from . import test_runner_runner
from . import test_delayable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from contextlib import closing
from unittest.mock import patch

from odoo.tests.common import TransactionCase
Expand Down Expand Up @@ -74,7 +73,7 @@ def failing_job():
# The hook writes on the same partner from another cursor. Its changes
# are rolled back on close, and the lock timeout avoids waiting forever.
def on_fail(**kwargs):
with closing(self.registry.cursor()) as other_cr:
with self.registry.cursor() as other_cr:
other_cr.execute("SET LOCAL lock_timeout = '2s'")
other_cr.execute(
"UPDATE res_partner SET name = 'Written by hook' WHERE id = %s",
Expand Down
3 changes: 1 addition & 2 deletions test_queue_job/tests/test_requeue_dead_job.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2025 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from contextlib import closing
from datetime import datetime, timedelta

from odoo.tests import tagged
Expand Down Expand Up @@ -61,7 +60,7 @@ def test_lock(self):
job_obj.set_started()
job_obj.lock()

with closing(self.env.registry.cursor()) as new_cr:
with self.env.registry.cursor() as new_cr:
locks = self.get_locks(job_obj.uuid, new_cr)

# Row should be locked
Expand Down
Loading