51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import schedule from "node-schedule";
|
|
import { exec } from "node:child_process";
|
|
|
|
console.log("start ...");
|
|
|
|
async function runTsFile(filePath: string): Promise<void> {
|
|
console.log(`\n🚀 시작됨: ${filePath}`);
|
|
|
|
const proc = Bun.spawn(["bun", "run", filePath], {
|
|
stdout: "inherit", // 자식 프로세스의 출력을 부모 터미널에 실시간으로 보냄
|
|
stderr: "inherit", // 에러 출력도 실시간으로 보냄
|
|
});
|
|
|
|
// 프로세스가 종료될 때까지 await로 대기
|
|
const exitCode = await proc.exited;
|
|
|
|
if (exitCode !== 0) {
|
|
console.error(`❌ 실패: ${filePath} (종료 코드: ${exitCode})`);
|
|
// 실패 시 다음 스크립트를 실행하지 않고 중단하고 싶다면 throw
|
|
throw new Error(`${filePath} 실행 중 오류가 발생했습니다.`);
|
|
}
|
|
|
|
console.log(`✅ 완료: ${filePath}\n`);
|
|
}
|
|
|
|
// 11시에 실행 - fetch-articles -> fetch-detailAddress -> updateRanging -> sendTelegram
|
|
schedule.scheduleJob("0 11 * * *", async () => {
|
|
console.log("Running scheduled jobs at 11 AM");
|
|
try {
|
|
await runTsFile("./src/fetch-articles.ts");
|
|
await runTsFile("./src/fetch-detailAddress.ts");
|
|
await runTsFile("./src/updateRanging.ts");
|
|
await runTsFile("./src/sendTelegram.ts");
|
|
} catch (error) {
|
|
console.error("Error in 11 AM scheduled job:", error);
|
|
}
|
|
});
|
|
|
|
// 16시에 실행 - fetch-articles -> fetch-detailAddress -> updateRanging -> sendTelegram
|
|
schedule.scheduleJob("0 16 * * *", async () => {
|
|
console.log("Running scheduled jobs at 4 PM");
|
|
try {
|
|
await runTsFile("./src/fetch-articles.ts");
|
|
await runTsFile("./src/fetch-detailAddress.ts");
|
|
await runTsFile("./src/updateRanging.ts");
|
|
await runTsFile("./src/sendTelegram.ts");
|
|
} catch (error) {
|
|
console.error("Error in 4 PM scheduled job:", error);
|
|
}
|
|
});
|