🐛 Protected Ghost blog against invalid uploaded routes.yaml (#10100)

* 🐛  Protected Ghost blog against invalid uploaded routes.yaml

no issue

- e.g. you upload `filter:tag=this is a wrong filter value`
- ask the url service if it has finished it's work to ensure the upload was successful
- wait 5 seconds till Ghost will bring back the last uploaded valid version

* fixed test
This commit is contained in:
Katharina Irrgang 2018-11-26 10:28:22 +01:00 committed by Fabien O'Carroll
parent ec0a58b6f7
commit e48c28b98f
3 changed files with 45 additions and 25 deletions

View File

@ -126,18 +126,49 @@ module.exports = {
.then(() => {
const siteApp = require('../../web/site/app');
try {
return siteApp.reload();
} catch (err) {
// bring back backup, otherwise your Ghost blog is broken
const bringBackValidRoutes = () => {
urlService.resetGenerators({releaseResourcesOnly: true});
return fs.copy(backupRoutesPath, `${config.getContentPath('settings')}/routes.yaml`)
.then(() => {
return siteApp.reload();
})
.then(() => {
});
};
try {
siteApp.reload();
} catch (err) {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
}
let tries = 0;
function isBlogRunning() {
return Promise.delay(1000)
.then(() => {
if (!urlService.hasFinished()) {
if (tries > 5) {
throw new common.errors.InternalServerError({
message: 'Could not load routes.yaml file.'
});
}
tries = tries + 1;
return isBlogRunning();
}
});
}
return isBlogRunning()
.catch((err) => {
return bringBackValidRoutes()
.finally(() => {
throw err;
});
});
});
}
},

View File

@ -126,8 +126,7 @@ class Queue extends EventEmitter {
err: err
}));
// just try again
this.run(options);
// @NOTE: The url service stays in maintenance mode. There is nothing we can do if an url generator fails.
}
} else {
// CASE 1: zero tolerance, kill run fn

View File

@ -3,6 +3,7 @@ const _ = require('lodash');
const Promise = require('bluebird');
const should = require('should');
const sinon = require('sinon');
const common = require('../../../../server/lib/common');
const Queue = require('../../../../server/services/url/Queue');
const sandbox = sinon.sandbox.create();
@ -13,6 +14,7 @@ describe('Unit: services/url/Queue', function () {
queue = new Queue();
sandbox.spy(queue, 'run');
sandbox.stub(common.logging, 'error');
});
afterEach(function () {
@ -130,31 +132,19 @@ describe('Unit: services/url/Queue', function () {
});
});
it('subscriber throws error', function (done) {
let i = 0;
let notified = 0;
queue.addListener('ended', function (event) {
event.should.eql('nachos');
queue.run.callCount.should.eql(3);
notified.should.eql(1);
done();
});
it('subscriber throws error', function () {
queue.register({
event: 'nachos'
}, function () {
if (i === 0) {
i = i + 1;
throw new Error('oops');
}
notified = notified + 1;
throw new Error('oops');
});
queue.start({
event: 'nachos'
});
common.logging.error.calledOnce.should.be.true();
queue.toNotify['nachos'].notified.length.should.eql(0);
});
});