Skip to content

Commit

Permalink
Create additional scheduler to handle channel reminder
Browse files Browse the repository at this point in the history
Explicitly running the reminder task every 5 hours instead of
running it every minute and trying to handle the timing from
within the task logic
  • Loading branch information
Fox-Islam committed Jun 5, 2021
1 parent 5adc510 commit 4d4dff6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
20 changes: 17 additions & 3 deletions src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ import tasks from './tasks';

function runScheduler(client) {
// Run every minutes
cron.schedule('* * * * *', () => {
tasks.map((task) => task(client))
});
if (tasks["minute"].length > 0) {
cron.schedule('* * * * *', () => {
tasks["minute"].map((task) => task(client))
});
}

if (tasks["hour"].length > 0) {
cron.schedule('0 */1 * * *', () => {
tasks["hour"].map((task) => task(client))
});
}

if (tasks["five-hour"].length > 0) {
cron.schedule('0 */5 * * *', () => {
tasks["five-hour"].map((task) => task(client))
});
}
}

export default runScheduler;
11 changes: 10 additions & 1 deletion src/tasks/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import studySessionTasks from './studySession';

export default [...studySessionTasks];
const notifySubscribers = studySessionTasks[0];
const sendChannelReminder = studySessionTasks[1];

const tasks = {
"minute": [],
"hour": [notifySubscribers],
"five-hour": [sendChannelReminder]
};

export default tasks;
30 changes: 11 additions & 19 deletions src/tasks/studySession/channelReminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const { getUpcomingStudySessionsForScheduler } = require('../../scripts/activiti
const { getUTCFullDate } = require("../../utils/date");

const upcomingStudySessionMessageContent = "Here are the upcoming study sessions:\n*Make sure to check the time zones!*";
const oneHour = 60 * 60 * 1000;
const interval = oneHour * 5;

export default function sendChannelReminder(client) {
const studySessionChannel = client.channels.cache.get(process.env.STUDY_SESSION_CHANNEL);
Expand All @@ -28,21 +26,19 @@ export default function sendChannelReminder(client) {

const mostRecentStudySessionMessage = studySessionMessages.first();

if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) {
makeStudySessionMessage().then((studyMessage) => {
if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) {
mostRecentStudySessionMessage.delete().then(() => {
studySessionChannel.send(studyMessage).catch((error) => {
console.log(error);
});
}).catch((error) => {
makeStudySessionMessage().then((studyMessage) => {
if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) {
mostRecentStudySessionMessage.delete().then(() => {
studySessionChannel.send(studyMessage).catch((error) => {
console.log(error);
});
}
}).catch((error) => {
console.log(error);
});
}
}).catch((error) => {
console.log(error);
});
}
}).catch((error) => {
console.log(error);
});
}).catch(function (error) {
console.log(error)
});
Expand All @@ -58,10 +54,6 @@ function isStudySessionMessage(message) {
return message.content === upcomingStudySessionMessageContent;
}

function isMessageOlderThan5HoursAgo(message) {
return (new Date() - message.createdAt) > interval;
}

function makeStudySessionMessage() {
return getUpcomingStudySessionsForScheduler().then((upcomingStudySessions) => {
if (upcomingStudySessions.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/studySession/notifySubscribers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const StudySession = mongoose.model("StudySession");
export default function notifySubscribersTask(client) {
const now = new Date();
const limit = new Date().setHours(now.getHours() + 1);
StudySession.find({startDate: {$gt: now, $lt: limit}, notificationSent: false}, (error, studySessions) => {
StudySession.find({ startDate: { $gt: now, $lte: limit }, notificationSent: false }, (error, studySessions) => {
if (error) console.error(error);
if (studySessions.length === 0) return null;
studySessions.map((studySession) => {
Expand Down

0 comments on commit 4d4dff6

Please sign in to comment.