Skip to content

Commit

Permalink
Fix potential timeout issues
Browse files Browse the repository at this point in the history
* Prevent tasks from running multiple times
* Add abort signals to fetch requests
  • Loading branch information
NeKzor committed Apr 17, 2024
1 parent 657e124 commit 4841f30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/server/tasks/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ const resetFailedAutorenders = async () => {
}
};

let isUpdating = false;

const update = async () => {
if (isUpdating) {
return;
}

isUpdating = true;

try {
await checkChangelogUpdates();
} catch (err) {
Expand All @@ -86,6 +94,8 @@ const update = async () => {
} catch (err) {
logger.error(err);
}

isUpdating = false;
};

setInterval(update, BOARD_INTEGRATION_UPDATE_INTERVAL);
6 changes: 6 additions & 0 deletions src/server/tasks/portal2_sr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
export const BOARD_BASE_API = 'https://board.portal2.sr';
export const AUTORENDER_BASE_API = 'https://autorender.portal2.sr/api/v1';

const DEFAULT_ABORT_TIMEOUT_MS = 10_000;

export type ChangelogOptions =
& {
id?: number;
Expand Down Expand Up @@ -79,6 +81,7 @@ export const getChangelog = async (options?: ChangelogOptions) => {
headers: {
'User-Agent': Deno.env.get('USER_AGENT')!,
},
signal: AbortSignal.timeout(DEFAULT_ABORT_TIMEOUT_MS),
});

if (!res.ok) {
Expand Down Expand Up @@ -106,6 +109,7 @@ export const fetchDemo = async (id: string | number) => {
'User-Agent': Deno.env.get('USER_AGENT')!,
},
redirect: 'manual',
signal: AbortSignal.timeout(DEFAULT_ABORT_TIMEOUT_MS),
});

const location = res.headers.get('Location');
Expand All @@ -121,6 +125,7 @@ export const fetchDemo = async (id: string | number) => {
headers: {
'User-Agent': Deno.env.get('USER_AGENT')!,
},
signal: AbortSignal.timeout(DEFAULT_ABORT_TIMEOUT_MS),
});

return {
Expand Down Expand Up @@ -152,6 +157,7 @@ export const getInfo = async (changelogId: string) => {
headers: {
'User-Agent': Deno.env.get('USER_AGENT')!,
},
signal: AbortSignal.timeout(DEFAULT_ABORT_TIMEOUT_MS),
});

if (!res.ok) {
Expand Down
9 changes: 9 additions & 0 deletions src/server/tasks/stale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ const checkStaleVideos = async () => {
}
};

let isUpdating = false;

const update = async () => {
if (isUpdating) {
return;
}

try {
isUpdating = true;
await checkStaleVideos();
} catch (err) {
logger.error(err);
} finally {
isUpdating = false;
}
};

Expand Down

0 comments on commit 4841f30

Please sign in to comment.