diff --git a/core/server/scheduling/SchedulingDefault.js b/core/server/scheduling/SchedulingDefault.js index 60aae38794..cbde4c76f5 100644 --- a/core/server/scheduling/SchedulingDefault.js +++ b/core/server/scheduling/SchedulingDefault.js @@ -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); } }); diff --git a/core/test/unit/scheduling/SchedulingDefault_spec.js b/core/test/unit/scheduling/SchedulingDefault_spec.js index 9546e88011..c99986aeb2 100644 --- a/core/test/unit/scheduling/SchedulingDefault_spec.js +++ b/core/test/unit/scheduling/SchedulingDefault_spec.js @@ -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); + })(); + }); }); });