refactor: decouple Anilist watch status updates from API endpoint to an asynchronous queue worker.
This commit is contained in:
31
src/index.ts
31
src/index.ts
@@ -5,6 +5,7 @@ import { maybeUpdateLastConnectedAt } from "~/controllers/maybeUpdateLastConnect
|
||||
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
||||
|
||||
import { onNewEpisode } from "./controllers/internal/new-episode";
|
||||
import { AnilistUpdateType } from "./libs/anilist/updateType";
|
||||
import type { QueueBody } from "./libs/tasks/queueTask";
|
||||
|
||||
export const app = new OpenAPIHono<{ Bindings: Env }>();
|
||||
@@ -74,7 +75,30 @@ export default {
|
||||
async queue(batch) {
|
||||
switch (batch.queue as QueueName) {
|
||||
case "ANILIST_UPDATES":
|
||||
batch.retryAll();
|
||||
for (const message of (
|
||||
batch as MessageBatch<QueueBody["ANILIST_UPDATES"]>
|
||||
).messages) {
|
||||
switch (message.body.updateType) {
|
||||
case AnilistUpdateType.UpdateWatchStatus:
|
||||
if (!message.body[AnilistUpdateType.UpdateWatchStatus]) {
|
||||
throw new Error(
|
||||
`Discarding update, unknown body ${JSON.stringify(message.body)}`,
|
||||
);
|
||||
}
|
||||
|
||||
const { updateWatchStatusOnAnilist } =
|
||||
await import("~/controllers/watch-status/anilist");
|
||||
const payload = message.body[AnilistUpdateType.UpdateWatchStatus];
|
||||
await updateWatchStatusOnAnilist(
|
||||
payload.titleId,
|
||||
payload.watchStatus,
|
||||
payload.aniListToken,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
message.ack();
|
||||
}
|
||||
break;
|
||||
case "NEW_EPISODE":
|
||||
for (const message of (batch as MessageBatch<QueueBody["NEW_EPISODE"]>)
|
||||
@@ -89,9 +113,8 @@ export default {
|
||||
}
|
||||
},
|
||||
async scheduled(event, env, ctx) {
|
||||
const { processDelayedTasks } = await import(
|
||||
"~/libs/tasks/processDelayedTasks"
|
||||
);
|
||||
const { processDelayedTasks } =
|
||||
await import("~/libs/tasks/processDelayedTasks");
|
||||
await processDelayedTasks(env, ctx);
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
|
||||
Reference in New Issue
Block a user