mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-29 13:52:10 +03:00
Small improvements for the scheduler (#8957)
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
This commit is contained in:
parent
7dec743bba
commit
bf47397ac2
@ -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');
|
||||
@ -18,6 +19,7 @@ function SchedulingDefault(options) {
|
||||
|
||||
this.allJobs = {};
|
||||
this.deletedJobs = {};
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
util.inherits(SchedulingDefault, SchedulingBase);
|
||||
@ -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 = {};
|
||||
@ -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;
|
||||
@ -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);
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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;
|
||||
@ -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
|
||||
|
@ -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 || {};
|
||||
@ -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
|
||||
*/
|
||||
@ -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);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user