From 240a7be7f13b9414604863f58d42e7eb28b5bf8b Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Thu, 8 May 2025 00:53:33 +0200 Subject: [PATCH] - Cron Completato --- src/server/controllers/articleController.js | 1 - src/server/models/cron.js | 117 +++++++++++--------- src/server/modules/CronMod.js | 6 +- src/server/modules/Macro.js | 2 +- src/server/version.txt | 2 +- 5 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/server/controllers/articleController.js b/src/server/controllers/articleController.js index 73d6f05..c54d43d 100644 --- a/src/server/controllers/articleController.js +++ b/src/server/controllers/articleController.js @@ -933,7 +933,6 @@ exports.updateAllBook = async (idapp, options) => { try { const macro = new Macro(idapp); // Crea un'istanza della classe Macro options.idapp = idapp; - return await macro.updateLocalDbFromGM_T_Web_Articoli(options); } catch (e) { diff --git a/src/server/models/cron.js b/src/server/models/cron.js index 5bb325d..e93a9c6 100755 --- a/src/server/models/cron.js +++ b/src/server/models/cron.js @@ -78,67 +78,78 @@ CronSchema.statics.executeQueryTable = function (idapp, params, user) { CronSchema.statics.startJobCron = async function (idapp) { // Esegui un loop su tutti i cron job che trovi per l'idapp specificato const Cron = this; - const cronJobs = await Cron.find({ idapp, active: true }); + try { + const cronJobs = await Cron.find({ idapp, active: true }); - // console.log('Check startJobCron...', idapp); + // console.log('Check startJobCron...', idapp); - const mycronMod = new CronMod(); + const mycronMod = new CronMod(); - const currentDate = new Date(); + const currentDate = new Date(); - for (const mycron of cronJobs) { - const jobTime = new Date(); - const [hours, minutes] = mycron.startTime.split(':'); - jobTime.setHours(hours, minutes, 0, 0); + for (const mycron of cronJobs) { + const jobTime = new Date(); + const [hours, minutes] = mycron.startTime.split(':'); + jobTime.setHours(hours, minutes, 0, 0); - // Check if jobTime has passed and if 'everyXMinutes' have elapsed since last execution - if (!mycron.quanteVolteEseguito) mycron.quanteVolteEseguito = 0; - const timesPerDay = mycron.quanteVolteEseguiAlGG || 1; - const startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()); - const lastJobDate = new Date( - mycron.lastJobStarted.getFullYear(), - mycron.lastJobStarted.getMonth(), - mycron.lastJobStarted.getDate() - ); - const tempooltre = currentDate - mycron.lastJobStarted >= mycron.everyXMinutes * 60 * 1000; - if ( - ((mycron.quanteVolteEseguito < timesPerDay && startDate.getTime() === lastJobDate.getTime()) || - startDate.getTime() !== lastJobDate.getTime()) && - (!mycron.lastJobStarted || tempooltre) - ) { - if (currentDate >= jobTime) { - // Execute the function at the scheduled time - console.log(`Sto eseguendo il Cron Job: ${mycron.nomeFunzioneDbOp} alle ${currentDate.toLocaleTimeString()}`); - const status = shared_consts.STATUS_JOB.START; - if (startDate.getTime() !== lastJobDate.getTime()) { - mycron.quanteVolteEseguito = 0; - } - const quanteVolteEseguito = mycron.quanteVolteEseguito + 1; - await Cron.findOneAndUpdate( - { _id: mycron._id }, - { $set: { lastJobStarted: currentDate, status, quanteVolteEseguito } }, - { new: true } + // Check if jobTime has passed and if 'everyXMinutes' have elapsed since last execution + if (!mycron.quanteVolteEseguito) mycron.quanteVolteEseguito = 0; + const timesPerDay = mycron.quanteVolteEseguiAlGG || 1; + const startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()); + let lastJobDate = null; + if (mycron.lastJobStarted) { + lastJobDate = new Date( + mycron.lastJobStarted.getFullYear(), + mycron.lastJobStarted.getMonth(), + mycron.lastJobStarted.getDate() ); - mycronMod - .eseguiDbOp(idapp, { dbop: mycron.nomeFunzioneDbOp }, null, null) - .then(async (ris) => { - console.log(`${currentDate.toLocaleTimeString()} LOG JOB: ${ris.mystr}`); - const myid = mycron._id; - const status = shared_consts.STATUS_JOB.FINISH; - const risupdate = await Cron.findOneAndUpdate( - { _id: myid }, - { $set: { lastJobEnd: new Date(), status } }, - { new: true } - ); - }) - .catch(async (err) => { - const log = "Errore durante l'esecuzione del job: " + err.message || err; - const status = shared_consts.STATUS_JOB.ERR; - await Cron.findOneAndUpdate({ _id: mycron._id }, { $set: { log, status } }, { new: true }); - console.error(log); - }); + } + + let tempooltre = currentDate - mycron.lastJobStarted >= mycron.everyXMinutes * 60 * 1000; + const canExecute = + (!mycron.lastJobStarted || // se non c'è un ultimo eseguito, esegui + tempooltre) && // se è passato il tempo di attesa, esegui + (mycron.lastJobStarted && (mycron.quanteVolteEseguito < timesPerDay) || // se non ho ancora raggiunto il numero di esecuzioni al giorno + (!lastJobDate || (startDate.getTime() !== lastJobDate.getTime()))); // se non è lo stesso giorno, esegui + if (canExecute) { + if (currentDate >= jobTime) { + // Execute the function at the scheduled time + console.log(`Sto eseguendo il Cron Job: ${mycron.nomeFunzioneDbOp} alle ${currentDate.toLocaleTimeString()}`); + const status = shared_consts.STATUS_JOB.START; + if (!lastJobDate || startDate.getTime() !== lastJobDate.getTime()) { + mycron.quanteVolteEseguito = 0; + } + + const quanteVolteEseguito = mycron.quanteVolteEseguito + 1; + await Cron.findOneAndUpdate( + { _id: mycron._id }, + { $set: { lastJobStarted: currentDate, status, quanteVolteEseguito } }, + { new: true } + ); + mycronMod + .eseguiDbOp(idapp, { dbop: mycron.nomeFunzioneDbOp }, null, null) + .then(async (ris) => { + console.log(`${currentDate.toLocaleTimeString()} LOG JOB: ${ris.mystr}`); + const myid = mycron._id; + const status = shared_consts.STATUS_JOB.FINISH; + const risupdate = await Cron.findOneAndUpdate( + { _id: myid }, + { $set: { lastJobEnd: new Date(), status } }, + { new: true } + ); + }) + .catch(async (err) => { + const log = "Errore durante l'esecuzione del job: " + err.message || err; + const status = shared_consts.STATUS_JOB.ERR; + await Cron.findOneAndUpdate({ _id: mycron._id }, { $set: { log, status } }, { new: true }); + console.error(log); + }); + } } } + } catch (e) { + console.error('Error startJobCron:', e); + return false; } }; diff --git a/src/server/modules/CronMod.js b/src/server/modules/CronMod.js index 2f77ee8..0fd53f8 100644 --- a/src/server/modules/CronMod.js +++ b/src/server/modules/CronMod.js @@ -38,9 +38,10 @@ class CronMod { async eseguiDbOp(idapp, mydata, req, res) { - let ris = await User.DbOp(idapp, mydata); + mydata.idapp = idapp; + const populate = require("../populate/populate"); const globalTables = require("../tools/globalTables"); @@ -77,6 +78,9 @@ class CronMod { const { updateAllBook } = require("../controllers/articleController"); + console.log('updateAllBooksAndRemoveCanc...'); + + mystr = await updateAllBook(idapp, {usaDBGMLocale: false, caricatutti: true, rimuovieventualiCancellati: true}); ris = { mystr }; diff --git a/src/server/modules/Macro.js b/src/server/modules/Macro.js index d52213e..b850771 100644 --- a/src/server/modules/Macro.js +++ b/src/server/modules/Macro.js @@ -56,7 +56,7 @@ class Macro { const lavoromassivo = options.caricatutti; if (lavoromassivo) { - myjob = await JobsInProgress.addNewJob({ idapp, descr: 'Riaggiorna Articoli', nomeFunzioneDbOp: 'updateAllBook', status: shared_consts.STATUS_JOB.START }); + myjob = await JobsInProgress.addNewJob({ idapp: options.idapp, descr: 'Riaggiorna Articoli', nomeFunzioneDbOp: 'updateAllBook', status: shared_consts.STATUS_JOB.START }); if (!myjob) { mylog = 'ATTENZIONE! ❌ STAVO GIA ESEGUENDO QUESTO JOB, quindi ESCO !'; console.error(mylog); diff --git a/src/server/version.txt b/src/server/version.txt index adf1ebc..84da421 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.38 \ No newline at end of file +1.2.39 \ No newline at end of file