Skip to content

Commit

Permalink
Merge pull request #193 from julianpoy/independent-indexer
Browse files Browse the repository at this point in the history
Moved indexer logic to separate app
  • Loading branch information
julianpoy authored Mar 19, 2019
2 parents ace3eab + 2f5f35b commit 6771e5c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
43 changes: 43 additions & 0 deletions Backend/index.app.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
37 changes: 0 additions & 37 deletions Backend/models/recipe.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
};

0 comments on commit 6771e5c

Please sign in to comment.