2016-05-19 14:49:22 +03:00
|
|
|
var util = require('util'),
|
|
|
|
moment = require('moment'),
|
|
|
|
request = require('superagent'),
|
2017-08-31 10:12:44 +03:00
|
|
|
debug = require('ghost-ignition').debug('scheduling-default'),
|
2017-12-11 22:27:09 +03:00
|
|
|
SchedulingBase = require('./SchedulingBase'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common');
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* allJobs is a sorted list by time attribute
|
|
|
|
*/
|
|
|
|
function SchedulingDefault(options) {
|
|
|
|
SchedulingBase.call(this, options);
|
|
|
|
|
|
|
|
this.runTimeoutInMs = 1000 * 60 * 5;
|
|
|
|
this.offsetInMinutes = 10;
|
|
|
|
this.beforePingInMs = -50;
|
2016-06-14 17:08:49 +03:00
|
|
|
this.retryTimeoutInMs = 1000 * 5;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-11-08 02:24:34 +03:00
|
|
|
this.rescheduleOnBoot = true;
|
2016-05-19 14:49:22 +03:00
|
|
|
this.allJobs = {};
|
|
|
|
this.deletedJobs = {};
|
2017-08-31 10:12:44 +03:00
|
|
|
this.isRunning = false;
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(SchedulingDefault, SchedulingBase);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add to list
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.schedule = function (object) {
|
|
|
|
this._addJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove from list
|
|
|
|
* add to list
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.reschedule = function (object) {
|
|
|
|
this._deleteJob({time: object.extra.oldTime, url: object.url});
|
|
|
|
this._addJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove from list
|
|
|
|
* deletion happens right before execution
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.unschedule = function (object) {
|
|
|
|
this._deleteJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if there are new jobs which needs to be published in the next x minutes
|
|
|
|
* because allJobs is a sorted list, we don't have to iterate over all jobs, just until the offset is too big
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.run = function () {
|
|
|
|
var self = this,
|
2017-09-05 21:23:11 +03:00
|
|
|
timeout = null,
|
|
|
|
recursiveRun;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
if (this.isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isRunning = true;
|
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
recursiveRun = function recursiveRun() {
|
|
|
|
timeout = setTimeout(function () {
|
|
|
|
var times = Object.keys(self.allJobs),
|
|
|
|
nextJobs = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
times.every(function (time) {
|
|
|
|
if (moment(Number(time)).diff(moment(), 'minutes') <= self.offsetInMinutes) {
|
|
|
|
nextJobs[time] = self.allJobs[time];
|
|
|
|
delete self.allJobs[time];
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
// break!
|
|
|
|
return false;
|
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
clearTimeout(timeout);
|
|
|
|
self._execute(nextJobs);
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2017-09-05 21:23:11 +03:00
|
|
|
recursiveRun();
|
|
|
|
}, self.runTimeoutInMs);
|
|
|
|
};
|
|
|
|
|
|
|
|
recursiveRun();
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* each timestamp key entry can have multiple jobs
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._addJob = function (object) {
|
|
|
|
var timestamp = moment(object.time).valueOf(),
|
|
|
|
keys = [],
|
|
|
|
sortedJobs = {},
|
|
|
|
instantJob = {},
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
// CASE: should have been already pinged or should be pinged soon
|
|
|
|
if (moment(timestamp).diff(moment(), 'minutes') < this.offsetInMinutes) {
|
2017-08-31 10:12:44 +03:00
|
|
|
debug('Imergency job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
instantJob[timestamp] = [object];
|
|
|
|
this._execute(instantJob);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: are there jobs already scheduled for the same time?
|
|
|
|
if (!this.allJobs[timestamp]) {
|
|
|
|
this.allJobs[timestamp] = [];
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
debug('Added job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
|
2016-05-19 14:49:22 +03:00
|
|
|
this.allJobs[timestamp].push(object);
|
|
|
|
|
|
|
|
keys = Object.keys(this.allJobs);
|
|
|
|
keys.sort();
|
|
|
|
|
|
|
|
for (i = 0; i < keys.length; i = i + 1) {
|
|
|
|
sortedJobs[keys[i]] = this.allJobs[keys[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.allJobs = sortedJobs;
|
|
|
|
};
|
|
|
|
|
|
|
|
SchedulingDefault.prototype._deleteJob = function (object) {
|
2016-06-28 21:14:29 +03:00
|
|
|
if (!object.time) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:40:53 +03:00
|
|
|
var deleteKey = object.url + '_' + moment(object.time).valueOf();
|
|
|
|
|
|
|
|
if (!this.deletedJobs[deleteKey]) {
|
|
|
|
this.deletedJobs[deleteKey] = [];
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
debug('Deleted job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
|
2016-06-15 10:40:53 +03:00
|
|
|
this.deletedJobs[deleteKey].push(object);
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ping jobs
|
|
|
|
* setTimeout is not accurate, but we can live with that fact and use setImmediate feature to qualify
|
|
|
|
* we don't want to use process.nextTick, this would block any I/O operation
|
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._execute = function (jobs) {
|
|
|
|
var keys = Object.keys(jobs),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
keys.forEach(function (timestamp) {
|
|
|
|
var timeout = null,
|
|
|
|
diff = moment(Number(timestamp)).diff(moment());
|
|
|
|
|
|
|
|
// awake a little before
|
|
|
|
timeout = setTimeout(function () {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
(function retry() {
|
|
|
|
var immediate = setImmediate(function () {
|
|
|
|
clearImmediate(immediate);
|
|
|
|
|
|
|
|
if (moment().diff(moment(Number(timestamp))) <= self.beforePingInMs) {
|
|
|
|
return retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
var toExecute = jobs[timestamp];
|
|
|
|
delete jobs[timestamp];
|
|
|
|
|
|
|
|
toExecute.forEach(function (job) {
|
|
|
|
var deleteKey = job.url + '_' + moment(job.time).valueOf();
|
|
|
|
|
|
|
|
if (self.deletedJobs[deleteKey]) {
|
2016-06-15 10:40:53 +03:00
|
|
|
if (self.deletedJobs[deleteKey].length === 1) {
|
|
|
|
delete self.deletedJobs[deleteKey];
|
|
|
|
} else {
|
|
|
|
self.deletedJobs[deleteKey].pop();
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._pingUrl(job);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|
2016-10-13 11:49:10 +03:00
|
|
|
}, diff - 70);
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-06-14 17:08:49 +03:00
|
|
|
* - if we detect to publish a post in the past (case blog is down), we add a force flag
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._pingUrl = function (object) {
|
2017-08-31 10:12:44 +03:00
|
|
|
debug('Ping url', object.url, moment().format('YYYY-MM-DD HH:mm:ss'), moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
var url = object.url,
|
|
|
|
time = object.time,
|
2016-06-15 10:40:53 +03:00
|
|
|
httpMethod = object.extra ? object.extra.httpMethod : 'PUT',
|
2016-06-14 17:08:49 +03:00
|
|
|
tries = object.tries || 0,
|
2017-08-31 10:12:44 +03:00
|
|
|
requestTimeout = object.extra ? object.extra.timeoutInMS : 1000 * 5,
|
2016-06-14 17:08:49 +03:00
|
|
|
maxTries = 30,
|
|
|
|
req = request[httpMethod.toLowerCase()](url),
|
|
|
|
self = this, timeout;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
if (moment(time).isBefore(moment())) {
|
|
|
|
if (httpMethod === 'GET') {
|
|
|
|
req.query('force=true');
|
|
|
|
} else {
|
|
|
|
req.send({
|
|
|
|
force: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
req.timeout({
|
|
|
|
response: requestTimeout
|
|
|
|
});
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
req.end(function (err, response) {
|
|
|
|
if (err) {
|
|
|
|
// CASE: post/page was deleted already
|
|
|
|
if (response && response.status === 404) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 17:08:49 +03:00
|
|
|
// CASE: blog is in maintenance mode, retry
|
|
|
|
if (response && response.status === 503 && tries < maxTries) {
|
|
|
|
timeout = setTimeout(function pingAgain() {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
object.tries = tries + 1;
|
|
|
|
self._pingUrl(object);
|
|
|
|
}, self.retryTimeoutInMs);
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.GhostError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
|
|
|
level: 'critical'
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SchedulingDefault;
|