-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from julianpoy/independent-indexer
Moved indexer logic to separate app
- Loading branch information
Showing
2 changed files
with
43 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters