diff --git a/modules/apis/controllers/apis.controller.js b/modules/apis/controllers/apis.controller.js index d817bcb69..7bba295f6 100644 --- a/modules/apis/controllers/apis.controller.js +++ b/modules/apis/controllers/apis.controller.js @@ -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); diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index eee7fb1d1..e40f9db97 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -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 = {}; @@ -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 = []; } diff --git a/scripts/crons/apis.js b/scripts/crons/apis.js index 05bf2067f..22214f7f2 100644 --- a/scripts/crons/apis.js +++ b/scripts/crons/apis.js @@ -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 { @@ -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}`)); }