Skip to content

Commit

Permalink
feat(stock-import): add repeater on 409 errors (#67)
Browse files Browse the repository at this point in the history
fixes #66
  • Loading branch information
lojzatran authored and hisabimbola committed Oct 16, 2017
1 parent e77a431 commit b371965
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/coffee/stockimport.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ xmlHelpers = require './xmlhelpers'
class StockImport

constructor: (@logger, options = {}) ->
options = _.defaults options, {user_agent: 'sphere-stock-import'}
options = _.defaults options, {user_agent: 'sphere-stock-import', max409Retries: 10}
@sync = new InventorySync
@client = new SphereClient options
@csvHeaders = options.csvHeaders
@csvDelimiter = options.csvDelimiter
@customFieldMappings = new CustomFieldMappings()
@max409Retries = options.max409Retries
@_resetSummary()

_resetSummary: ->
Expand Down Expand Up @@ -349,15 +350,32 @@ class StockImport
posts = _.map inventoryEntries, (entry) =>
existingEntry = @_match(entry, existingEntries)
if existingEntry?
synced = @sync.buildActions(entry, existingEntry)
if synced.shouldUpdate()
@client.inventoryEntries.byId(synced.getUpdateId()).update(synced.getUpdatePayload())
else
Promise.resolve statusCode: 304
@_updateInventory(entry, existingEntry)
else
@client.inventoryEntries.create(entry)

debug 'About to send %s requests', _.size(posts)
Promise.all(posts)

_updateInventory: (entry, existingEntry, tryCount = 1) =>
synced = @sync.buildActions(entry, existingEntry)
if synced.shouldUpdate()
@client.inventoryEntries.byId(synced.getUpdateId()).update(synced.getUpdatePayload())
.catch (err) =>
if (err.statusCode == 409)
debug "Got 409 error for entry #{JSON.stringify(entry)}, repeat the request "
+ "for #{tryCount} times."
if (tryCount <= @max409Retries)
@client.inventoryEntries
.byId(synced.getUpdateId())
.fetch()
.then (result) =>
@_updateInventory(entry, result.body, tryCount + 1)
else
Promise.reject new Error("Retry limit #{max409Retries} reached for stock #{JSON.stringify(entry)}")
else
Promise.reject err
else
Promise.resolve statusCode: 304

module.exports = StockImport
22 changes: 22 additions & 0 deletions src/spec/stockimport.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,28 @@ describe 'StockImport', ->
done()
.catch (err) -> done(err)

it 'should repeat on 409 error', (done) ->
methodCalledCounter = 0
inventoryEntries = [
{sku: 'foo', quantityOnStock: 2},
{sku: 'foo', quantityOnStock: 3, supplyChannel: {typeId: 'channel', id: '111'}}
]
existingEntries = [{id: '123', version: 1, sku: 'foo', quantityOnStock: 1}]

spyOn(@import.client.inventoryEntries, 'update').andCallFake () ->
methodCalledCounter++
return Promise.reject({ statusCode: 409 })

spyOn(@import.client.inventoryEntries, 'fetch').andCallFake ->
new Promise (resolve, reject) -> resolve({body: existingEntries[0]})

@import._createOrUpdate inventoryEntries, existingEntries
.then =>
done('Test should fail')
.catch () ->
expect(methodCalledCounter).toEqual(11)
done()


describe '::_match', ->

Expand Down

0 comments on commit b371965

Please sign in to comment.