feat: Add a new scheduled task to check upcoming titles and refactor its controller for cron execution
This commit is contained in:
@@ -15,7 +15,7 @@ type AiringSchedule = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export async function getUpcomingTitlesFromAnilist(req: HonoRequest) {
|
||||
export async function getUpcomingTitlesFromAnilist() {
|
||||
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
|
||||
const stub = env.ANILIST_DO.get(durableObjectId);
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ import { getUpcomingTitlesFromAnilist } from "./anilist";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
app.post("/", async (c) => {
|
||||
const titles = await getUpcomingTitlesFromAnilist(c.req);
|
||||
export async function checkUpcomingTitles() {
|
||||
const titles = await getUpcomingTitlesFromAnilist();
|
||||
|
||||
await Promise.allSettled(
|
||||
titles.map(async (title) => {
|
||||
@@ -44,6 +44,10 @@ app.post("/", async (c) => {
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
app.post("/", async (c) => {
|
||||
await checkUpcomingTitles();
|
||||
|
||||
return c.json(SuccessResponse, 200);
|
||||
});
|
||||
|
||||
19
src/index.ts
19
src/index.ts
@@ -12,6 +12,8 @@ import {
|
||||
} from "~/libs/tasks/queueTask";
|
||||
import { maybeUpdateLastConnectedAt } from "~/middleware/maybeUpdateLastConnectedAt";
|
||||
|
||||
import { checkUpcomingTitles } from "./controllers/internal/upcoming-titles";
|
||||
|
||||
export const app = new OpenAPIHono<{ Bindings: Env }>();
|
||||
|
||||
app.use(maybeUpdateLastConnectedAt);
|
||||
@@ -121,9 +123,20 @@ export default {
|
||||
});
|
||||
},
|
||||
async scheduled(event, env, ctx) {
|
||||
const { processDelayedTasks } =
|
||||
await import("~/libs/tasks/processDelayedTasks");
|
||||
await processDelayedTasks(env);
|
||||
switch (event.cron) {
|
||||
case "0 */12 * * *":
|
||||
const { processDelayedTasks } =
|
||||
await import("~/libs/tasks/processDelayedTasks");
|
||||
await processDelayedTasks(env);
|
||||
break;
|
||||
case "0 18 * * *":
|
||||
const { checkUpcomingTitles } =
|
||||
await import("~/controllers/internal/upcoming-titles");
|
||||
await checkUpcomingTitles();
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unhandled cron: ${event.cron}`);
|
||||
}
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
|
||||
|
||||
@@ -3,11 +3,13 @@ import mapKeys from "lodash.mapkeys";
|
||||
|
||||
import { Case, changeStringCase } from "../changeStringCase";
|
||||
|
||||
export function getAdminSdkCredentials(env: Cloudflare.Env = cloudflareEnv) {
|
||||
export function getAdminSdkCredentials(
|
||||
env: Cloudflare.Env = cloudflareEnv,
|
||||
): AdminSdkCredentials {
|
||||
return mapKeys(
|
||||
JSON.parse(env.ADMIN_SDK_JSON) as AdminSdkCredentials,
|
||||
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
|
||||
);
|
||||
) satisfies AdminSdkCredentials;
|
||||
}
|
||||
|
||||
export interface AdminSdkCredentials {
|
||||
|
||||
@@ -67,7 +67,7 @@ id = "c8db249d8ee7462b91f9c374321776e4"
|
||||
preview_id = "ff38240eb2aa4b1388c705f4974f5aec"
|
||||
|
||||
[triggers]
|
||||
crons = ["0 */12 * * *"]
|
||||
crons = ["0 */12 * * *", "0 18 * * *"]
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "DB"
|
||||
|
||||
Reference in New Issue
Block a user