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

Add a check for changes in study session message before reposting #48

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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]
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the separation here! It definitely gives us a lot more control. What do you think about having this sendChannelReminder function get called when new study sessions/events are made?
Hopefully that would also remove any frustration of users scheduling a session and having to wait a number of hours before it shows up in the events channel.

function createStudySession(message) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes a lot of sense!

In fact, since that would handle all cases where a study session is added to the list (there's no other way to create a study session), the only thing that would be handled by the scheduler is the case when removing a study session from the list (either through cancellation or the date passing), which I think we can get away with being less urgent with an update for - could maybe drop the scheduled task time to 12-hourly? Daily?

Also, I haven't checked but I think once a study session passes it remains in the database forever, do you think those are useful to keep around? If not, I think it would be a "good first issue" to create a weekly/fortnightly/monthly scheduled task that deletes all study sessions that have passed


export default tasks;
37 changes: 24 additions & 13 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,17 +26,19 @@ export default function sendChannelReminder(client) {

const mostRecentStudySessionMessage = studySessionMessages.first();

if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) {
mostRecentStudySessionMessage.delete().then(() => {
makeStudySessionMessage().then((studyMessage) => {
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 @@ -54,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 Expand Up @@ -98,6 +94,21 @@ function getUpcomingStudySessionSummary(message) {
return truncatedString;
}

function upcomingSessionsAreDifferent(oldMessage, newMessage) {
const oldMessageEmbed = oldMessage.embeds[0];
const newMessageEmbed = newMessage.embed;
if (oldMessageEmbed.title !== newMessageEmbed.title) {
return true;
}
if (oldMessageEmbed.fields.length !== newMessageEmbed.fields.length) {
return true;
}
return oldMessageEmbed.fields.some((oldMessageEmbedField, index) => {
return newMessageEmbed.fields[index].name !== oldMessageEmbedField.name ||
newMessageEmbed.fields[index].value !== oldMessageEmbedField.value
});
}

function createNotFoundMessage() {
return {
content: upcomingStudySessionMessageContent,
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