Last active
September 16, 2017 06:34
-
-
Save urielaero/f6fbb77275a1391a604a1b3668b9a1f2 to your computer and use it in GitHub Desktop.
services/CronJob.js
This file contains hidden or 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
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