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

49 lines
1.4 KiB
JavaScript

"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
// const cronJob = cron.job('00 00 22 * * *', () => {
const cronJob = cron.job('00 00 */1 * * *', () => {
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;