Skip to content

Commit

Permalink
Small improvements for the scheduler (#8957)
Browse files Browse the repository at this point in the history
no issue

- add caching logic to adapter creation (same as we use for storages)
- add debug logs to the default scheduler
- add `requestTimeout` to the default scheduler to support custom timeouts
- add `isRunning` logic to protect running the scheduler twice
  • Loading branch information
kirrg001 authored and aileen committed Aug 31, 2017
1 parent 7dec743 commit bf47397
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions core/server/adapters/scheduling/SchedulingDefault.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var util = require('util'),
moment = require('moment'),
request = require('superagent'),
debug = require('ghost-ignition').debug('scheduling-default'),
SchedulingBase = require(__dirname + '/SchedulingBase'),
errors = require(__dirname + '/../../errors'),
logging = require(__dirname + '/../../logging');
Expand All @@ -18,6 +19,7 @@ function SchedulingDefault(options) {

this.allJobs = {};
this.deletedJobs = {};
this.isRunning = false;
}

util.inherits(SchedulingDefault, SchedulingBase);
Expand Down Expand Up @@ -54,6 +56,12 @@ SchedulingDefault.prototype.run = function () {
var self = this,
timeout = null;

if (this.isRunning) {
return;
}

this.isRunning = true;

timeout = setTimeout(function () {
var times = Object.keys(self.allJobs),
nextJobs = {};
Expand Down Expand Up @@ -89,6 +97,8 @@ SchedulingDefault.prototype._addJob = function (object) {

// CASE: should have been already pinged or should be pinged soon
if (moment(timestamp).diff(moment(), 'minutes') < this.offsetInMinutes) {
debug('Imergency job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));

instantJob[timestamp] = [object];
this._execute(instantJob);
return;
Expand All @@ -99,6 +109,7 @@ SchedulingDefault.prototype._addJob = function (object) {
this.allJobs[timestamp] = [];
}

debug('Added job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
this.allJobs[timestamp].push(object);

keys = Object.keys(this.allJobs);
Expand All @@ -122,6 +133,7 @@ SchedulingDefault.prototype._deleteJob = function (object) {
this.deletedJobs[deleteKey] = [];
}

debug('Deleted job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
this.deletedJobs[deleteKey].push(object);
};

Expand Down Expand Up @@ -178,10 +190,13 @@ SchedulingDefault.prototype._execute = function (jobs) {
* - if we detect to publish a post in the past (case blog is down), we add a force flag
*/
SchedulingDefault.prototype._pingUrl = function (object) {
debug('Ping url', object.url, moment().format('YYYY-MM-DD HH:mm:ss'), moment(object.time).format('YYYY-MM-DD HH:mm:ss'));

var url = object.url,
time = object.time,
httpMethod = object.extra ? object.extra.httpMethod : 'PUT',
tries = object.tries || 0,
requestTimeout = object.extra ? object.extra.timeoutInMS : 1000 * 5,
maxTries = 30,
req = request[httpMethod.toLowerCase()](url),
self = this, timeout;
Expand All @@ -196,6 +211,10 @@ SchedulingDefault.prototype._pingUrl = function (object) {
}
}

req.timeout({
response: requestTimeout
});

req.end(function (err, response) {
if (err) {
// CASE: post/page was deleted already
Expand Down
9 changes: 8 additions & 1 deletion core/server/adapters/scheduling/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var _ = require('lodash'),
Promise = require('bluebird'),
SchedulingBase = require(__dirname + '/SchedulingBase'),
errors = require(__dirname + '/../../errors');
errors = require(__dirname + '/../../errors'),
cache = {};

exports.createAdapter = function (options) {
options = options || {};
Expand All @@ -15,6 +16,10 @@ exports.createAdapter = function (options) {
return Promise.reject(new errors.IncorrectUsageError({message: 'Please provide an active adapter.'}));
}

if (cache.hasOwnProperty(activeAdapter)) {
return cache[activeAdapter];
}

/**
* CASE: active adapter is a npm module
*/
Expand Down Expand Up @@ -68,5 +73,7 @@ exports.createAdapter = function (options) {
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not provide the minimum required functions.'}));
}

cache[activeAdapter] = adapter;

return Promise.resolve(adapter);
};

0 comments on commit bf47397

Please sign in to comment.