2019-03-04 10:59:38 +03:00
|
|
|
const util = require('util');
|
|
|
|
const moment = require('moment');
|
|
|
|
const debug = require('ghost-ignition').debug('scheduling-default');
|
|
|
|
const SchedulingBase = require('./SchedulingBase');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const request = require('../../lib/request');
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Default post scheduling implementation.
|
|
|
|
*
|
|
|
|
* The default scheduler is used for all self-hosted blogs.
|
|
|
|
* It is implemented with pure javascript (timers).
|
|
|
|
*
|
|
|
|
* "node-cron" did not perform well enough and we really just needed a simple time management.
|
|
|
|
|
|
|
|
* @param {Objec†} options
|
|
|
|
* @constructor
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
function SchedulingDefault(options) {
|
|
|
|
SchedulingBase.call(this, options);
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// NOTE: How often should the scheduler wake up?
|
2016-05-19 14:49:22 +03:00
|
|
|
this.runTimeoutInMs = 1000 * 60 * 5;
|
2019-05-01 23:05:42 +03:00
|
|
|
|
|
|
|
// NOTE: An offset between now and past, which helps us choosing jobs which need to be executed soon.
|
2016-05-19 14:49:22 +03:00
|
|
|
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
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// NOTE: Each scheduler implementation can decide whether to load scheduled posts on bootstrap or not.
|
2017-11-08 02:24:34 +03:00
|
|
|
this.rescheduleOnBoot = true;
|
2019-05-01 23:05:42 +03:00
|
|
|
|
|
|
|
// NOTE: A sorted list of all scheduled jobs.
|
2016-05-19 14:49:22 +03:00
|
|
|
this.allJobs = {};
|
2019-05-01 23:05:42 +03:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Add a new job to the scheduler.
|
|
|
|
*
|
|
|
|
* A new job get's added when the post scheduler module receives a new model event e.g. "post.scheduled".
|
|
|
|
*
|
|
|
|
* @param {Object} object
|
|
|
|
* {
|
|
|
|
* time: [Number] A unix timestamp
|
|
|
|
* url: [String] The full post/page API url to publish it.
|
|
|
|
* extra: {
|
|
|
|
* httpMethod: [String] The method of the target API endpoint.
|
|
|
|
* oldTime: [Number] The previous published time.
|
|
|
|
* }
|
|
|
|
* }
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.schedule = function (object) {
|
|
|
|
this._addJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Remove & schedule a job.
|
|
|
|
*
|
|
|
|
* This function is useful if the model layer detects a rescheduling event.
|
|
|
|
* Rescheduling means: scheduled -> update published at.
|
|
|
|
* To be able to delete the previous job we need the old published time.
|
|
|
|
*
|
|
|
|
* @param {Object} object
|
|
|
|
* {
|
|
|
|
* time: [Number] A unix timestamp
|
|
|
|
* url: [String] The full post/page API url to publish it.
|
|
|
|
* extra: {
|
|
|
|
* httpMethod: [String] The method of the target API endpoint.
|
|
|
|
* oldTime: [Number] The previous published time.
|
|
|
|
* }
|
|
|
|
* }
|
2019-05-06 12:11:43 +03:00
|
|
|
* @param {Object} options
|
|
|
|
* {
|
|
|
|
* bootstrap: [Boolean]
|
|
|
|
* }
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
2019-05-06 12:11:43 +03:00
|
|
|
SchedulingDefault.prototype.reschedule = function (object, options = {bootstrap: false}) {
|
|
|
|
/**
|
|
|
|
* CASE:
|
|
|
|
* The post scheduling unit calls "reschedule" on bootstrap, because other custom scheduling implementations
|
|
|
|
* could use a database and we need to give the chance to update the job (delete + re-add).
|
|
|
|
*
|
|
|
|
* We receive a "bootstrap" variable to ensure that jobs are scheduled correctly for this scheduler implementation,
|
|
|
|
* because "object.extra.oldTime" === "object.time". If we mark the job as deleted, it won't get scheduled.
|
|
|
|
*/
|
|
|
|
if (!options.bootstrap) {
|
|
|
|
this._deleteJob({time: object.extra.oldTime, url: object.url});
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
this._addJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Unschedule a job.
|
|
|
|
*
|
|
|
|
* Unscheduling means: scheduled -> draft.
|
|
|
|
*
|
|
|
|
* @param {Object} object
|
|
|
|
* {
|
|
|
|
* time: [Number] A unix timestamp
|
|
|
|
* url: [String] The full post/page API url to publish it.
|
|
|
|
* extra: {
|
|
|
|
* httpMethod: [String] The method of the target API endpoint.
|
|
|
|
* oldTime: [Number] The previous published time.
|
|
|
|
* }
|
|
|
|
* }
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.unschedule = function (object) {
|
|
|
|
this._deleteJob(object);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description "run" is executed from outside (see post-scheduling module)
|
|
|
|
*
|
|
|
|
* This function will ensure that the scheduler will be kept alive while the blog is running.
|
|
|
|
* It will run recursively and checks if there are new jobs which need to be executed in the next X minutes.
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype.run = function () {
|
2018-09-10 12:47:43 +03:00
|
|
|
const self = this;
|
|
|
|
let timeout = null,
|
2017-09-05 21:23:11 +03:00
|
|
|
recursiveRun;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// NOTE: Ensure the scheduler never runs twice.
|
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 () {
|
2018-09-10 12:47:43 +03:00
|
|
|
const times = Object.keys(self.allJobs),
|
2017-09-05 21:23:11 +03:00
|
|
|
nextJobs = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// CASE: We stop till the offset is too big. We are only interested in jobs which need get executed soon.
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Add the actual job to "allJobs".
|
|
|
|
* @param {Object} object
|
|
|
|
* @private
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._addJob = function (object) {
|
2018-09-10 12:47:43 +03:00
|
|
|
let timestamp = moment(object.time).valueOf(),
|
2016-05-19 14:49:22 +03:00
|
|
|
keys = [],
|
|
|
|
sortedJobs = {},
|
|
|
|
instantJob = {},
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
// CASE: should have been already pinged or should be pinged soon
|
|
|
|
if (moment(timestamp).diff(moment(), 'minutes') < this.offsetInMinutes) {
|
2019-05-01 23:05:42 +03:00
|
|
|
debug('Emergency job', object.url, moment(object.time).format('YYYY-MM-DD HH:mm:ss'));
|
2017-08-31 10:12:44 +03:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
/**
|
|
|
|
* @description Delete the job.
|
|
|
|
*
|
|
|
|
* Keep a list of deleted jobs because it can happen that a job is already part of the next execution list,
|
|
|
|
* but it got deleted meanwhile.
|
|
|
|
*
|
|
|
|
* @param {Object} object
|
|
|
|
* @private
|
|
|
|
*/
|
2016-05-19 14:49:22 +03:00
|
|
|
SchedulingDefault.prototype._deleteJob = function (object) {
|
2018-09-10 12:47:43 +03:00
|
|
|
const {url, time} = object;
|
|
|
|
|
|
|
|
if (!time) {
|
2016-06-28 21:14:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:47:43 +03:00
|
|
|
const deleteKey = `${url}_${moment(time).valueOf()}`;
|
2016-06-15 10:40:53 +03:00
|
|
|
|
|
|
|
if (!this.deletedJobs[deleteKey]) {
|
|
|
|
this.deletedJobs[deleteKey] = [];
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:47:43 +03:00
|
|
|
debug('Deleted job', url, moment(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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description The "execute" function will receive the next jobs which need execution.
|
|
|
|
*
|
|
|
|
* Based on "offsetInMinutes" we figure out which jobs need execution and the "execute" function will
|
|
|
|
* ensure that
|
|
|
|
*
|
|
|
|
* The advantage of having a two step system (a general runner and an executor) is:
|
|
|
|
* - accuracy
|
|
|
|
* - setTimeout is limited to 24,3 days
|
|
|
|
*
|
|
|
|
* The execution of "setTimeout" is never guaranteed, therefor we've optimised the execution by using "setImmediate".
|
|
|
|
* The executor will put each job to sleep using `setTimeout` with a threshold of 70ms. And "setImmediate" is then
|
|
|
|
* used to detect the correct moment to trigger the URL.
|
|
|
|
|
|
|
|
* We can't use "process.nextTick" otherwise we will block I/O operations.
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._execute = function (jobs) {
|
2018-09-10 12:47:43 +03:00
|
|
|
const keys = Object.keys(jobs),
|
2016-05-19 14:49:22 +03:00
|
|
|
self = this;
|
|
|
|
|
|
|
|
keys.forEach(function (timestamp) {
|
2018-09-10 12:47:43 +03:00
|
|
|
let timeout = null,
|
2016-05-19 14:49:22 +03:00
|
|
|
diff = moment(Number(timestamp)).diff(moment());
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// NOTE: awake a little before...
|
2016-05-19 14:49:22 +03:00
|
|
|
timeout = setTimeout(function () {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
(function retry() {
|
2018-09-10 12:47:43 +03:00
|
|
|
let immediate = setImmediate(function () {
|
2016-05-19 14:49:22 +03:00
|
|
|
clearImmediate(immediate);
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// CASE: It's not the time yet...
|
2016-05-19 14:49:22 +03:00
|
|
|
if (moment().diff(moment(Number(timestamp))) <= self.beforePingInMs) {
|
|
|
|
return retry();
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:47:43 +03:00
|
|
|
const toExecute = jobs[timestamp];
|
2016-05-19 14:49:22 +03:00
|
|
|
delete jobs[timestamp];
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// CASE: each timestamp can have multiple jobs
|
2016-05-19 14:49:22 +03:00
|
|
|
toExecute.forEach(function (job) {
|
2018-09-10 12:47:43 +03:00
|
|
|
const {url, time} = job;
|
|
|
|
const deleteKey = `${url}_${moment(time).valueOf()}`;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// CASE: Was the job already deleted in the meanwhile...?
|
2016-05-19 14:49:22 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-01 23:05:42 +03:00
|
|
|
* @description Ping the job URL.
|
|
|
|
* @param {Object} object
|
|
|
|
* @return {Promise}
|
|
|
|
* @private
|
2016-05-19 14:49:22 +03:00
|
|
|
*/
|
|
|
|
SchedulingDefault.prototype._pingUrl = function (object) {
|
2018-09-10 12:47:43 +03:00
|
|
|
const {url, time} = object;
|
2019-03-04 10:59:38 +03:00
|
|
|
|
|
|
|
debug('Ping url', url, moment().format('YYYY-MM-DD HH:mm:ss'), moment(time).format('YYYY-MM-DD HH:mm:ss'));
|
|
|
|
|
|
|
|
const httpMethod = object.extra ? object.extra.httpMethod : 'PUT';
|
|
|
|
const tries = object.tries || 0;
|
|
|
|
const requestTimeout = object.extra ? object.extra.timeoutInMS : 1000 * 5;
|
|
|
|
const maxTries = 30;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
timeout: requestTimeout,
|
|
|
|
method: httpMethod.toLowerCase(),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
// CASE: 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
|
|
|
if (moment(time).isBefore(moment())) {
|
|
|
|
if (httpMethod === 'GET') {
|
2019-05-01 23:05:42 +03:00
|
|
|
// @TODO: rename to searchParams when updating to Got v10
|
2019-03-04 10:59:38 +03:00
|
|
|
options.query = 'force=true';
|
2016-05-19 14:49:22 +03:00
|
|
|
} else {
|
2019-03-04 10:59:38 +03:00
|
|
|
options.body = JSON.stringify({force: true});
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 10:59:38 +03:00
|
|
|
return request(url, options).catch((err) => {
|
|
|
|
const {statusCode} = err;
|
2018-07-17 19:10:05 +03:00
|
|
|
|
2019-03-04 10:59:38 +03:00
|
|
|
// CASE: post/page was deleted already
|
|
|
|
if (statusCode === 404) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-17 19:10:05 +03:00
|
|
|
|
2019-03-04 10:59:38 +03:00
|
|
|
// CASE: blog is in maintenance mode, retry
|
|
|
|
if (statusCode === 503 && tries < maxTries) {
|
|
|
|
setTimeout(() => {
|
|
|
|
object.tries = tries + 1;
|
|
|
|
this._pingUrl(object);
|
|
|
|
}, this.retryTimeoutInMs);
|
2016-06-14 17:08:49 +03:00
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.GhostError({
|
2018-09-10 12:47:43 +03:00
|
|
|
err,
|
2019-03-04 10:59:38 +03:00
|
|
|
context: 'Retrying...',
|
|
|
|
level: 'normal'
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2019-03-04 10:59:38 +03:00
|
|
|
|
|
|
|
return;
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
2019-03-04 10:59:38 +03:00
|
|
|
|
|
|
|
common.logging.error(new common.errors.GhostError({
|
|
|
|
err,
|
|
|
|
level: 'critical'
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SchedulingDefault;
|