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

70 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2017-07-23 11:07:47 +02: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');
const signatureTool = require('../signature-tool/signature-tool');
2017-06-10 22:07:32 +02:00
const createAllSignatures = () => {
2017-05-10 11:04:06 +02:00
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - UPDATE SIGNATURES');
// mock response
const res = {
locals: {
items: {}
}
};
// 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'))) {
callback()
} else {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err)
}
};
signatureTool(user._id, res, next)
}, () => {
if (err) {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err)
}
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': finished successful - UPDATE SIGNATURES');
})
});
2017-06-10 22:07:32 +02:00
};
2017-07-15 12:03:38 +02:00
const createBackup = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - CREATE BACKUP');
exec(__dirname + '/../../backup/backup.sh', (error, stdout, stderr) => {
if (error) {
console.log(error);
}
console.log('\x1b[32m%s\x1b[0m',stderr);
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job finished - CREATE BACKUP');
})
};
2017-06-10 22:07:32 +02:00
// Execute daily @ 02:30 AM
2017-07-15 12:03:38 +02:00
const cronJobSignature = cron.job('00 30 02 * * *', createAllSignatures);
2017-07-27 16:38:35 +02:00
// Execute every 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,
2017-06-10 22:07:32 +02:00
createAllSignatures: createAllSignatures
};