From dbdf76554b34d3704f4445c552e0d8bc3e2a8b1e Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 4 Feb 2024 12:52:15 +0200 Subject: [PATCH] remove https --- cron.log | 6 ++++++ dist/GameSource.js | 2 +- dist/index.js | 18 +++++++++++++++--- src/GameSource.ts | 2 +- src/index.ts | 21 ++++++++++++++++----- 5 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 cron.log diff --git a/cron.log b/cron.log new file mode 100644 index 0000000..a7caf7e --- /dev/null +++ b/cron.log @@ -0,0 +1,6 @@ +2024-02-04T10:30:00.949Z - START CRON JOB +2024-02-04T10:30:03.875Z - END CRON JOB +2024-02-04T10:31:00.105Z - START CRON JOB +2024-02-04T10:31:00.825Z - END CRON JOB +2024-02-04T10:52:00.163Z - START CRON JOB +2024-02-04T10:52:03.334Z - END CRON JOB diff --git a/dist/GameSource.js b/dist/GameSource.js index 0772a2d..92bd9e1 100644 --- a/dist/GameSource.js +++ b/dist/GameSource.js @@ -11,7 +11,7 @@ const moment_1 = __importDefault(require("moment")); // require // search for upcomming games class GameSource { async getGamesFromHaifa() { - const sourceUrl = `https://mhaifafc.com/games?lang=en`; + const sourceUrl = `http://mhaifafc.com/games?lang=en`; const result = await axios_1.default.get(sourceUrl); const parsedResult = (0, node_html_parser_1.parse)(result.data.toString()); const gameBoxs = parsedResult.querySelectorAll(".game-box"); diff --git a/dist/index.js b/dist/index.js index 9d9a06e..84863f9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7,6 +7,7 @@ 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")); +const fs_1 = __importDefault(require("fs")); // Importing fs for logging dotenv_1.default.config(); class App { constructor() { @@ -14,11 +15,12 @@ class App { this.googleCalendar = new GoogleCalendar_1.default(); } async startCronJob() { + this.writeLog('START CRON JOB'); // Log when the cron job starts console.log("START Haifa Reminder"); const newGamesAdded = []; await this.googleCalendar.init(); try { - const games = await app.gameSource.getGamesFromHaifa(); + const games = await this.gameSource.getGamesFromHaifa(); for (const game of games) { const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(game.start.dateTime, game.end.dateTime, game.summary); console.log(game); @@ -35,20 +37,30 @@ class App { console.log("New games added:", newGamesAdded); } else { - console.log("No new games was Added!"); + console.log("No new games were Added!"); } } catch (error) { console.error("Error in cron job:", error.message); } + finally { + this.writeLog('END CRON JOB'); // Log when the cron job ends + } + } + writeLog(message) { + const timestamp = new Date().toISOString(); + const logMessage = `${timestamp} - ${message}\n`; + // Write to log file synchronously + fs_1.default.appendFileSync('cron.log', logMessage); + console.log(logMessage); // Optional: also log to console } } const app = new App(); 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); + app.writeLog(`ERROR: ${error.message}`); // Log any errors }); }, { scheduled: true, diff --git a/src/GameSource.ts b/src/GameSource.ts index f7110c0..b68baf1 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -9,7 +9,7 @@ import moment from "moment"; // require export default class GameSource { async getGamesFromHaifa(): Promise { - const sourceUrl = `https://mhaifafc.com/games?lang=en`; + const sourceUrl = `http://mhaifafc.com/games?lang=en`; const result = await axios.get(sourceUrl); const parsedResult = parse(result.data.toString()); const gameBoxs = parsedResult.querySelectorAll(".game-box"); diff --git a/src/index.ts b/src/index.ts index 2eb40cb..e3e2c8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import GameSource from "./GameSource"; import GoogleCalendar from "./GoogleCalendar"; import env from "dotenv"; import cron from 'node-cron'; +import fs from 'fs'; // Importing fs for logging env.config(); @@ -16,11 +17,12 @@ class App { } async startCronJob() { + this.writeLog('START CRON JOB'); // Log when the cron job starts console.log("START Haifa Reminder"); const newGamesAdded = []; await this.googleCalendar.init(); try { - const games = await app.gameSource.getGamesFromHaifa(); + const games = await this.gameSource.getGamesFromHaifa(); for (const game of games) { const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent( @@ -38,25 +40,34 @@ class App { } } - if(newGamesAdded.length > 0) { + if (newGamesAdded.length > 0) { console.log("New games added:", newGamesAdded); } else { - console.log("No new games was Added!"); + console.log("No new games were Added!"); } } catch (error) { console.error("Error in cron job:", error.message); + } finally { + this.writeLog('END CRON JOB'); // Log when the cron job ends } } + + writeLog(message) { + const timestamp = new Date().toISOString(); + const logMessage = `${timestamp} - ${message}\n`; + // Write to log file synchronously + fs.appendFileSync('cron.log', logMessage); + console.log(logMessage); // Optional: also log to console + } } const app = new App(); - 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); + app.writeLog(`ERROR: ${error.message}`); // Log any errors }); }, { scheduled: true,