diff --git a/Backend/index.app.js b/Backend/index.app.js new file mode 100644 index 000000000..c93b90206 --- /dev/null +++ b/Backend/index.app.js @@ -0,0 +1,43 @@ +let ElasticService = require('./services/elastic'); +let cron = require('node-cron'); +let Raven = require('raven'); +let SQ = require("sequelize"); +let Op = SQ.Op; + +var Recipe = require('./models').Recipe; + +cron.schedule('*/1 * * * *', () => { + let lt = new Date(); + lt.setDate(lt.getDate() - 7); + + Recipe.findAll({ + where: { + [Op.or]: [ + { indexedAt: null }, + { indexedAt: { [Op.lt]: lt } } + ] + }, + limit: 400, + order: [ + ['indexedAt', 'ASC'] + ] + }).then(recipes => { + if (!recipes || recipes.length === 0) return; + + return ElasticService.bulk('index', 'recipes', recipes).then(() => { + let ids = recipes.map(r => r.id); + return Recipe.update( + { indexedAt: new Date() }, + { + where: { + id: { [Op.in]: ids } + }, + silent: true, + hooks: false + } + ); + }); + }).catch(e => { + Raven.captureException(e); + }); +}); diff --git a/Backend/models/recipe.js b/Backend/models/recipe.js index 50a15030c..b9cc01999 100644 --- a/Backend/models/recipe.js +++ b/Backend/models/recipe.js @@ -1,6 +1,5 @@ let UtilService = require('../services/util'); let ElasticService = require('../services/elastic'); -let cron = require('node-cron'); let Raven = require('raven'); let SQ = require("sequelize"); let Op = SQ.Op; @@ -283,41 +282,5 @@ module.exports = (sequelize, DataTypes) => { }); } - cron.schedule('*/1 * * * *', () => { - let lt = new Date(); - lt.setDate(lt.getDate() - 7); - - Recipe.findAll({ - where: { - [Op.or]: [ - { indexedAt: null }, - { indexedAt: { [Op.lt]: lt } } - ] - }, - limit: 400, - order: [ - ['indexedAt', 'ASC'] - ] - }).then(recipes => { - if (!recipes || recipes.length === 0) return; - - return ElasticService.bulk('index', 'recipes', recipes).then(() => { - let ids = recipes.map(r => r.id); - return Recipe.update( - { indexedAt: new Date() }, - { - where: { - id: { [Op.in]: ids } - }, - silent: true, - hooks: false - } - ); - }); - }).catch(e => { - Raven.captureException(e); - }); - }); - return Recipe; };