From 78c2beec3cde535bcd9126a038acf60256cb4ab5 Mon Sep 17 00:00:00 2001 From: Yuliy Vigdorchik Date: Wed, 15 Aug 2018 16:47:35 -0400 Subject: [PATCH 1/4] Check if the schedule publish returned a non-ok response and log --- lib/services/schedule.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/services/schedule.js b/lib/services/schedule.js index c55865f7..98f2bac2 100644 --- a/lib/services/schedule.js +++ b/lib/services/schedule.js @@ -35,7 +35,15 @@ function publishExternally(url) { return bluebird.try(function () { const published = replaceVersion(url, 'published'); - return rest.putObject(published); + return rest.putObject(published) + .then(function (res) { + if (!res.ok) { + log('error', 'failed to publish url from schedule', { + url + }); + } + return res; + }); }); } @@ -57,7 +65,8 @@ function getPublishableItems(list, now) { })); return _.filter(list, function (item) { - return item.value && item.value[scheduledAtProperty] < now; + return true; + // return item.value && item.value[scheduledAtProperty] < now; }); } From 57604d22a798238aaa739f5d7759fe9f60a271de Mon Sep 17 00:00:00 2001 From: Yuliy Vigdorchik Date: Wed, 15 Aug 2018 16:48:24 -0400 Subject: [PATCH 2/4] Remove debug code --- lib/services/schedule.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/services/schedule.js b/lib/services/schedule.js index 98f2bac2..effeaff6 100644 --- a/lib/services/schedule.js +++ b/lib/services/schedule.js @@ -65,8 +65,7 @@ function getPublishableItems(list, now) { })); return _.filter(list, function (item) { - return true; - // return item.value && item.value[scheduledAtProperty] < now; + return item.value && item.value[scheduledAtProperty] < now; }); } From c3b9ddd8b8c9a0fe9dc2780c822abe91264ba6fc Mon Sep 17 00:00:00 2001 From: Yuliy Vigdorchik Date: Wed, 15 Aug 2018 16:49:11 -0400 Subject: [PATCH 3/4] Make sure that there is some response before trying to check if it has an ok result --- lib/services/schedule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/schedule.js b/lib/services/schedule.js index effeaff6..36dfb952 100644 --- a/lib/services/schedule.js +++ b/lib/services/schedule.js @@ -37,7 +37,7 @@ function publishExternally(url) { return rest.putObject(published) .then(function (res) { - if (!res.ok) { + if (res && !res.ok) { log('error', 'failed to publish url from schedule', { url }); From 86cce09c54724d59b1ed80bcf8055f6a62beede2 Mon Sep 17 00:00:00 2001 From: Yuliy Vigdorchik Date: Wed, 15 Aug 2018 17:16:15 -0400 Subject: [PATCH 4/4] Cover the added lines --- lib/services/schedule.js | 1 + lib/services/schedule.test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/services/schedule.js b/lib/services/schedule.js index 36dfb952..ba3007eb 100644 --- a/lib/services/schedule.js +++ b/lib/services/schedule.js @@ -213,6 +213,7 @@ module.exports.stopListening = stopListening; module.exports.setScheduleInterval = setScheduleInterval; // For testing +module.exports.publishExternally = publishExternally; module.exports.setLog = function (fakeLogger) { log = fakeLogger; }; diff --git a/lib/services/schedule.test.js b/lib/services/schedule.test.js index e1ffd555..41549a01 100644 --- a/lib/services/schedule.test.js +++ b/lib/services/schedule.test.js @@ -151,6 +151,27 @@ describe(_.startCase(filename), function () { sandbox.clock.tick(intervalDelay); }); + it('logs if the publish request is not ok', function (done) { + const uri = 'http://abce', + scheduledItem = {at: intervalDelay - 1, publish: uri}, + data = {key:'some-key', value: JSON.stringify(scheduledItem)}; + + rest.putObject.returns(bluebird.resolve({ok: false})); + siteService.sites.returns([{host: 'a', path: '/'}]); + db.pipeToPromise.returns(bluebird.resolve(JSON.stringify([data]))); + db.get.returns(bluebird.resolve(JSON.stringify(scheduledItem))); + db.batch.returns(bluebird.resolve()); + + lib.setLog(function (logType, msg) { + expect(logType).to.equal('error'); + expect(msg).to.match(/failed to publish/); + done(); + }); + + fn(); + sandbox.clock.tick(intervalDelay); + }); + it('logs error if failed to parse JSON', function (done) { const uri = 'abce/_pages/abcd', data = {key:'some-key', value: JSON.stringify({at: intervalDelay - 1, publish: uri}).substring(5)}; @@ -185,4 +206,18 @@ describe(_.startCase(filename), function () { sandbox.clock.tick(intervalDelay); }); }); + + describe('publishExternally', function () { + const fn = lib[this.title]; + + it('handles a false-y response from the rest service', function () { + const uri = 'http://abce'; + + rest.putObject.returns(bluebird.resolve(null)); + return fn(uri) + .then(function (res) { + expect(res).to.be.null; + }); + }); + }); });