feat(backend): alert managers while a ticket keeps failing to print (setting print.alert_after_failures, default 2)
Owner request after step 1.11: hear about a failing ticket during service, not only when it's abandoned. - New notification print.failing (critical, all managers) when a print job's failed attempts reach print.alert_after_failures (ordering setting, default 2, 0 = never). Once per job; jobs on the same printer within an hour fold into one notification. print.failed_final (abandoned) stays. - Both the first attempt (route-and-print, sent after its commit) and the retry sweep report it. - Texts in en/el; tests for 1/2/3/0 and folding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -78,7 +78,8 @@
|
||||
"access_used": "Η υποστήριξη της Xenia συνδέθηκε στο κατάστημα (από {ip})."
|
||||
},
|
||||
"print": {
|
||||
"failed_final": "Ένα δελτίο της παραγγελίας #{order_id} δεν τυπώθηκε ποτέ στον εκτυπωτή {printer} ({attempts} προσπάθειες)."
|
||||
"failed_final": "Ένα δελτίο της παραγγελίας #{order_id} δεν τυπώθηκε ποτέ στον εκτυπωτή {printer} ({attempts} προσπάθειες).",
|
||||
"failing": "Ένα δελτίο της παραγγελίας #{order_id} δεν έχει τυπωθεί ακόμα στον εκτυπωτή {printer} (αποτυχημένες προσπάθειες μέχρι τώρα: {attempts}· συνεχίζει να προσπαθεί). Ελέγξτε τον εκτυπωτή."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,8 @@
|
||||
"access_used": "Xenia support logged in to this venue (from {ip})."
|
||||
},
|
||||
"print": {
|
||||
"failed_final": "A ticket for order #{order_id} never printed on {printer} ({attempts} attempts)."
|
||||
"failed_final": "A ticket for order #{order_id} never printed on {printer} ({attempts} attempts).",
|
||||
"failing": "A ticket for order #{order_id} hasn't printed on {printer} yet (failed attempts so far: {attempts}; still retrying). Check the printer."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ MODULE = Module(
|
||||
SettingDef('orders.waiter_price_adjust_allowed', 'false', "Allow waiters to adjust item prices (before or after ordering): 'true' | 'false'", type='bool'),
|
||||
SettingDef('payments.card_enabled', 'false', "Allow waiters to record card payments (in addition to cash): 'true' | 'false'", type='bool'),
|
||||
SettingDef('payments.waiter_revert_allowed', 'false', "Allow waiters to revert individual paid items back to unpaid: 'true' | 'false'", type='bool'),
|
||||
SettingDef('print.alert_after_failures', '2', 'Tell managers when a kitchen/bar ticket has failed to print this many times and is still retrying (0 = never; they are always told when a ticket is finally given up).', type='int'),
|
||||
SettingDef('print.beep_on_ticket', 'true', "Play beep when a kitchen ticket prints: 'true' | 'false'", type='bool'),
|
||||
SettingDef('print.beep_pattern', 'double', "Beep pattern: 'single' | 'double' | 'triple' | 'long' | 'custom:n1:n2:n3'", type='str'),
|
||||
SettingDef('print.divider_style', 'dash', 'Divider character used between sections: dash, equals, star, or empty', type='str'),
|
||||
|
||||
@@ -1792,6 +1792,7 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer
|
||||
return [{"printer_name": "spoof", "success": True, "error": None}]
|
||||
|
||||
results = []
|
||||
failing = [] # (job, printer name) that failed this time: may alert managers after the commit
|
||||
|
||||
order = db.query(Order).filter(Order.id == order_id).first()
|
||||
if not order:
|
||||
@@ -1984,6 +1985,7 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer
|
||||
error_msg = str(e)
|
||||
logger.error("print.job.failed job=%s attempt=%s printer=%s addr=%s:%s will_retry=yes error=%r",
|
||||
pj.id, pj.retry_count, printer.name, printer.ip_address, printer.port, error_msg)
|
||||
failing.append((pj, printer.name))
|
||||
|
||||
log = PrintLog(
|
||||
order_id=order_id,
|
||||
@@ -2002,6 +2004,8 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer
|
||||
if item.id in all_done_ids:
|
||||
item.printed = True
|
||||
db.commit()
|
||||
for pj, printer_name in failing: # after the commit: notify() commits too
|
||||
_notify_print_failing(db, pj, printer_name)
|
||||
|
||||
return results
|
||||
|
||||
@@ -2243,6 +2247,8 @@ def _retry_sweep():
|
||||
error_message=error_msg,
|
||||
))
|
||||
db.commit()
|
||||
if not success:
|
||||
_notify_print_failing(db, pj, printer.name)
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Unexpected error processing PrintJob %s: %s", job_id, e)
|
||||
@@ -2250,6 +2256,28 @@ def _retry_sweep():
|
||||
db.close()
|
||||
|
||||
|
||||
def _alert_after_failures(db) -> int:
|
||||
from models.settings import PosSettings
|
||||
row = db.query(PosSettings).filter(PosSettings.key == "print.alert_after_failures").first()
|
||||
try:
|
||||
return max(0, int(row.value)) if row and row.value not in (None, "") else 2
|
||||
except ValueError:
|
||||
return 2
|
||||
|
||||
|
||||
def _notify_print_failing(db, pj, printer_name: str) -> None:
|
||||
"""print.failing: a ticket has now failed `print.alert_after_failures` times (default 2,
|
||||
0 = never) and is still being retried. Sent once per job (when its attempt count reaches
|
||||
the setting); several jobs on one printer within an hour fold into one notification."""
|
||||
n = _alert_after_failures(db)
|
||||
if not n or (pj.retry_count or 0) != n:
|
||||
return
|
||||
from core.notifications import notify
|
||||
notify("print.failing", "critical", "all_managers",
|
||||
{"order_id": pj.order_id, "printer": printer_name, "attempts": pj.retry_count, "job_id": pj.id},
|
||||
dedupe_key=f"print.failing:{pj.printer_id}", ttl=datetime.timedelta(hours=1), db=db)
|
||||
|
||||
|
||||
def _notify_print_failed(db, pj) -> None:
|
||||
"""print.failed_final: managers learn that a ticket never printed (one notification per
|
||||
printer per hour; further failures in that hour are counted on it)."""
|
||||
|
||||
@@ -157,3 +157,27 @@ def test_print_failed_final(client, db, people):
|
||||
assert item["key"] == "print.failed_final" and item["count"] == 2
|
||||
assert item["message"] == "A ticket for order #42 never printed on 999 (7 attempts)."
|
||||
assert keys(client, people["waiter"][1]) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("setting,alerted_at", [(None, 2), ("1", 1), ("3", 3), ("0", None)])
|
||||
def test_print_failing_after_n_attempts(client, db, people, setting, alerted_at):
|
||||
from models.settings import PosSettings
|
||||
from services.printer_service import _notify_print_failing
|
||||
if setting is not None:
|
||||
db.merge(PosSettings(key="print.alert_after_failures", value=setting))
|
||||
db.commit()
|
||||
pj = SimpleNamespace(id=5, order_id=42, printer_id=3, retry_count=0)
|
||||
seen = []
|
||||
for attempt in range(1, 5):
|
||||
pj.retry_count = attempt
|
||||
_notify_print_failing(db, pj, "Bar")
|
||||
if keys(client, people["store_manager"][1]) and not seen:
|
||||
seen.append(attempt)
|
||||
assert (seen[0] if seen else None) == alerted_at
|
||||
if alerted_at:
|
||||
item = listing(client, people["store_manager"][1])["items"][0]
|
||||
assert item["key"] == "print.failing" and item["count"] == 1 # once per job
|
||||
assert "still retrying" in item["message"]
|
||||
# another job on the same printer within the hour folds in
|
||||
_notify_print_failing(db, SimpleNamespace(id=6, order_id=43, printer_id=3, retry_count=alerted_at), "Bar")
|
||||
assert listing(client, people["store_manager"][1])["items"][0]["count"] == 2
|
||||
|
||||
Reference in New Issue
Block a user