Using a custom scheduling module

Scheduling

[Documentation in progress]

Default Adapter

Ghost ships a default adapter for scheduling, which will schedule your posts or newsletters within the process.
This adapter is configured by default, so if you start your server the scheduling feature is ready to use.

A disadvantage of the default adapter can be for you:

  • if you have a cache in front of your blog, the default adapter is not able to clear a cache entry
  • if your blog is off, scheduling is as well. That's why you maybe want to write custom adapters to communicate with external schedulers
Custom adapters

You can write your own adapters.
For example if you want to use the Heroku Scheduler or your own external logic.

1. Writing your adapter

my-adapter.js

var util = require('util');

// If this require does not work, then your content folder structure is different
// So change the require path so that your adapter can import the scheduling base.
var SchedulingBase = require('../../core/server/adapters/scheduling/SchedulingBase');

function MyAdapter(options) {
    SchedulingBase.call(this, options);
}

util.inherits(MyAdapter, SchedulingBase);

// required functions you need to implement
MyAdapter.prototype.schedule = function(object) {
    // when the job should be executed (time is a UTC timestamp)
    var time = object.time;

    // the url you need to execute when the time is reached
    var url = object.url;

    // the HTTP method you need to use
    var httpMethod = object.extra.httpMethod;
};

MyAdapter.prototype.reschedule = function(object) {
    // see MyAdapter.prototype.schedule

    // the time when the url was scheduled before (oldTime is a UTC timestamp)
    var oldTime = object.extra.oldTime;
};

MyAdapter.prototype.unschedule = function(object) {
    // see MyAdapter.prototype.schedule
};

//this function is called on server bootstrap
MyAdapter.prototype.run = function() {};

module.exports = MyAdapter;

2. Adding your adapter

  • copy your adapter to content/scheduling/.

3. Extend your config

"scheduling": {
  "active": 'my-adapter'
}

In this example we run Ghost in production, replace production if you run Ghost for example in development mode.

Publish posts in the past

By default Ghost disallows publishing posts in the past or in the future to avoid problems/bugs. But in case your scheduling service was down, you can use the force flag to publish posts in the past.

if (moment(time).isBefore(moment())) {
    request[httpMethod](url)
        .send({ force: true })
        .end(...)
}

If the httpMethod is a GET, you need to use query parameters.

Using a custom scheduling module