Skip to content

Instantly share code, notes, and snippets.

@xavxyz
Created July 7, 2016 12:59
Show Gist options
  • Save xavxyz/7e0af2ea78f0fd7709a1ad0b369a8bd3 to your computer and use it in GitHub Desktop.
Save xavxyz/7e0af2ea78f0fd7709a1ad0b369a8bd3 to your computer and use it in GitHub Desktop.
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() {
// fetch all posts tagged as scheduled
const scheduledPosts = Posts.find({status: Posts.config.STATUS_SCHEDULED}, {fields: {_id: 1, status: 1, postedAt: 1, userId: 1, title: 1}}).fetch();
// filter the scheduled posts to retrieve only the one that should update, considering their schedule
const postsToUpdate = scheduledPosts.filter(post => post.postedAt <= new Date());
// note: as a pending post gets its postedAt when approved, it may be not necessary to check the default post status
// loop through the posts to update and set the right status depending on the post's author
postsToUpdate.forEach(function(post) {
// status can be approved or pending depending on settings and author's role
const status = Posts.getDefaultStatus(post.userId);
// update the post
Posts.update({_id: post._id}, {$set: { status }});
// log the action
console.log('// Status changed for scheduled post:', post.title);
});
}
});
};
Meteor.startup(function () {
addJob();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment