2026-08-11 23:07:26 +08:00
|
|
|
import cron, { type ScheduledTask } from "node-cron";
|
|
|
|
|
import { backfillAccountProfiles } from "./account-enrichment-service";
|
|
|
|
|
import { runScheduledCollections } from "./collection-service";
|
|
|
|
|
import { withDatabaseLock } from "./database";
|
|
|
|
|
import {
|
|
|
|
|
resolveCollectionMcpConfig,
|
|
|
|
|
type CollectionMcpBindings,
|
|
|
|
|
} from "./mcp-collection-client";
|
|
|
|
|
import { ensureSchema, getRawDb } from "./mvp-db";
|
|
|
|
|
import { getRuntimeEnv, isEnabled } from "./runtime-env";
|
2026-08-18 16:39:59 +08:00
|
|
|
import {
|
|
|
|
|
resolveWecomConfig,
|
|
|
|
|
type WecomBindings,
|
|
|
|
|
} from "./wecom-client";
|
|
|
|
|
import { runDueSoonWecomNotifications } from "./wecom-notifier-service";
|
2026-08-11 23:07:26 +08:00
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
|
var __kocLoopScheduler: ScheduledTask | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function runDailyJob() {
|
|
|
|
|
await withDatabaseLock("koc-loop-daily-collection", 0, async () => {
|
|
|
|
|
await ensureSchema();
|
|
|
|
|
const db = getRawDb();
|
2026-08-18 16:39:59 +08:00
|
|
|
const env = getRuntimeEnv();
|
2026-08-11 23:07:26 +08:00
|
|
|
const config = resolveCollectionMcpConfig(
|
2026-08-18 16:39:59 +08:00
|
|
|
env as unknown as CollectionMcpBindings,
|
2026-08-11 23:07:26 +08:00
|
|
|
);
|
|
|
|
|
const collections = await runScheduledCollections(db, Date.now(), config);
|
|
|
|
|
const accounts = await backfillAccountProfiles(db, config, 10);
|
2026-08-18 16:39:59 +08:00
|
|
|
let wecom: Awaited<ReturnType<typeof runDueSoonWecomNotifications>> | null =
|
|
|
|
|
null;
|
|
|
|
|
if (isEnabled(env.WECOM_NOTIFY_ENABLED, true)) {
|
|
|
|
|
const wecomConfig = resolveWecomConfig(env as unknown as WecomBindings);
|
|
|
|
|
if (
|
|
|
|
|
wecomConfig.robotWebhook ||
|
|
|
|
|
(wecomConfig.corpId && wecomConfig.agentId && wecomConfig.secret)
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
wecom = await runDueSoonWecomNotifications(db, wecomConfig);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("[KOC LOOP] wecom notify failed", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-11 23:07:26 +08:00
|
|
|
console.info("[KOC LOOP] daily scheduler completed", {
|
|
|
|
|
collections,
|
|
|
|
|
accounts,
|
2026-08-18 16:39:59 +08:00
|
|
|
wecom,
|
2026-08-11 23:07:26 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startScheduler() {
|
|
|
|
|
if (!isEnabled(getRuntimeEnv().ENABLE_SCHEDULER, true)) return;
|
|
|
|
|
if (globalThis.__kocLoopScheduler) return;
|
|
|
|
|
globalThis.__kocLoopScheduler = cron.schedule(
|
2026-08-12 11:12:23 +08:00
|
|
|
"0 9 * * *",
|
2026-08-11 23:07:26 +08:00
|
|
|
() => void runDailyJob().catch((error) => {
|
|
|
|
|
console.error("[KOC LOOP] daily scheduler failed", error);
|
|
|
|
|
}),
|
|
|
|
|
{ timezone: "Asia/Shanghai", noOverlap: true },
|
|
|
|
|
);
|
2026-08-12 11:12:23 +08:00
|
|
|
console.info("[KOC LOOP] scheduler enabled at 09:00 Asia/Shanghai");
|
2026-08-11 23:07:26 +08:00
|
|
|
}
|