-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationDispatchScheduler.java
More file actions
78 lines (65 loc) · 3.28 KB
/
NotificationDispatchScheduler.java
File metadata and controls
78 lines (65 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package me.pinitnotification.application.notification;
import me.pinitnotification.application.push.PushSendResult;
import me.pinitnotification.application.push.PushService;
import me.pinitnotification.domain.notification.UpcomingScheduleNotification;
import me.pinitnotification.domain.notification.UpcomingScheduleNotificationRepository;
import me.pinitnotification.domain.push.PushSubscriptionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Clock;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@Service
public class NotificationDispatchScheduler {
private static final Logger log = LoggerFactory.getLogger(NotificationDispatchScheduler.class);
private final UpcomingScheduleNotificationRepository notificationRepository;
private final NotificationDispatchQueryRepository dispatchQueryRepository;
private final PushSubscriptionRepository pushSubscriptionRepository;
private final PushService pushService;
private final Clock clock;
public NotificationDispatchScheduler(UpcomingScheduleNotificationRepository notificationRepository,
NotificationDispatchQueryRepository dispatchQueryRepository,
PushSubscriptionRepository pushSubscriptionRepository,
PushService pushService,
Clock clock) {
this.notificationRepository = notificationRepository;
this.dispatchQueryRepository = dispatchQueryRepository;
this.pushSubscriptionRepository = pushSubscriptionRepository;
this.pushService = pushService;
this.clock = clock;
}
@Scheduled(cron = "0 */10 * * * *")
@Transactional
public void dispatchDueNotifications() {
Instant now = Instant.now(clock);
List<NotificationDispatchItem> dispatchItems = dispatchQueryRepository.findDueNotificationsWithTokens(now);
if (dispatchItems.isEmpty()) {
return;
}
Set<String> tokensToDelete = new LinkedHashSet<>();
dispatchItems.forEach(item -> sendNotificationToOwner(item, tokensToDelete));
notificationRepository.deleteAllInBatch(dispatchItems.stream().map(NotificationDispatchItem::notification).toList());
if (!tokensToDelete.isEmpty()) {
pushSubscriptionRepository.deleteByTokens(tokensToDelete);
}
}
private void sendNotificationToOwner(NotificationDispatchItem dispatchItem, Set<String> tokensToDelete) {
UpcomingScheduleNotification notification = dispatchItem.notification();
List<String> tokens = dispatchItem.tokens();
if (tokens.isEmpty()) {
log.info("No push tokens for owner; skip sending. ownerId={}, scheduleId={}", notification.getOwnerId(), notification.getScheduleId());
return;
}
tokens.forEach(token -> {
PushSendResult result = pushService.sendPushMessage(token, notification);
if (result.shouldDeleteToken()) {
tokensToDelete.add(token);
}
});
}
}