short circuit on notifications job

This commit is contained in:
Andre Medeiros 2022-03-26 12:52:10 -04:00
parent e5670c9828
commit 88a3e0a21f
2 changed files with 13 additions and 2 deletions

View file

@ -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)
}

View file

@ -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]