scheduled posts: don't run update when not needed

This commit is contained in:
xavizalote 2016-07-14 10:12:27 +02:00
parent 336a1624eb
commit b7bb9fd5f9

View file

@ -16,14 +16,14 @@ const addJob = function () {
return parser.text('every 10 minutes');
},
job() {
// fetch all posts tagged as scheduled
// fetch all posts tagged as future
const scheduledPosts = Posts.find({isFuture: true}, {fields: {_id: 1, status: 1, postedAt: 1, userId: 1, title: 1}}).fetch();
if (scheduledPosts) {
// filter the scheduled posts to retrieve only the one that should update, considering their schedule
const postsToUpdate = scheduledPosts.filter(post => post.postedAt <= new Date());
// update all posts with status approved
// filter the scheduled posts to retrieve only the one that should update, considering their schedule
const postsToUpdate = scheduledPosts.filter(post => post.postedAt <= new Date());
// update posts found
if (!_.isEmpty(postsToUpdate)) {
const postsIds = _.pluck(postsToUpdate, '_id');
Posts.update({_id: {$in: postsIds}}, {$set: {isFuture: false}}, {multi: true});