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