opt-cc/server/cron-job/cron.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-05-10 11:04:06 +02:00
const cron = require('cron');
const async = require('async');
2017-07-15 12:03:38 +02:00
const {exec} = require('child_process');
2017-05-10 11:04:06 +02:00
const UserModel = require('../models/user');
2017-10-20 23:42:41 +02:00
const signatureTool = require('../tools/signature-tool');
2017-05-10 11:04:06 +02:00
2018-02-04 15:58:28 +01:00
const debug = require('debug');
const error = debug('cc:cronjob:err');
const logger = debug('cc:cronjob');
logger.log = console.log.bind(console);
2017-06-10 22:07:32 +02:00
const createAllSignatures = () => {
2018-02-04 15:58:28 +01:00
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
2017-05-10 11:04:06 +02:00
+ ': cron job started - UPDATE SIGNATURES');
// mock response
const res = {
locals: {
2018-03-12 09:26:44 +01:00
items: {},
},
2017-05-10 11:04:06 +02:00
};
// re-create signature image for each active user
UserModel.find({}, (err, users) => {
async.eachSeries(users, (user, callback) => {
// mock next to execute callback
const next = (err) => {
if (!err || (err && err.message.startsWith('Fraction not defined'))) {
2018-03-12 09:26:44 +01:00
callback();
2017-05-10 11:04:06 +02:00
} else {
2018-02-04 15:58:28 +01:00
error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
2018-03-12 09:26:44 +01:00
+ ': Error in execution - UPDATE SIGNATURES: ' + err.message);
2017-05-10 11:04:06 +02:00
}
};
2018-03-12 09:26:44 +01:00
signatureTool(user._id, res, next);
2017-05-10 11:04:06 +02:00
}, () => {
if (err) {
2018-02-04 15:58:28 +01:00
error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
2018-03-12 09:26:44 +01:00
+ ': Error in execution - UPDATE SIGNATURES: ' + err.message);
2017-05-10 11:04:06 +02:00
}
2018-02-04 15:58:28 +01:00
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
2017-05-10 11:04:06 +02:00
+ ': finished successful - UPDATE SIGNATURES');
2018-03-12 09:26:44 +01:00
});
2017-05-10 11:04:06 +02:00
});
2017-06-10 22:07:32 +02:00
};
2017-07-15 12:03:38 +02:00
const createBackup = () => {
2018-02-04 15:58:28 +01:00
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
2017-07-15 12:03:38 +02:00
+ ': cron job started - CREATE BACKUP');
2018-02-04 15:58:28 +01:00
exec(__dirname + '/../../backup/backup.sh', (err, stdout, stderr) => {
if (err) {
error(err.message);
2017-07-15 12:03:38 +02:00
}
2018-02-26 09:04:27 +01:00
logger('\x1b[32m%s\x1b[0m', stderr);
2018-02-04 15:58:28 +01:00
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
2017-07-15 12:03:38 +02:00
+ ': cron job finished - CREATE BACKUP');
2018-03-12 09:26:44 +01:00
});
2017-07-15 12:03:38 +02:00
};
2017-06-10 22:07:32 +02:00
// Execute daily @ 02:30 AM
const cronJobSignature = cron.job('00 30 02 * * *', createAllSignatures);
2017-07-15 12:03:38 +02:00
2017-09-15 21:02:43 +02:00
// Execute on Mon, Thu and Sat @ 04:00 AM
const cronJobBackup = cron.job('00 00 04 * * mon,thu,sat', createBackup);
2017-05-10 11:04:06 +02:00
2017-06-10 22:07:32 +02:00
module.exports = {
2017-07-15 12:03:38 +02:00
cronJobSignature: cronJobSignature,
cronJobBackup: cronJobBackup,
2018-03-12 09:26:44 +01:00
createAllSignatures: createAllSignatures,
2017-06-10 22:07:32 +02:00
};