Skip to content

Commit

Permalink
fix(publishing): queue does now abort automatically (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jun 2, 2017
1 parent 5b3e524 commit 96b4370
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
10 changes: 10 additions & 0 deletions lib/push/publishing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import log from 'npmlog'
import getEntityName from './get-entity-name'
import errorBuffer from '../utils/error-buffer'

let lastQueueLength = 0

function runQueue (queue, result) {
if (!result) {
result = []
Expand Down Expand Up @@ -37,6 +39,13 @@ function runQueue (queue, result) {
.then((entities) => entities.filter((entity) => entity))
.then((entities) => {
if (entities.length > 0) {
if (lastQueueLength && lastQueueLength === entities.length) {
errorBuffer.push(new Error('Queue was not able to publish at least one entity. Aborting.'))
const failedEntities = entities.map((entitiy) => entitiy.sys.id)
return result.filter((entity) => !failedEntities.includes(entity.sys.id))
}
lastQueueLength = entities.length

log.info(`Found ${entities.length} unpublished entities`)
return runQueue(entities, result)
}
Expand All @@ -58,6 +67,7 @@ export function publishEntities (entities) {
const type = entity.sys.type || 'unknown type'
log.info(`Starting publishing ${entities.length} ${type}s`)

lastQueueLength = entities.length
return runQueue(entities)
.then((result) => {
log.info(`Finished publishing ${entities.length} ${type}s. Returning ${result.length} entities`)
Expand Down
53 changes: 36 additions & 17 deletions test/push/publishing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,11 @@ test('Fails to publish entities', (t) => {
const publishStub = sinon.stub()
publishStub.onFirstCall().returns(Promise.resolve({sys: {type: 'Asset', publishedVersion: 2}}))
const apiError = {
message: 'Validation error',
status: 422,
sys: {
type: 'Error',
id: 'UnresolvedLinks'
},
details: {
errors: [
{
name: 'notResolvable',
link: {
type: 'Link',
linkType: 'Entry',
id: 'linkedEntryId'
},
path: [
'fields',
'category',
'en-US',
0
]
name: 'notResolvable'
}
]
}
Expand All @@ -88,6 +72,41 @@ test('Fails to publish entities', (t) => {
})
})

test('Queue does abort itself', (t) => {
setup()
const publishStub = sinon.stub()
const apiError = {
status: 422,
details: {
errors: [
{
name: 'notResolvable'
}
]
}
}
const errorValidation = new Error(JSON.stringify(apiError))
// First call resolves, others fail
publishStub.returns(Promise.reject(errorValidation))
publishStub.onFirstCall().returns(Promise.resolve({sys: {type: 'Asset', publishedVersion: 2, id: '123'}}))
return publishEntities([
{ sys: {id: '123', type: 'asset'}, publish: publishStub },
{ sys: {id: '456', type: 'asset'}, publish: publishStub }
])
.then((result) => {
const logs = logMock.info.args.map((args) => args[0])
t.equals(publishStub.callCount, 3, 'publishes the first, retries the second only once')
t.equals(errorBufferMock.push.callCount, 4, 'logs 4 errors')
t.equals(errorBufferMock.push.lastCall.args[0].message, 'Queue was not able to publish at least one entity. Aborting.', 'Aborted queue with error')
t.equals(logs.filter((log) => log.includes('Starting new publishing queue')).length, 2, 'Starts queue twice')
t.equals(logs.filter((log) => log.includes('Unable to resolve 456 (456)')).length, 2, 'Is unable to resolve 456 twice')
t.equals(logs.filter((log) => log.includes('Published Asset 123')).length, 1, 'Is able to publish 123')
t.equals(result.filter((entity) => entity.sys.id === '123').length, 1, 'Result contains the published entity')
teardown()
t.end()
})
})

test('Unpublish entities', (t) => {
setup()
const unpublishStub = sinon.stub().returns(Promise.resolve({sys: {type: 'Asset'}}))
Expand Down

0 comments on commit 96b4370

Please sign in to comment.