Skip to content

Commit 96b4370

Browse files
authored
fix(publishing): queue does now abort automatically (#119)
1 parent 5b3e524 commit 96b4370

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

lib/push/publishing.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import log from 'npmlog'
33
import getEntityName from './get-entity-name'
44
import errorBuffer from '../utils/error-buffer'
55

6+
let lastQueueLength = 0
7+
68
function runQueue (queue, result) {
79
if (!result) {
810
result = []
@@ -37,6 +39,13 @@ function runQueue (queue, result) {
3739
.then((entities) => entities.filter((entity) => entity))
3840
.then((entities) => {
3941
if (entities.length > 0) {
42+
if (lastQueueLength && lastQueueLength === entities.length) {
43+
errorBuffer.push(new Error('Queue was not able to publish at least one entity. Aborting.'))
44+
const failedEntities = entities.map((entitiy) => entitiy.sys.id)
45+
return result.filter((entity) => !failedEntities.includes(entity.sys.id))
46+
}
47+
lastQueueLength = entities.length
48+
4049
log.info(`Found ${entities.length} unpublished entities`)
4150
return runQueue(entities, result)
4251
}
@@ -58,6 +67,7 @@ export function publishEntities (entities) {
5867
const type = entity.sys.type || 'unknown type'
5968
log.info(`Starting publishing ${entities.length} ${type}s`)
6069

70+
lastQueueLength = entities.length
6171
return runQueue(entities)
6272
.then((result) => {
6373
log.info(`Finished publishing ${entities.length} ${type}s. Returning ${result.length} entities`)

test/push/publishing-test.js

+36-17
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,11 @@ test('Fails to publish entities', (t) => {
4444
const publishStub = sinon.stub()
4545
publishStub.onFirstCall().returns(Promise.resolve({sys: {type: 'Asset', publishedVersion: 2}}))
4646
const apiError = {
47-
message: 'Validation error',
4847
status: 422,
49-
sys: {
50-
type: 'Error',
51-
id: 'UnresolvedLinks'
52-
},
5348
details: {
5449
errors: [
5550
{
56-
name: 'notResolvable',
57-
link: {
58-
type: 'Link',
59-
linkType: 'Entry',
60-
id: 'linkedEntryId'
61-
},
62-
path: [
63-
'fields',
64-
'category',
65-
'en-US',
66-
0
67-
]
51+
name: 'notResolvable'
6852
}
6953
]
7054
}
@@ -88,6 +72,41 @@ test('Fails to publish entities', (t) => {
8872
})
8973
})
9074

75+
test('Queue does abort itself', (t) => {
76+
setup()
77+
const publishStub = sinon.stub()
78+
const apiError = {
79+
status: 422,
80+
details: {
81+
errors: [
82+
{
83+
name: 'notResolvable'
84+
}
85+
]
86+
}
87+
}
88+
const errorValidation = new Error(JSON.stringify(apiError))
89+
// First call resolves, others fail
90+
publishStub.returns(Promise.reject(errorValidation))
91+
publishStub.onFirstCall().returns(Promise.resolve({sys: {type: 'Asset', publishedVersion: 2, id: '123'}}))
92+
return publishEntities([
93+
{ sys: {id: '123', type: 'asset'}, publish: publishStub },
94+
{ sys: {id: '456', type: 'asset'}, publish: publishStub }
95+
])
96+
.then((result) => {
97+
const logs = logMock.info.args.map((args) => args[0])
98+
t.equals(publishStub.callCount, 3, 'publishes the first, retries the second only once')
99+
t.equals(errorBufferMock.push.callCount, 4, 'logs 4 errors')
100+
t.equals(errorBufferMock.push.lastCall.args[0].message, 'Queue was not able to publish at least one entity. Aborting.', 'Aborted queue with error')
101+
t.equals(logs.filter((log) => log.includes('Starting new publishing queue')).length, 2, 'Starts queue twice')
102+
t.equals(logs.filter((log) => log.includes('Unable to resolve 456 (456)')).length, 2, 'Is unable to resolve 456 twice')
103+
t.equals(logs.filter((log) => log.includes('Published Asset 123')).length, 1, 'Is able to publish 123')
104+
t.equals(result.filter((entity) => entity.sys.id === '123').length, 1, 'Result contains the published entity')
105+
teardown()
106+
t.end()
107+
})
108+
})
109+
91110
test('Unpublish entities', (t) => {
92111
setup()
93112
const unpublishStub = sinon.stub().returns(Promise.resolve({sys: {type: 'Asset'}}))

0 commit comments

Comments
 (0)