feat: implement retry logic and row locking for threaded KDS report updates to prevent concurrency conflicts
This commit is contained in:
parent
c6aa68fe39
commit
88571bc418
@ -1,104 +1,138 @@
|
|||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
import random
|
||||||
from odoo import models, fields, api
|
from odoo import models, fields, api
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def _threaded_report_update(registry, uid, context, pdis_state_id, is_reset, is_completed):
|
def _threaded_report_update(registry, uid, context, pdis_state_id, is_reset, is_completed):
|
||||||
"""Background worker to update KDS reports without blocking the POS transaction."""
|
"""
|
||||||
try:
|
Background worker with Retry Logic and Row Locking.
|
||||||
with registry.cursor() as new_cr:
|
"""
|
||||||
new_env = api.Environment(new_cr, uid, context)
|
max_retries = 5
|
||||||
pdis_state = new_env['pos.prep.state'].browse(pdis_state_id)
|
for attempt in range(max_retries):
|
||||||
if not pdis_state.exists():
|
try:
|
||||||
return
|
# Staggered start to avoid simultaneous collisions
|
||||||
|
time.sleep(random.uniform(0.05, 0.2))
|
||||||
|
|
||||||
|
with registry.cursor() as new_cr:
|
||||||
|
new_env = api.Environment(new_cr, uid, context)
|
||||||
|
pdis_state = new_env['pos.prep.state'].browse(pdis_state_id)
|
||||||
|
if not pdis_state.exists():
|
||||||
|
return
|
||||||
|
|
||||||
|
order_line = pdis_state.prep_line_id.pos_order_line_id
|
||||||
|
if not order_line:
|
||||||
|
return
|
||||||
|
|
||||||
|
order = order_line.order_id
|
||||||
|
order_name = order.name or order.pos_reference or "Unknown Order"
|
||||||
|
stage = pdis_state.stage_id
|
||||||
|
display_id = stage.prep_display_id.id
|
||||||
|
order_id = order.id
|
||||||
|
|
||||||
|
KdsLineReport = new_env['pos.kds.report.line'].sudo()
|
||||||
|
KdsOrderReport = new_env['pos.kds.report.order'].sudo()
|
||||||
|
|
||||||
|
# --- ROW LOCKING ---
|
||||||
|
# Attempt to lock the existing order report if it exists
|
||||||
|
new_cr.execute("""
|
||||||
|
SELECT id FROM pos_kds_report_order
|
||||||
|
WHERE pos_order_id = %s AND prep_display_id = %s
|
||||||
|
FOR UPDATE
|
||||||
|
""", (order_id, display_id))
|
||||||
|
# -------------------
|
||||||
|
|
||||||
|
if is_reset:
|
||||||
|
line_report = KdsLineReport.search([
|
||||||
|
('pos_order_line_id', '=', order_line.id),
|
||||||
|
('prep_display_id', '=', display_id)
|
||||||
|
], limit=1)
|
||||||
|
if line_report:
|
||||||
|
line_report.unlink()
|
||||||
|
|
||||||
|
order_report = KdsOrderReport.search([
|
||||||
|
('pos_order_id', '=', order_id),
|
||||||
|
('prep_display_id', '=', display_id)
|
||||||
|
], limit=1)
|
||||||
|
|
||||||
|
if order_report:
|
||||||
|
res = KdsLineReport._read_group(
|
||||||
|
[('pos_order_id', '=', order_id), ('prep_display_id', '=', display_id)],
|
||||||
|
aggregates=['completion_time:max']
|
||||||
|
)
|
||||||
|
max_comp_time = res[0][0] if res else 0
|
||||||
|
if max_comp_time == 0:
|
||||||
|
order_report.unlink()
|
||||||
|
else:
|
||||||
|
order_report.write({'completion_time': max_comp_time})
|
||||||
|
new_cr.commit()
|
||||||
|
_logger.info("POS_KDS_PERF: Reset report SUCCESS for %s", order_name)
|
||||||
|
return
|
||||||
|
|
||||||
|
# If completed
|
||||||
|
prep_time = max(0, order_line.preparation_time)
|
||||||
|
svc_time = max(0, order_line.service_time)
|
||||||
|
comp_time = prep_time + svc_time
|
||||||
|
|
||||||
|
vals = {
|
||||||
|
'pos_order_id': order_id,
|
||||||
|
'pos_order_line_id': order_line.id,
|
||||||
|
'product_id': order_line.product_id.id,
|
||||||
|
'prep_display_id': display_id,
|
||||||
|
'preparation_time': prep_time,
|
||||||
|
'service_time': svc_time,
|
||||||
|
'completion_time': comp_time,
|
||||||
|
'completion_datetime': fields.Datetime.now(),
|
||||||
|
}
|
||||||
|
|
||||||
order_line = pdis_state.prep_line_id.pos_order_line_id
|
|
||||||
stage = pdis_state.stage_id
|
|
||||||
display_id = stage.prep_display_id.id
|
|
||||||
|
|
||||||
KdsLineReport = new_env['pos.kds.report.line'].sudo()
|
|
||||||
KdsOrderReport = new_env['pos.kds.report.order'].sudo()
|
|
||||||
|
|
||||||
if is_reset:
|
|
||||||
line_report = KdsLineReport.search([
|
line_report = KdsLineReport.search([
|
||||||
('pos_order_line_id', '=', order_line.id),
|
('pos_order_line_id', '=', order_line.id),
|
||||||
('prep_display_id', '=', display_id)
|
('prep_display_id', '=', display_id)
|
||||||
], limit=1)
|
], limit=1)
|
||||||
|
|
||||||
if line_report:
|
if line_report:
|
||||||
line_report.unlink()
|
line_report.write(vals)
|
||||||
|
else:
|
||||||
|
KdsLineReport.create(vals)
|
||||||
|
|
||||||
|
# Update order-level report
|
||||||
order_report = KdsOrderReport.search([
|
order_report = KdsOrderReport.search([
|
||||||
('pos_order_id', '=', order_line.order_id.id),
|
('pos_order_id', '=', order_id),
|
||||||
('prep_display_id', '=', display_id)
|
('prep_display_id', '=', display_id)
|
||||||
], limit=1)
|
], limit=1)
|
||||||
|
|
||||||
|
res = KdsLineReport._read_group(
|
||||||
|
[('pos_order_id', '=', order_id), ('prep_display_id', '=', display_id)],
|
||||||
|
aggregates=['completion_time:max']
|
||||||
|
)
|
||||||
|
max_comp_time = res[0][0] if res else comp_time
|
||||||
|
|
||||||
|
order_vals = {
|
||||||
|
'pos_order_id': order_id,
|
||||||
|
'prep_display_id': display_id,
|
||||||
|
'completion_time': max_comp_time,
|
||||||
|
'completion_datetime': fields.Datetime.now(),
|
||||||
|
}
|
||||||
if order_report:
|
if order_report:
|
||||||
res = KdsLineReport._read_group(
|
order_report.write(order_vals)
|
||||||
[('pos_order_id', '=', order_line.order_id.id), ('prep_display_id', '=', display_id)],
|
else:
|
||||||
aggregates=['completion_time:max']
|
KdsOrderReport.create(order_vals)
|
||||||
)
|
|
||||||
max_comp_time = res[0][0] if res else 0
|
|
||||||
if max_comp_time == 0:
|
|
||||||
order_report.unlink()
|
|
||||||
else:
|
|
||||||
order_report.write({'completion_time': max_comp_time})
|
|
||||||
new_cr.commit()
|
new_cr.commit()
|
||||||
return
|
_logger.info("POS_KDS_PERF: Background report update SUCCESS for order %s", order_name)
|
||||||
|
return # Exit loop on success
|
||||||
# If completed
|
|
||||||
prep_time = max(0, order_line.preparation_time)
|
|
||||||
svc_time = max(0, order_line.service_time)
|
|
||||||
comp_time = prep_time + svc_time
|
|
||||||
|
|
||||||
vals = {
|
|
||||||
'pos_order_id': order_line.order_id.id,
|
|
||||||
'pos_order_line_id': order_line.id,
|
|
||||||
'product_id': order_line.product_id.id,
|
|
||||||
'prep_display_id': display_id,
|
|
||||||
'preparation_time': prep_time,
|
|
||||||
'service_time': svc_time,
|
|
||||||
'completion_time': comp_time,
|
|
||||||
'completion_datetime': fields.Datetime.now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
line_report = KdsLineReport.search([
|
|
||||||
('pos_order_line_id', '=', order_line.id),
|
|
||||||
('prep_display_id', '=', display_id)
|
|
||||||
], limit=1)
|
|
||||||
|
|
||||||
if line_report:
|
|
||||||
line_report.write(vals)
|
|
||||||
else:
|
|
||||||
KdsLineReport.create(vals)
|
|
||||||
|
|
||||||
# Update order-level report
|
except Exception as e:
|
||||||
order_report = KdsOrderReport.search([
|
if "could not serialize access" in str(e) or "concurrent update" in str(e):
|
||||||
('pos_order_id', '=', order_line.order_id.id),
|
_logger.warning("POS_KDS_PERF: Concurrent update detected, retrying (%s/%s)...", attempt + 1, max_retries)
|
||||||
('prep_display_id', '=', display_id)
|
continue
|
||||||
], limit=1)
|
|
||||||
|
|
||||||
res = KdsLineReport._read_group(
|
|
||||||
[('pos_order_id', '=', order_line.order_id.id), ('prep_display_id', '=', display_id)],
|
|
||||||
aggregates=['completion_time:max']
|
|
||||||
)
|
|
||||||
max_comp_time = res[0][0] if res else comp_time
|
|
||||||
|
|
||||||
order_vals = {
|
|
||||||
'pos_order_id': order_line.order_id.id,
|
|
||||||
'prep_display_id': display_id,
|
|
||||||
'completion_time': max_comp_time,
|
|
||||||
'completion_datetime': fields.Datetime.now(),
|
|
||||||
}
|
|
||||||
if order_report:
|
|
||||||
order_report.write(order_vals)
|
|
||||||
else:
|
else:
|
||||||
KdsOrderReport.create(order_vals)
|
_logger.error("POS_KDS_PERF: Background KDS reporting failed: %s", e)
|
||||||
|
break
|
||||||
new_cr.commit()
|
else:
|
||||||
except Exception as e:
|
_logger.error("POS_KDS_PERF: Failed to update report after %s retries", max_retries)
|
||||||
_logger.error("Background KDS reporting failed: %s", e)
|
|
||||||
|
|
||||||
class PosPreparationState(models.Model):
|
class PosPreparationState(models.Model):
|
||||||
_inherit = 'pos.prep.state'
|
_inherit = 'pos.prep.state'
|
||||||
@ -111,7 +145,6 @@ class PosPreparationState(models.Model):
|
|||||||
if not stage:
|
if not stage:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Determine status synchronously to decide if we need a thread
|
|
||||||
if not hasattr(self.env, '_kds_display_cache'):
|
if not hasattr(self.env, '_kds_display_cache'):
|
||||||
self.env._kds_display_cache = {}
|
self.env._kds_display_cache = {}
|
||||||
|
|
||||||
@ -146,8 +179,6 @@ class PosPreparationState(models.Model):
|
|||||||
if not (is_completed or is_reset):
|
if not (is_completed or is_reset):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Start background thread to handle the reporting DB operations
|
|
||||||
# This makes the main POS sync call nearly instant
|
|
||||||
thread = threading.Thread(
|
thread = threading.Thread(
|
||||||
target=_threaded_report_update,
|
target=_threaded_report_update,
|
||||||
args=(self.env.registry, self.env.uid, self.env.context, pdis_state.id, is_reset, is_completed)
|
args=(self.env.registry, self.env.uid, self.env.context, pdis_state.id, is_reset, is_completed)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user