diff --git a/src/FileSequence.js b/src/FileSequence.js index cfca4992d..8fffd7c03 100644 --- a/src/FileSequence.js +++ b/src/FileSequence.js @@ -30,6 +30,7 @@ function newFileSequence () { thisObject.eventHandler = newEventHandler() let eventSubscriptionIdDatasetUpdated + let callerId return thisObject @@ -73,8 +74,10 @@ function newFileSequence () { fileCloud = newFileCloud() fileCloud.initialize(bot) + callerId = newUniqueId() + let key = devTeam.codeName + '-' + bot.codeName + '-' + product.codeName + '-' + thisSet.codeName - systemEventHandler.listenToEvent(key, 'Dataset Updated', undefined, key, onResponse, updateFiles) + systemEventHandler.listenToEvent(key, 'Dataset Updated', undefined, callerId, onResponse, updateFiles) function onResponse (message) { eventSubscriptionIdDatasetUpdated = message.eventSubscriptionId @@ -87,7 +90,6 @@ function newFileSequence () { function onSequenceFileReceived (err, file) { try { if (finalized === true) { return } - initialized = true switch (err.result) { case GLOBAL.DEFAULT_OK_RESPONSE.result: { @@ -101,14 +103,18 @@ function newFileSequence () { case GLOBAL.CUSTOM_OK_RESPONSE.result: { if (ERROR_LOG === true) { logger.write('[INFO] initialize -> onSequenceFileReceived -> err.message = ' + err.message) } - + initialized = true callBackFunction(err) return } case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { + if (err.message === 'File does not exist.') { // We will assume that the process which generates the files has never been started, that does not imply that we can wait for data to come later. + initialized = true + callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE) + return + } if (ERROR_LOG === true) { logger.write('[INFO] initialize -> onSequenceFileReceived -> err.message = ' + err.message) } - callBackFunction(err, thisObject) return } @@ -118,7 +124,7 @@ function newFileSequence () { return } } - + initialized = true maxSequence = Number(file) /* Now we will get the sequence of files */ @@ -177,8 +183,6 @@ function newFileSequence () { try { if (finalized === true || initialized === false) { return } - logger.write('[INFO] updateFiles -> Entering function.') - /* To keep this data structure up-to-date we need to: @@ -193,7 +197,7 @@ function newFileSequence () { fileCloud.getFile(devTeam, bot, thisSet, exchange, market, undefined, undefined, 'Sequence', undefined, onSequenceFileReceived) - function onSequenceFileReceived (err, file) { + function onSequenceFileReceived (err, sequenceFile) { try { if (finalized === true) { return } switch (err.result) { @@ -206,22 +210,23 @@ function newFileSequence () { } case GLOBAL.CUSTOM_OK_RESPONSE.result: { - if (ERROR_LOG === true) { logger.write('[INFO] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) } + if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) } return } case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { - if (ERROR_LOG === true) { logger.write('[INFO] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) } + if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) } return } default: { - if (ERROR_LOG === true) { logger.write('[INFO] updateFiles -> onSequenceFileReceived -> Received Unexpected Response.') } + if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> Received Unexpected Response.') } return } } - maxSequence = Number(file) + maxSequence = Number(sequenceFile) + filesLoaded = 0 /* Now we will get the sequence of files, but in this case only from the currentMaxSequence and above. */ @@ -235,26 +240,18 @@ function newFileSequence () { case GLOBAL.DEFAULT_OK_RESPONSE.result: { break } - case GLOBAL.DEFAULT_FAIL_RESPONSE.result: { return } - case GLOBAL.CUSTOM_FAIL_RESPONSE.result: { - if (ERROR_LOG === true) { logger.write('[INFO] updateFiles -> onSequenceFileReceived -> onFileReceived -> err.message = ' + err.message) } - return - } - - default: { + if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> onFileReceived -> err.message = ' + err.message) } return } } files.set(i, file) - if (i !== currentMaxSequence) { - filesLoaded++ - } + filesLoaded++ if (filesLoaded === maxSequence + 1) { thisObject.eventHandler.raiseEvent('Files Updated', undefined)