From 88a3e0a21f12cc15a5ed66ad465635dc43df0e50 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 26 Mar 2022 12:52:10 -0400 Subject: [PATCH] short circuit on notifications job --- internal/repository/postgres_device.go | 8 ++++++-- internal/worker/notifications.go | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/repository/postgres_device.go b/internal/repository/postgres_device.go index 440e3ce..c146471 100644 --- a/internal/repository/postgres_device.go +++ b/internal/repository/postgres_device.go @@ -90,7 +90,9 @@ func (p *postgresDeviceRepository) GetInboxNotifiableByAccountID(ctx context.Con SELECT devices.id, apns_token, sandbox, active_until, grace_period_until FROM devices INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id - WHERE devices_accounts.account_id = $1 AND devices_accounts.inbox_notifiable = TRUE` + WHERE devices_accounts.account_id = $1 AND + devices_accounts.inbox_notifiable = TRUE AND + grace_period_until > NOW()` return p.fetch(ctx, query, id) } @@ -100,7 +102,9 @@ func (p *postgresDeviceRepository) GetWatcherNotifiableByAccountID(ctx context.C SELECT devices.id, apns_token, sandbox, active_until, grace_period_until FROM devices INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id - WHERE devices_accounts.account_id = $1 AND devices_accounts.watcher_notifiable = TRUE` + WHERE devices_accounts.account_id = $1 AND + devices_accounts.watcher_notifiable = TRUE AND + grace_period_until > NOW()` return p.fetch(ctx, query, id) } diff --git a/internal/worker/notifications.go b/internal/worker/notifications.go index 0da3249..d03d107 100644 --- a/internal/worker/notifications.go +++ b/internal/worker/notifications.go @@ -314,6 +314,13 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { return } + if len(devices) == 0 { + nc.logger.WithFields(logrus.Fields{ + "account#username": account.NormalizedUsername(), + }).Debug("no notifiable devices, finishing job") + return + } + // Iterate backwards so we notify from older to newer for i := msgs.Count - 1; i >= 0; i-- { msg := msgs.Children[i]