Skip to content

Commit

Permalink
Updating hygiene and descriptions
Browse files Browse the repository at this point in the history
Moved variables to be more in line with best practice, changed the if evaluation to be more intuitive (saying is today after the original message time plus the timeout window is more natural than saying is the original message time plus the timeout interval is less than today) and added a comment describing it for clarity
  • Loading branch information
jesse-matsec committed Dec 15, 2022
1 parent 73fff29 commit fb81b96
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/api/_acks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ const REMINDER_FREQUENCY_MS = REMINDER_FREQUENCY_MIN * 60 * 1000;

const DEBUG_CHECK_ALL = false;

const NOW = new Date().getTime();

const TIMEOUT_INTERVAL = 30; //timeout interval in days (30 by default)
const TIMEOUT_INTERVAL = 30 * 24 * 60 * 60 * 1000; // Default timeout interval for ackbot message checking is 30d

export async function checkForReminders() {
const upperBound = DEBUG_CHECK_ALL ? '+inf' : NOW - REMINDER_FREQUENCY_MS;
console.log('checkForReminders started: ', { now: NOW, upperBound });
const now = new Date().getTime();
const upperBound = DEBUG_CHECK_ALL ? '+inf' : now - REMINDER_FREQUENCY_MS;
console.log('checkForReminders started: ', { now: now, upperBound });

const vals = await redis.zrange(REDIS_ACK_KEY, '-inf', upperBound, { byScore: true }) as string[];
console.log('checkForReminders got reminders: ', { now: NOW, vals });
console.log('checkForReminders got reminders: ', { now: now, vals });

const complete: { channel: string, ts: string }[] = [];
const incomplete: { channel: string, ts: string }[] = [];
const timeoutValue: number = TIMEOUT_INTERVAL * 24 * 60 * 60 * 1000;

// check each of them
await map(vals, async (val, idx) => {
const [channel, ts] = val.split(':');
Expand All @@ -35,7 +34,7 @@ export async function checkForReminders() {
const { isComplete } = await checkMessageAcks(channel, ts, false);
if (isComplete) {
complete.push({ channel, ts });
} else if ( parseInt(ts) + timeoutValue < NOW ) {
} else if ( now > parseInt(ts, 10) + TIMEOUT_INTERVAL ) { // At execution, check if the present time is after the day and time of the original message + 30 days, if so, mark the message as complete (because of timeout)
complete.push({ channel, ts });
}
else {
Expand All @@ -53,8 +52,8 @@ export async function checkForReminders() {
if (incomplete.length > 0) {
await saveReminders(incomplete);
}
console.log('checkForReminders done: ', { vals, complete, incomplete, now: NOW, upperBound });
return { vals, complete, incomplete, now: NOW, upperBound };
console.log('checkForReminders done: ', { vals, complete, incomplete, now: now, upperBound });
return { vals, complete, incomplete, now: now, upperBound };
}

async function saveReminders(reminders: { channel: string, ts: string }[]): Promise<void> {
Expand Down

0 comments on commit fb81b96

Please sign in to comment.