Skip to content

Commit 6e3b13c

Browse files
committed
Add retry logic to syllabus scraping and Algolia sync functions
Add exponential backoff and error handling to all major fetch, database, and Algolia operations. Improves reliability of scraping, syllabus parsing, PDF download/upload, and Algolia sync/export. Adds utility for exporting Algolia data to Supabase storage.
1 parent 898cc54 commit 6e3b13c

File tree

4 files changed

+829
-395
lines changed

4 files changed

+829
-395
lines changed

src/config/algolia.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const algolia = (c: Context) => {
1010
}>(c);
1111

1212
return algoliaWithEnv(ALGOLIA_APP_ID, ALGOLIA_API_KEY);
13-
}
13+
};
1414

1515
export const algoliaWithEnv = (appId: string, apiKey: string) => {
16-
const client = algoliasearch(appId, apiKey, { requester: createFetchRequester() });
16+
const client = algoliasearch(appId, apiKey, {
17+
requester: createFetchRequester(),
18+
});
1719
const index = client.initIndex("nthu_courses");
1820
return index;
1921
};

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
scrapeArchivedCourses,
1717
scrapeSyllabus,
1818
syncCoursesToAlgolia,
19+
exportCoursesToAlgoliaFile,
1920
} from "./scheduled/syllabus";
2021

2122
export type Bindings = {
@@ -59,7 +60,9 @@ const APIHandler = {
5960
try {
6061
const cache = await scrapeArchivedCourses(env, semester);
6162
await scrapeSyllabus(env, semester, cache);
63+
// await scrapeSyllabus(env, semester);
6264
// await syncCoursesToAlgolia(env, semester);
65+
// await exportCoursesToAlgoliaFile(env, semester);
6366
console.log("Scheduled tasks completed successfully.");
6467
resolve(void 0);
6568
} catch (error) {
@@ -70,4 +73,4 @@ const APIHandler = {
7073
},
7174
};
7275

73-
export default APIHandler;
76+
export default APIHandler;

src/issue.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { z } from "zod";
55
import { env } from "hono/adapter";
66

77
type GithubEnv = {
8-
CLIENT_ID: string;
8+
GITHUB_CLIENT_ID: string;
99
GITHUB_APP_PRIVATE_KEY: string;
1010
GITHUB_INSTALLATION_ID: string;
1111
};
@@ -126,14 +126,17 @@ const app = new Hono()
126126
async (c) => {
127127
const { title, body, labels } = c.req.valid("form");
128128

129-
const { CLIENT_ID, GITHUB_APP_PRIVATE_KEY, GITHUB_INSTALLATION_ID } =
130-
env<GithubEnv>(c);
129+
const {
130+
GITHUB_CLIENT_ID,
131+
GITHUB_APP_PRIVATE_KEY,
132+
GITHUB_INSTALLATION_ID,
133+
} = env<GithubEnv>(c);
131134

132135
// base64 encoded private key to utf8, not using buffer
133136
const privateKey = atob(GITHUB_APP_PRIVATE_KEY);
134137

135138
const accessToken = await getInstallationAccessToken(
136-
CLIENT_ID,
139+
GITHUB_CLIENT_ID,
137140
privateKey,
138141
GITHUB_INSTALLATION_ID,
139142
);
@@ -165,12 +168,15 @@ const app = new Hono()
165168
),
166169
async (c) => {
167170
const { tag } = c.req.valid("query");
168-
const { CLIENT_ID, GITHUB_APP_PRIVATE_KEY, GITHUB_INSTALLATION_ID } =
169-
env<GithubEnv>(c);
171+
const {
172+
GITHUB_CLIENT_ID,
173+
GITHUB_APP_PRIVATE_KEY,
174+
GITHUB_INSTALLATION_ID,
175+
} = env<GithubEnv>(c);
170176
const privateKey = atob(GITHUB_APP_PRIVATE_KEY);
171177

172178
const accessToken = await getInstallationAccessToken(
173-
CLIENT_ID,
179+
GITHUB_CLIENT_ID,
174180
privateKey,
175181
GITHUB_INSTALLATION_ID,
176182
);

0 commit comments

Comments
 (0)