From d4fdbad2415129575adba34137016dbea69dc5d6 Mon Sep 17 00:00:00 2001 From: Ovler Date: Tue, 24 Dec 2024 10:21:39 +0800 Subject: [PATCH] feat: add cron job for processing video tasks and update Deno configuration --- deno.json | 8 ++++++++ main.ts | 8 +++++++- src/index.ts | 8 ++++---- tsconfig.json | 4 +++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/deno.json b/deno.json index f0f5859..cb5b23c 100644 --- a/deno.json +++ b/deno.json @@ -8,5 +8,13 @@ "compilerOptions": { "jsx": "precompile", "jsxImportSource": "hono/jsx" + }, + "deploy": { + "project": "f4b70966-04b4-4b53-9110-1f64c556bc5c", + "exclude": [ + "**/node_modules" + ], + "include": [], + "entrypoint": "main.ts" } } \ No newline at end of file diff --git a/main.ts b/main.ts index 88d7d48..281795c 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { app } from './src/index.ts' +import { app, processVideoTasks } from './src/index.ts' // const app = new Hono() @@ -7,3 +7,9 @@ app.get('/', (c) => { }) Deno.serve({ port: 8787 },app.fetch) + +Deno.cron("sample cron", "* * * * *", async () => { + console.log("cron job running every minute"); + const result = await processVideoTasks(); + console.log("Processed video tasks:", result); +}); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 46a622c..a361358 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,9 @@ async function fetchTasks(apibase: string): Promise { .filter((aid) => typeof aid === "number") as number[]; } -async function processVideoTasks(apibase: string, aids: number[]) { +export async function processVideoTasks() { + const apibase = getAPIBASE(); + const aids = await fetchTasks(apibase); console.log(`Starting task with ${aids.length} videos`); const data: BiliResponse = (await batchGetVideoInfo(aids)) as BiliResponse; @@ -122,10 +124,8 @@ app.get("/gettasks", async (c) => { }); app.get("/starttask", async (c) => { - const apibase = getAPIBASE(); try { - const aids = await fetchTasks(apibase); - const result = await processVideoTasks(apibase, aids); + const result = await processVideoTasks(); return c.json(result); } catch (error: any) { return c.json({ error: error.message }, 500); diff --git a/tsconfig.json b/tsconfig.json index 934f03c..2089e0d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,8 @@ "@cloudflare/workers-types/2023-07-01" ], "jsx": "react-jsx", - "jsxImportSource": "hono/jsx" + "jsxImportSource": "hono/jsx", + "allowImportingTsExtensions": true, + "noEmit": true }, } \ No newline at end of file