Compare commits

..

7 Commits

7 changed files with 35 additions and 16 deletions

View File

@@ -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);

View File

@@ -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);
}); });

View File

@@ -51,7 +51,7 @@ describe('requests the "/title" route', () => {
headers: new Headers({ "x-anilist-token": "asd" }), headers: new Headers({ "x-anilist-token": "asd" }),
}); });
expect(await response.json()).toMatchSnapshot(); await expect(response.json()).resolves.toMatchSnapshot();
expect(response.status).toBe(200); expect(response.status).toBe(200);
}); });
@@ -63,7 +63,7 @@ describe('requests the "/title" route', () => {
const response = await app.request("/title?id=10"); const response = await app.request("/title?id=10");
expect(await response.json()).toMatchSnapshot(); await expect(response.json()).resolves.toMatchSnapshot();
expect(response.status).toBe(200); expect(response.status).toBe(200);
}); });
@@ -75,7 +75,7 @@ describe('requests the "/title" route', () => {
const response = await app.request("/title?id=-1"); const response = await app.request("/title?id=-1");
expect(await response.json()).toEqual({ success: false }); await expect(response.json()).resolves.toEqual({ success: false });
expect(response.status).toBe(404); expect(response.status).toBe(404);
}); });
}); });

View File

@@ -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>;

View File

@@ -43,7 +43,7 @@ export class AnilistDurableObject extends DurableObject {
async getTitle( async getTitle(
id: number, id: number,
userId?: string, userId?: number,
token?: string, token?: string,
): Promise<Title | null> { ): Promise<Title | null> {
const promises: Promise<any>[] = [ const promises: Promise<any>[] = [
@@ -135,8 +135,7 @@ export class AnilistDurableObject extends DurableObject {
nextSeason, nextSeason,
nextYear, nextYear,
limit, limit,
page, });
}).then((data) => data?.Page);
}, },
DateTime.now().plus({ days: 1 }), DateTime.now().plus({ days: 1 }),
); );
@@ -155,7 +154,8 @@ export class AnilistDurableObject extends DurableObject {
nextSeason, nextSeason,
nextYear, nextYear,
limit, limit,
}); page,
}).then((data) => data?.Page);
}, },
DateTime.now().plus({ days: 1 }), DateTime.now().plus({ days: 1 }),
); );

View File

@@ -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 {

View File

@@ -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"