Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Split NOTIFICATIONS_SCHEDULE_DELAY into three separate variables #17669

Merged
merged 3 commits into from
Jun 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions app/notification-queue/server/NotificationQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { PushNotification } from '../../push-notifications/server';
const {
NOTIFICATIONS_WORKER_TIMEOUT = 2000,
NOTIFICATIONS_BATCH_SIZE = 100,
NOTIFICATIONS_SCHEDULE_DELAY = 120,
NOTIFICATIONS_SCHEDULE_DELAY_ONLINE = -1,
NOTIFICATIONS_SCHEDULE_DELAY_AWAY = 120,
NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE = 0,
} = process.env;

class NotificationClass {
Expand All @@ -18,7 +20,11 @@ class NotificationClass {

private maxBatchSize = Number(NOTIFICATIONS_BATCH_SIZE);

private maxScheduleDelaySeconds = Number(NOTIFICATIONS_SCHEDULE_DELAY);
private maxScheduleDelaySecondsOnline = Number(NOTIFICATIONS_SCHEDULE_DELAY_ONLINE);

private maxScheduleDelaySecondsAway = Number(NOTIFICATIONS_SCHEDULE_DELAY_AWAY);

private maxScheduleDelaySecondsOffline = Number(NOTIFICATIONS_SCHEDULE_DELAY_OFFLINE);

initWorker(): void {
this.running = true;
Expand Down Expand Up @@ -109,19 +115,32 @@ class NotificationClass {
return;
}

const delay = this.maxScheduleDelaySeconds;

let schedule: Date | undefined;

if (user.statusConnection === 'online') {
if (this.maxScheduleDelaySecondsOnline === -1) {
return;
}

schedule = new Date();
schedule.setSeconds(schedule.getSeconds() + delay);
schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOnline);
} else if (user.statusConnection === 'away') {
if (this.maxScheduleDelaySecondsAway === -1) {
return;
}

const elapsedSeconds = Math.floor((Date.now() - user._updatedAt) / 1000);
if (elapsedSeconds < delay) {
if (elapsedSeconds < this.maxScheduleDelaySecondsAway) {
schedule = new Date();
schedule.setSeconds(schedule.getSeconds() + delay - elapsedSeconds);
schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsAway - elapsedSeconds);
}
} else if (user.statusConnection === 'offline') {
if (this.maxScheduleDelaySecondsOffline === -1) {
return;
}

schedule = new Date();
schedule.setSeconds(schedule.getSeconds() + this.maxScheduleDelaySecondsOffline);
}

await NotificationQueue.insertOne({
Expand Down