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

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-05-10 11:04:06 +02:00
"use strict"
const cron = require('cron');
const async = require('async');
const UserModel = require('../models/user');
const signatureTool = require('../signature-tool/signature-tool');
// Execute daily @ 02:30 AM
2017-05-10 15:02:58 +02:00
const cronJob = cron.job('00 30 02 * * *', () => {
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');
})
});
});
module.exports = cronJob;