Skip to content

Instantly share code, notes, and snippets.

@urielaero
Last active September 16, 2017 06:34
Show Gist options
  • Save urielaero/f6fbb77275a1391a604a1b3668b9a1f2 to your computer and use it in GitHub Desktop.
Save urielaero/f6fbb77275a1391a604a1b3668b9a1f2 to your computer and use it in GitHub Desktop.
services/CronJob.js
var doneList = []; //0 results in search
module.exports = {
run: function(minutes) {
Entity.find()
.sort('createdAt DESC')
.populate('webSearches')
.then((entities) => {
console.log('working');
const next = entities.find((ent) => {
return !ent.webSearches.length && doneList.indexOf(ent.id) === -1;
});
setTimeout(() => {
save(next.id, minutes);
doneList.push(next.id);
}, minutes*60000);
});
}
}
function save(entityId, minutes) {
console.log('work', entityId);
Entity.findOne(entityId)
.then(entity => CrawlService.searchEntity(entity, 'google'))
.then(searchResults => {
console.log('done: '+entityId, searchResults)
module.exports.run(15);
})
.catch(error => console.log(error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment