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

Feat: Add a cron job to hit the filter orphan tasks API every 6hours #65

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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';
}
yesyash marked this conversation as resolved.
Show resolved Hide resolved
} 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');
Comment on lines +24 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

await? won't this cause workers to exhaust 30s timelimit

Copy link
Member Author

Choose a reason for hiding this comment

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

Sir, without await how we can call the async function, I mean yes you are right the case is real where we might be exhausting the 30 time-limit. But I then how to do it without await

break;
}
case EVERY_11_HOURS: {
return await addMissedUpdatesRole(env);
Expand Down
Loading