- Aggiunta della funzione Cron
- Flag: Pubblicati OnLine + Solo CagalogoGenerale + Dettagli
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
const mongoose = require('mongoose').set('debug', false)
|
||||
const mongoose = require('mongoose').set('debug', false);
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = 'F';
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
mongoose.plugin((schema) => {
|
||||
schema.options.usePushEach = true;
|
||||
});
|
||||
|
||||
const CronMod = require('../modules/CronMod');
|
||||
|
||||
const CronSchema = new Schema({
|
||||
idapp: {
|
||||
@@ -27,26 +29,45 @@ const CronSchema = new Schema({
|
||||
nomeFunzioneDbOp: {
|
||||
type: String,
|
||||
},
|
||||
startTime: { // Orario iniziale (es. "08:30")
|
||||
startTime: {
|
||||
// Orario iniziale (es. "08:30")
|
||||
type: String,
|
||||
default: "00:00"
|
||||
},
|
||||
everyXHours: { // Esegui ogni X ore
|
||||
everyXMinutes: {
|
||||
// Esegui ogni X ore
|
||||
type: Number,
|
||||
},
|
||||
quanteVolteEseguito: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
quanteVolteEseguiAlGG: {
|
||||
type: Number,
|
||||
default: 1,
|
||||
},
|
||||
lastJobStarted: {
|
||||
type: Date,
|
||||
},
|
||||
lastJobEnd: {
|
||||
type: Date,
|
||||
},
|
||||
log: {
|
||||
type: String,
|
||||
},
|
||||
status: {
|
||||
type: Number,
|
||||
default: 24
|
||||
},
|
||||
date_created: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
default: Date.now,
|
||||
},
|
||||
date_updated: {
|
||||
type: Date,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
CronSchema.statics.getFieldsForSearch = function () {
|
||||
return [{ field: 'descr', type: tools.FieldType.string }]
|
||||
return [{ field: 'descr', type: tools.FieldType.string }];
|
||||
};
|
||||
|
||||
CronSchema.statics.executeQueryTable = function (idapp, params, user) {
|
||||
@@ -54,6 +75,73 @@ CronSchema.statics.executeQueryTable = function (idapp, params, user) {
|
||||
return tools.executeQueryTable(this, 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 });
|
||||
|
||||
// console.log('Check startJobCron...', idapp);
|
||||
|
||||
const mycronMod = new CronMod();
|
||||
|
||||
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);
|
||||
|
||||
// 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 }
|
||||
);
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CronSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const Cron = this;
|
||||
|
||||
@@ -61,7 +149,6 @@ CronSchema.statics.findAllIdApp = async function (idapp) {
|
||||
return await Cron.find({ idapp }).then((arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error('Errore: ', err);
|
||||
}
|
||||
@@ -70,8 +157,9 @@ CronSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const Cron = mongoose.model('Cron', CronSchema);
|
||||
|
||||
Cron.createIndexes()
|
||||
.then(() => { })
|
||||
.catch((err) => { throw err; });
|
||||
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
module.exports = { Cron };
|
||||
|
||||
@@ -532,7 +532,7 @@ module.exports.removeProductInfoWithoutDateUpdatedFromGM = async function (idapp
|
||||
try {
|
||||
const arrproductInfo = await ProductInfo.find({ idapp, date_updated_fromGM: { $exists: false } });
|
||||
|
||||
if (arrproductInfo.length > 0) {
|
||||
if (arrproductInfo.length > 0 && arrproductInfo.length < 1000) {
|
||||
mylog = `Rimuovo ${arrproductInfo.length} productInfo senza date_updated_fromGM !!`
|
||||
console.log(mylog);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user