diff --git a/main/food/management/commands/process_inventory_scans.py b/main/food/management/commands/process_inventory_scans.py index bfc8bdd..742e775 100644 --- a/main/food/management/commands/process_inventory_scans.py +++ b/main/food/management/commands/process_inventory_scans.py @@ -2,7 +2,8 @@ import time from django.core.management.base import BaseCommand -from food.services.inventory_scanner import process_next_scan_item +from food.services.inventory_scanner import (dispatch_pending_scan_notifications, + process_next_scan_item) class Command(BaseCommand): @@ -16,6 +17,7 @@ class Command(BaseCommand): def handle(self, *args, **options): processed = 0 while True: + dispatch_pending_scan_notifications() did_work = process_next_scan_item() if did_work: processed += 1 diff --git a/main/food/services/inventory_scanner.py b/main/food/services/inventory_scanner.py index 3cb2384..69dc01f 100644 --- a/main/food/services/inventory_scanner.py +++ b/main/food/services/inventory_scanner.py @@ -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): diff --git a/main/food/test_inventory_scanner.py b/main/food/test_inventory_scanner.py index a0e0f95..205edab 100644 --- a/main/food/test_inventory_scanner.py +++ b/main/food/test_inventory_scanner.py @@ -12,9 +12,11 @@ from food.models import (FoodProduct, InventoryLocation, InventoryScanBatch, KitchenInventoryItem, OpenFoodFactsRequest) from food.services.inventory_scanner import (ScanWorkflowError, adjust_confirmed_count, approve_scan_item, approve_scan_items, confirm_scan_item, create_active_scan, get_or_create_active_run, - process_next_scan_item, save_review_item, submit_batch, update_review_preference) + dispatch_pending_scan_notifications, process_next_scan_item, save_review_item, + submit_batch, update_review_preference) from food.services.off_rate_limit import OpenFoodFactsRateLimited, off_http_request from food.services.openfoodfacts import OpenFoodFactsTransientError +from planner.services.notifications import NotificationError class InventoryScanQueueTests(TestCase): @@ -184,6 +186,25 @@ class InventoryReviewServiceTests(TestCase): stock = KitchenInventoryItem.objects.get(product=approved.product) self.assertEqual(stock.quantity, 2) + def test_completion_notification_retries_with_stable_ntfy_sequence_id(self): + run = InventoryScanRun.objects.create(user=self.user, selected_location=self.fridge, + status=InventoryScanRun.Status.COMPLETE_WITH_REVIEW, completed_at=timezone.now()) + with patch('planner.services.notifications.send_notification', side_effect=NotificationError('offline')) as send: + dispatch_pending_scan_notifications() + run.refresh_from_db() + self.assertIsNotNone(run.notification_attempted_at) + self.assertIsNone(run.notification_sent_at) + first_sequence_id = send.call_args.kwargs['sequence_id'] + InventoryScanRun.objects.filter(pk=run.pk).update( + notification_attempted_at=timezone.now() - timedelta(seconds=61)) + with patch('planner.services.notifications.send_notification', return_value=True) as send: + dispatch_pending_scan_notifications() + self.assertEqual(send.call_args.kwargs['sequence_id'], first_sequence_id) + dispatch_pending_scan_notifications() + send.assert_called_once() + run.refresh_from_db() + self.assertIsNotNone(run.notification_sent_at) + def test_bulk_approval_applies_shared_fields_to_selected_items_atomically(self): run = get_or_create_active_run(self.user, self.fridge.pk) items = [] diff --git a/main/planner/services/notifications.py b/main/planner/services/notifications.py index 04bee3d..2d119b3 100644 --- a/main/planner/services/notifications.py +++ b/main/planner/services/notifications.py @@ -9,11 +9,14 @@ class NotificationError(Exception): """A delivery error safe to display without exposing the topic URL.""" -def send_ntfy_notification(url, title, message, timeout=10): +def send_ntfy_notification(url, title, message, timeout=10, sequence_id=None): + headers = {'Content-Type': 'text/plain; charset=utf-8', 'Title': title} + if sequence_id: + headers['X-Sequence-ID'] = str(sequence_id)[:64] request = Request( url, data=message.encode('utf-8'), - headers={'Content-Type': 'text/plain; charset=utf-8', 'Title': title}, + headers=headers, method='POST', ) try: @@ -29,9 +32,9 @@ def send_ntfy_notification(url, title, message, timeout=10): raise NotificationError(f'ntfy server responded with HTTP {status}.') -def send_notification(user, title, message): +def send_notification(user, title, message, *, sequence_id=None): preferences = UserSettings.objects.filter(user=user).first() if not preferences or not preferences.ntfy_enabled or not preferences.ntfy_url: return False - send_ntfy_notification(preferences.ntfy_url, title, message) + send_ntfy_notification(preferences.ntfy_url, title, message, sequence_id=sequence_id) return True diff --git a/main/planner/test_ntfy.py b/main/planner/test_ntfy.py index c426cf0..9f6c4c4 100644 --- a/main/planner/test_ntfy.py +++ b/main/planner/test_ntfy.py @@ -5,7 +5,7 @@ from django.test import TestCase from django.urls import reverse from .models import UserSettings -from .services.notifications import NotificationError, send_notification +from .services.notifications import NotificationError, send_notification, send_ntfy_notification class NtfySettingsTests(TestCase): @@ -47,3 +47,15 @@ class NtfySettingsTests(TestCase): def test_disabled_preference_suppresses_delivery(self, sender): self.assertFalse(send_notification(self.user, 'Title', 'Message')) sender.assert_not_called() + + @patch('planner.services.notifications.urlopen') + def test_send_ntfy_notification_includes_stable_sequence_id(self, urlopen): + response = Mock() + response.getcode.return_value = 200 + urlopen.return_value.__enter__.return_value = response + + send_ntfy_notification(self.topic, 'Title', 'Message', sequence_id='inventory-scan-123') + + request = urlopen.call_args.args[0] + headers = {key.lower(): value for key, value in request.header_items()} + self.assertEqual(headers['x-sequence-id'], 'inventory-scan-123')