From daea0c9cfb7fe42bd13519b4afa14a3945f81413 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 4 Feb 2024 11:45:59 +0200 Subject: [PATCH] invoke cron every min --- dist/index.js | 15 +++++++++++---- package-lock.json | 27 +++++++++++++++++++++++++++ package.json | 2 ++ src/index.ts | 16 ++++++++++++---- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index 17fefd1..9d9a06e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const GameSource_1 = __importDefault(require("./GameSource")); const GoogleCalendar_1 = __importDefault(require("./GoogleCalendar")); const dotenv_1 = __importDefault(require("dotenv")); +const node_cron_1 = __importDefault(require("node-cron")); dotenv_1.default.config(); class App { constructor() { @@ -13,7 +14,7 @@ class App { this.googleCalendar = new GoogleCalendar_1.default(); } async startCronJob() { - console.log("START CRON JOB"); + console.log("START Haifa Reminder"); const newGamesAdded = []; await this.googleCalendar.init(); try { @@ -43,7 +44,13 @@ class App { } } const app = new App(); -app.startCronJob().catch((error) => { - console.error("Error in app:", error.message); +node_cron_1.default.schedule('* * * * *', () => { + console.log('Running startCronJob at 7:00 AM Jerusalem time'); + console.log("START CRON JOB"); + app.startCronJob().catch((error) => { + console.error("Error in scheduled cron job:", error.message); + }); +}, { + scheduled: true, + timezone: "Asia/Jerusalem" }); -// app.startWebServer(); diff --git a/package-lock.json b/package-lock.json index ba2d4aa..55ec18c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,14 @@ "googleapis": "^122.0.0", "moment": "^2.29.4", "moment-timezone": "^0.5.43", + "node-cron": "^3.0.3", "node-html-parser": "^6.1.5", "uuid": "^9.0.0" }, "devDependencies": { "@types/express": "^4.17.17", "@types/node": "^18.15.5", + "@types/node-cron": "^3.0.11", "typescript": "^5.0.3" } }, @@ -73,6 +75,12 @@ "integrity": "sha512-Ark2WDjjZO7GmvsyFFf81MXuGTA/d6oP38anyxWOL6EREyBKAxKoFHwBhaZxCfLRLpO8JgVXwqOwSwa7jRcjew==", "dev": true }, + "node_modules/@types/node-cron": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz", + "integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==", + "dev": true + }, "node_modules/@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", @@ -611,6 +619,25 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/node-cron/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/node-fetch": { "version": "2.6.12", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", diff --git a/package.json b/package.json index 598e856..f063fa1 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "googleapis": "^122.0.0", "moment": "^2.29.4", "moment-timezone": "^0.5.43", + "node-cron": "^3.0.3", "node-html-parser": "^6.1.5", "uuid": "^9.0.0" }, @@ -16,6 +17,7 @@ "devDependencies": { "@types/express": "^4.17.17", "@types/node": "^18.15.5", + "@types/node-cron": "^3.0.11", "typescript": "^5.0.3" } } diff --git a/src/index.ts b/src/index.ts index d22b81c..2eb40cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import GameSource from "./GameSource"; import GoogleCalendar from "./GoogleCalendar"; import env from "dotenv"; +import cron from 'node-cron'; env.config(); @@ -15,7 +16,7 @@ class App { } async startCronJob() { - console.log("START CRON JOB"); + console.log("START Haifa Reminder"); const newGamesAdded = []; await this.googleCalendar.init(); try { @@ -50,7 +51,14 @@ class App { const app = new App(); -app.startCronJob().catch((error) => { - console.error("Error in app:", error.message); + +cron.schedule('* * * * *', () => { + console.log('Running startCronJob at 7:00 AM Jerusalem time'); + console.log("START CRON JOB"); + app.startCronJob().catch((error) => { + console.error("Error in scheduled cron job:", error.message); + }); +}, { + scheduled: true, + timezone: "Asia/Jerusalem" }); -// app.startWebServer();