ggs-cron/app.js
Ittipat Lusuk 296c006fd9
All checks were successful
CI for GGS-CRON / build-docker (push) Successful in 1m20s
CI for GGS-CRON / build-and-test (push) Successful in 3m14s
Add route, Update workflow
2025-08-07 14:54:19 +07:00

130 lines
3.4 KiB
JavaScript

var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
// var morgan = require("morgan");
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var healthRouter = require("./routes/health")
// const { createLogger, format, transports } = require('winston');
const {
logger,
Log,
getTestSpreadSheet,
GoogleFunctions,
PluginsManager,
} = require("./lib/common");
const { SyncText } = require("./lib/sync_text");
const { CronJobs } = require("./cron-jobs");
const { google } = require("googleapis");
const { EventEmitter } = require("stream");
require("dotenv").config();
Log.debug(process.env.TEST_SHEET_ID);
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
app
// .use(require("morgan")("combined", { stream: logger.stream }))
.use(express.json())
.use(express.urlencoded({ extended: false }))
.use(cookieParser())
.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/health", healthRouter);
Log.debug(`running on port ${process.env.PORT}`);
// =========================================================================
// CRON JOB (Trigger)
// =========================================================================
var cronTasks =
CronJobs.MAXIMUM_CRON_JOBS > 0 ? new Array(CronJobs.MAXIMUM_CRON_JOBS) : [];
const auth = GoogleFunctions.auth();
const sheet = GoogleFunctions.SpreadSheets(auth);
// let heartbeatTask = CronJobs.doEveryMinute(() => {
// Log.debug(`All running => ${JSON.stringify(cronTasks)}`);
// cronTasks[0].exec_times += 1;
// if(cronTasks[0].exec_times >= 1){
// Log.debug("stop hb");
// this.emit('stop');
// }
// }, 'hb');
// heartbeatTask.on('stop', () => heartbeatTask.stop());
// cronTasks[0] = {
// task: heartbeatTask,
// exec_times: 0
// };
// getTestSpreadSheet(sheet);
// const tha_sheets = GoogleFunctions.getAllSheetNamesByCountry(sheet, "tha", (sheetDetail) => {
// return sheetDetail.data.sheets.map((sheet) => {
// return {
// name: sheet.properties.title,
// id: sheet.properties.sheetId,
// };
// });
// });
// GoogleFunctions.getCountrySheetByName(sheet, "tha", "PriceSlot1");
// GoogleFunctions.getPriceSlotValues(sheet, "tha");
// v3 !!!!
GoogleFunctions.syncProfilePrice(sheet, "tha", false);
//
// Test Taobin SyncText
// SyncText.run(sheet, "uae", false);
const pm = new PluginsManager(cronTasks, CronJobs);
pm.load();
pm.checkScript();
// pm.runScript("heartbeat.js");
// Expect variable from plugins
let endpointMap;
// Object.keys(pm.scripts).forEach((scriptName) => {
// });
for (let scriptName of Object.keys(pm.scripts)) {
Log.debug(`running ${scriptName}`);
eval(pm.scripts[scriptName]);
pm.createEndpoint(app, express, scriptName, endpointMap);
endpointMap = undefined; // reset
}
// pm.loop()
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;