2016-07-07 15:13:19 +02:00
|
|
|
import Posts from '../collection.js';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
SyncedCron.options = {
|
|
|
|
log: true,
|
|
|
|
collectionName: 'cronHistory',
|
|
|
|
utc: false,
|
|
|
|
collectionTTL: 172800
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const addJob = function () {
|
|
|
|
SyncedCron.add({
|
|
|
|
name: 'checkScheduledPosts',
|
|
|
|
schedule(parser) {
|
|
|
|
return parser.text('every 10 minutes');
|
|
|
|
},
|
|
|
|
job() {
|
2016-07-14 10:12:27 +02:00
|
|
|
// fetch all posts tagged as future
|
2016-07-14 15:54:59 +09:00
|
|
|
const scheduledPosts = Posts.find({isFuture: true}, {fields: {_id: 1, status: 1, postedAt: 1, userId: 1, title: 1}}).fetch();
|
2016-07-07 15:13:19 +02:00
|
|
|
|
2016-07-14 10:12:27 +02:00
|
|
|
// 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)) {
|
2016-07-07 15:13:19 +02:00
|
|
|
const postsIds = _.pluck(postsToUpdate, '_id');
|
2016-07-14 15:54:59 +09:00
|
|
|
Posts.update({_id: {$in: postsIds}}, {$set: {isFuture: false}}, {multi: true});
|
2016-07-07 15:13:19 +02:00
|
|
|
|
|
|
|
// log the action
|
|
|
|
console.log('// Scheduled posts approved:', postsIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Meteor.startup(function () {
|
|
|
|
addJob();
|
|
|
|
});
|
|
|
|
|