haifa-reminder/dist/index.js

70 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-08-06 08:25:52 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const GameSource_1 = __importDefault(require("./GameSource"));
const GoogleCalendar_1 = __importDefault(require("./GoogleCalendar"));
const dotenv_1 = __importDefault(require("dotenv"));
2024-02-04 09:45:59 +00:00
const node_cron_1 = __importDefault(require("node-cron"));
2024-02-04 10:52:15 +00:00
const fs_1 = __importDefault(require("fs")); // Importing fs for logging
2023-08-06 08:25:52 +00:00
dotenv_1.default.config();
class App {
constructor() {
this.gameSource = new GameSource_1.default();
this.googleCalendar = new GoogleCalendar_1.default();
}
async startCronJob() {
2024-02-04 10:52:15 +00:00
this.writeLog('START CRON JOB'); // Log when the cron job starts
2024-02-04 09:45:59 +00:00
console.log("START Haifa Reminder");
2023-08-06 08:25:52 +00:00
const newGamesAdded = [];
await this.googleCalendar.init();
try {
2024-07-31 18:53:04 +00:00
const games = await this.gameSource.getGamesFromHaifa(this.writeLog);
2023-08-06 08:25:52 +00:00
for (const game of games) {
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(game.start.dateTime, game.end.dateTime, game.summary);
console.log(game);
2023-08-06 08:25:52 +00:00
if (!isDuplicateEvent) {
newGamesAdded.push(game);
console.log("Event does not exist");
await this.googleCalendar.updateNewEvent([game]);
}
else {
console.log("Event already exists");
}
}
if (newGamesAdded.length > 0) {
console.log("New games added:", newGamesAdded);
}
else {
2024-02-04 10:52:15 +00:00
console.log("No new games were Added!");
2023-08-06 08:25:52 +00:00
}
2024-02-04 11:38:35 +00:00
this.writeLog('Successfully ran project');
2023-08-06 08:25:52 +00:00
}
catch (error) {
2024-02-04 11:38:35 +00:00
this.writeLog("Error in cron job:" + error.message);
2023-08-06 08:25:52 +00:00
}
2024-02-04 10:52:15 +00:00
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
2023-08-06 08:25:52 +00:00
}
}
const app = new App();
2024-02-04 13:31:58 +00:00
node_cron_1.default.schedule('* * * * *', () => {
console.log('Running startCronJob at 10:00 AM Jerusalem time');
2024-02-04 09:45:59 +00:00
app.startCronJob().catch((error) => {
console.error("Error in scheduled cron job:", error.message);
2024-02-04 10:52:15 +00:00
app.writeLog(`ERROR: ${error.message}`); // Log any errors
2024-02-04 09:45:59 +00:00
});
}, {
scheduled: true,
timezone: "Asia/Jerusalem"
2023-08-06 08:25:52 +00:00
});