Add retry-safe scan completion notifications
This commit is contained in:
@@ -397,27 +397,46 @@ def _send_completion_notification(run_id):
|
||||
now = timezone.now()
|
||||
with transaction.atomic():
|
||||
run = InventoryScanRun.objects.select_for_update().select_related('user').get(pk=run_id)
|
||||
if run.notification_attempted_at or run.status not in (
|
||||
if run.notification_sent_at or run.status not in (
|
||||
InventoryScanRun.Status.COMPLETE, InventoryScanRun.Status.COMPLETE_WITH_REVIEW):
|
||||
return
|
||||
retry_window = timedelta(seconds=60)
|
||||
if run.notification_attempted_at and run.notification_attempted_at > now - retry_window:
|
||||
return
|
||||
from django.db.models import Count
|
||||
tallies = dict(run.batches.values('items__status').annotate(total=Count('items')).values_list('items__status', 'total'))
|
||||
succeeded = tallies.get(InventoryScanItem.Status.COMPLETE, 0)
|
||||
review = sum(tallies.get(status, 0) for status in (InventoryScanItem.Status.NEEDS_REVIEW,
|
||||
InventoryScanItem.Status.DEFERRED))
|
||||
failed = tallies.get(InventoryScanItem.Status.FAILED, 0)
|
||||
run.notification_attempted_at = now # durable at-most-once delivery guard for worker restarts
|
||||
run.save(update_fields=('notification_attempted_at',))
|
||||
run.notification_attempted_at = now
|
||||
run.notification_error = ''
|
||||
run.save(update_fields=('notification_attempted_at', 'notification_error'))
|
||||
user = run.user
|
||||
url = '/food/inventory/#inventory-review'
|
||||
message = f'Bulk scan finished: {succeeded} added, {review} need review, {failed} failed. {url}'
|
||||
try:
|
||||
sent = send_notification(user, 'Kitchen inventory scan complete', message)
|
||||
sent = send_notification(user, 'Kitchen inventory scan complete', message,
|
||||
sequence_id=f'inventory-scan-{run.api_id}')
|
||||
except NotificationError as error:
|
||||
InventoryScanRun.objects.filter(pk=run_id).update(notification_error=str(error)[:255])
|
||||
return
|
||||
if sent:
|
||||
InventoryScanRun.objects.filter(pk=run_id).update(notification_sent_at=timezone.now(), notification_error='')
|
||||
else:
|
||||
InventoryScanRun.objects.filter(pk=run_id).update(notification_error='ntfy notifications are not enabled')
|
||||
|
||||
|
||||
def dispatch_pending_scan_notifications(limit=50):
|
||||
"""Retry completed-run notifications after a short persisted delivery cooldown."""
|
||||
cutoff = timezone.now() - timedelta(seconds=60)
|
||||
run_ids = InventoryScanRun.objects.filter(
|
||||
status__in=(InventoryScanRun.Status.COMPLETE, InventoryScanRun.Status.COMPLETE_WITH_REVIEW),
|
||||
notification_sent_at__isnull=True).exclude(notification_error='ntfy notifications are not enabled').filter(
|
||||
Q(notification_attempted_at__isnull=True) | Q(notification_attempted_at__lte=cutoff)
|
||||
).order_by('completed_at').values_list('pk', flat=True)[:limit]
|
||||
for run_id in run_ids:
|
||||
_send_completion_notification(run_id)
|
||||
|
||||
|
||||
def update_review_preference(user, mode):
|
||||
|
||||
Reference in New Issue
Block a user