mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-03 16:38:22 +03:00
post-scheduling: HTTP 503 retry logic for default adapter
This commit is contained in:
parent
fe5e0eed48
commit
a3cd43909b
@ -13,6 +13,7 @@ function SchedulingDefault(options) {
|
||||
this.runTimeoutInMs = 1000 * 60 * 5;
|
||||
this.offsetInMinutes = 10;
|
||||
this.beforePingInMs = -50;
|
||||
this.retryTimeoutInMs = 1000 * 5;
|
||||
|
||||
this.allJobs = {};
|
||||
this.deletedJobs = {};
|
||||
@ -158,14 +159,16 @@ SchedulingDefault.prototype._execute = function (jobs) {
|
||||
};
|
||||
|
||||
/**
|
||||
* if we detect to publish a post in the past (case blog is down)
|
||||
* we add a force flag
|
||||
* - if we detect to publish a post in the past (case blog is down), we add a force flag
|
||||
*/
|
||||
SchedulingDefault.prototype._pingUrl = function (object) {
|
||||
var url = object.url,
|
||||
time = object.time,
|
||||
httpMethod = object.extra.httpMethod,
|
||||
req = request[httpMethod.toLowerCase()](url);
|
||||
tries = object.tries || 0,
|
||||
maxTries = 30,
|
||||
req = request[httpMethod.toLowerCase()](url),
|
||||
self = this, timeout;
|
||||
|
||||
if (moment(time).isBefore(moment())) {
|
||||
if (httpMethod === 'GET') {
|
||||
@ -184,6 +187,16 @@ SchedulingDefault.prototype._pingUrl = function (object) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
errors.logError(err);
|
||||
}
|
||||
});
|
||||
|
@ -282,5 +282,47 @@ describe('Scheduling Default Adapter', function () {
|
||||
setTimeout(retry, 100);
|
||||
})();
|
||||
});
|
||||
|
||||
it('pingUrl, but blog returns 503', function (done) {
|
||||
var app = express(),
|
||||
server = http.createServer(app),
|
||||
returned500Count = 0,
|
||||
returned200 = false,
|
||||
reqBody;
|
||||
|
||||
scope.adapter.retryTimeoutInMs = 200;
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.put('/ping', function (req, res) {
|
||||
reqBody = req.body;
|
||||
|
||||
if (returned500Count === 5) {
|
||||
returned200 = true;
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
|
||||
returned500Count = returned500Count + 1;
|
||||
res.sendStatus(503);
|
||||
});
|
||||
|
||||
server.listen(1111);
|
||||
|
||||
scope.adapter._pingUrl({
|
||||
url: 'http://localhost:1111/ping',
|
||||
time: moment().valueOf(),
|
||||
extra: {
|
||||
httpMethod: 'PUT'
|
||||
}
|
||||
});
|
||||
|
||||
(function retry() {
|
||||
if (returned200) {
|
||||
should.exist(reqBody.force);
|
||||
return server.close(done);
|
||||
}
|
||||
|
||||
setTimeout(retry, 100);
|
||||
})();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user