47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { Hono } from "hono";
|
|
|
|
import { onNewEpisode } from "~/jobs/new-episode";
|
|
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
|
import { maybeUpdateLastConnectedAt } from "~/middleware/maybeUpdateLastConnectedAt";
|
|
|
|
import type { QueueBody } from "./libs/tasks/queueTask";
|
|
|
|
export const app = new Hono<Cloudflare.Env>();
|
|
|
|
app.use(maybeUpdateLastConnectedAt);
|
|
|
|
// GraphQL endpoint replaces all REST routes
|
|
app.route(
|
|
"/graphql",
|
|
await import("~/graphql").then((module) => module.default),
|
|
);
|
|
|
|
export default {
|
|
fetch: app.fetch,
|
|
async queue(batch) {
|
|
switch (batch.queue as QueueName) {
|
|
case "ANILIST_UPDATES":
|
|
batch.retryAll();
|
|
break;
|
|
case "NEW_EPISODE":
|
|
for (const message of (batch as MessageBatch<QueueBody["NEW_EPISODE"]>)
|
|
.messages) {
|
|
await onNewEpisode(
|
|
message.body.aniListId,
|
|
message.body.episodeNumber,
|
|
);
|
|
message.ack();
|
|
}
|
|
break;
|
|
}
|
|
},
|
|
async scheduled(event, env, ctx) {
|
|
const { processDelayedTasks } = await import(
|
|
"~/libs/tasks/processDelayedTasks"
|
|
);
|
|
await processDelayedTasks(env, ctx);
|
|
},
|
|
} satisfies ExportedHandler<Env>;
|
|
|
|
export { AnilistDurableObject as AnilistDo } from "~/libs/anilist/anilist-do.ts";
|