Skip to content

Commit

Permalink
feat(script): end cron script ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Apr 22, 2020
1 parent bae1c75 commit d44cbaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
2 changes: 1 addition & 1 deletion modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ exports.getApi = async (req, res) => {
exports.getAggregateApi = async (req, res) => {
// TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next();
try {
const data = await ApisService.getAggregateApi(req.api, req.body, req.user);
const data = await ApisService.getAggregateApi(req.api, req.body);
responses.success(res, 'api getData')(data);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
Expand Down
8 changes: 4 additions & 4 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ exports.load = async (api, user) => {
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.workerAuto = async (api, body, user) => {
exports.workerAuto = async (api, body) => {
const start = new Date();
try {
const result = {};
Expand Down Expand Up @@ -239,15 +239,15 @@ exports.getApi = async (api, body) => {
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.getAggregateApi = async (api, body, user) => {
exports.getAggregateApi = async (api, body) => {
let result = await ApisRepository.getAggregateApi(api.slug, body);

if (result.length === 0 && api.autoRequest) {
// check if no data return, then we probably have no data :) ask for it !
this.workerAuto(api, body, user);
this.workerAuto(api, body);
} else if (Date.now() - Date.parse(result[0]._updatedAt) > Date.parse(api.expiration)) {
// check if data but data expired, ask for refresh !
this.workerAuto(api, body, user);
this.workerAuto(api, body);
result = [];
}

Expand Down
46 changes: 14 additions & 32 deletions scripts/crons/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const _ = require('lodash');
const cron = require('node-cron');
const mongoose = require('mongoose');

const config = require(path.resolve('./config'));
const mongooseService = require(path.resolve('./lib/services/mongoose'));
const mails = require(path.resolve('./lib/helpers/mails'));

const cronJobs = async () => {
try {
Expand All @@ -16,40 +14,24 @@ const cronJobs = async () => {
await mongooseService.loadModels();
mongoose.set('debug', false);

const ScrapService = require(path.resolve('./modules/scraps/services/scraps.service'));
let scraps = await ScrapService.list();
scraps = _.remove(scraps, (scrap) => scrap.cron);
console.log(chalk.green(`Scraps cron list requested: ${scraps.length}`));

_.forEach(scraps, (_scrap) => {
if (cron.validate(_scrap.cron)) {
console.log(chalk.blue(`- Init ${_scrap.title} (${_scrap.id}) : ${_scrap.cron}`));

cron.schedule(_scrap.cron, async () => {
console.log(chalk.blue(`- Running ${_scrap.title} (${_scrap.id}) : ${_scrap.cron}`));

const scrap = await ScrapService.get(_scrap.id);
const load = await ScrapService.load(scrap);

if (load.result.type !== 'success' && load.scrap.alert && load.scrap.alert !== '') {
mails.sendMail({
template: 'scrap-failed-alert',
from: config.mailer.from,
to: load.scrap.alert,
subject: `Scrap Failed : ${load.scrap.title}`,
params: {
result: JSON.stringify(load.result, null, 2),
scrapTitle: load.scrap.title,
appName: config.app.title,
appContact: config.app.appContact,
},
});
}
const ApiService = require(path.resolve('./modules/apis/services/apis.service'));
const apis = await ApiService.cron();
console.log(chalk.green(`Apis cron list requested: ${apis.length}`));

_.forEach(apis, (_api) => {
if (cron.validate(_api.cron)) {
console.log(chalk.blue(`- Init ${_api.title} (${_api.id}) : ${_api.cron}`));

cron.schedule(_api.cron, async () => {
console.log(chalk.blue(`- Running ${_api.title} (${_api.id}) : ${_api.cron}`));

const api = await ApiService.get(_api.id);
await ApiService.load(api, 'cron');
});
}
});

console.log(chalk.green(`Scraps cron list Charged: ${scraps.length}`));
console.log(chalk.green(`Apis cron list Charged: ${apis.length}`));
} catch (err) {
console.log(chalk.bold.red(`Error ${err}`));
}
Expand Down

0 comments on commit d44cbaa

Please sign in to comment.