Skip to content

Commit

Permalink
Feat: Add a cron job to hit the filter orphan tasks API every 6hours
Browse files Browse the repository at this point in the history
  • Loading branch information
MehulKChaudhari committed Apr 2, 2024
1 parent b529218 commit 240a927
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
57 changes: 56 additions & 1 deletion src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import config from '../config/config';
import { NAMESPACE_NAME } from '../constants';
import { updateUserRoles } from '../services/discordBotServices';
import { getMissedUpdatesUsers } from '../services/rdsBackendService';
import { DiscordUserRole, env, NicknameUpdateResponseType, UserStatusResponse } from '../types/global.types';
import {
DiscordUserRole,
env,
NicknameUpdateResponseType,
OrphanTasksStatusUpdateResponseType,
UserStatusResponse,
} from '../types/global.types';
import { apiCaller } from '../utils/apiCaller';
import { chunks } from '../utils/arrayUtils';
import { generateJwt } from '../utils/generateJwt';
Expand Down Expand Up @@ -141,3 +147,52 @@ export const syncIdle7dUsers = async (env: env) => {
export const syncOnboarding31dPlusUsers = async (env: env) => {
return await apiCaller(env, 'discord-actions/group-onboarding-31d-plus', 'PUT');
};

export async function filterOrphanTasks(env: env) {
const namespace = env[NAMESPACE_NAME] as unknown as KVNamespace;
let lastOrphanTasksFilteration: string | null = '0';
try {
lastOrphanTasksFilteration = await namespace.get('ORPHAN_TASKS_UPDATED_TIME');
if (lastOrphanTasksFilteration === null) {
console.error('Error while fetching KV "ORPHAN_TASKS_UPDATED_TIME" timestamp');
}
if (!lastOrphanTasksFilteration) {
lastOrphanTasksFilteration = '0';
}
} catch (err) {
console.error(err, 'Error while fetching the timestamp of last orphan tasks filteration');
throw err;
}

const url = config(env).RDS_BASE_API_URL;
let token;
try {
token = await generateJwt(env);
} catch (err) {
console.error(`Error while generating JWT token: ${err}`);
throw err;
}
const response = await fetch(`${url}/tasks/orphanTasks`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
lastOrphanTasksFilteration,
}),
});
if (!response.ok) {
throw new Error('Error while trying to update status of orphan tasks to backlog');
}

const data: OrphanTasksStatusUpdateResponseType = await response.json();

try {
await namespace.put('ORPHAN_TASKS_UPDATED_TIME', Date.now().toString());
} catch (err) {
console.error('Error while trying to update the last orphan tasks filteration timestamp');
}

return data;
}
7 changes: 7 additions & 0 deletions src/types/global.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export type NicknameUpdateResponseType = {
unsuccessfulNicknameUpdates: number;
};
};

export type OrphanTasksStatusUpdateResponseType = {
message: string;
data: {
orphanTasksUpdatedCount: number;
};
};
export type DiscordUsersResponse = {
message: string;
data: DiscordUserIdList;
Expand Down
6 changes: 5 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addMissedUpdatesRole,
callDiscordNicknameBatchUpdate,
filterOrphanTasks,
syncExternalAccounts,
syncIdle7dUsers,
syncIdleUsers,
Expand All @@ -20,7 +21,10 @@ export default {
async scheduled(req: ScheduledController, env: env, ctx: ExecutionContext) {
switch (req.cron) {
case EVERY_6_HOURS: {
return await callDiscordNicknameBatchUpdate(env);
await callDiscordNicknameBatchUpdate(env);
await filterOrphanTasks(env);
console.log('Worker for filtering the orphan tasks has completed');
break;
}
case EVERY_11_HOURS: {
return await addMissedUpdatesRole(env);
Expand Down

0 comments on commit 240a927

Please sign in to comment.