From 7029653c0086cd185fbd5b749d1b0f52fd0274a6 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Tue, 31 Jan 2017 10:29:57 -0800 Subject: [PATCH] [local-sync] Retry syncback tasks that throw retryable errors Summary: This will prevent us from showing error messages to the user when we can automatically recover from the error Test Plan: manual-- throw error from syncback task, check expected results Reviewers: evan, mark, spang Reviewed By: spang Differential Revision: https://phab.nylas.com/D3812 --- .../syncback-task-helpers.es6 | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/local-sync/src/local-sync-worker/syncback-task-helpers.es6 b/packages/local-sync/src/local-sync-worker/syncback-task-helpers.es6 index 0e1f71ab8..1c9bde346 100644 --- a/packages/local-sync/src/local-sync-worker/syncback-task-helpers.es6 +++ b/packages/local-sync/src/local-sync-worker/syncback-task-helpers.es6 @@ -1,5 +1,7 @@ +const {IMAPErrors} = require('isomorphic-core') const SyncbackTaskFactory = require('./syncback-task-factory'); +const MAX_TASK_RETRIES = 2 // TODO NOTE! These are the tasks we exclude from the sync loop. This should be // refactored later. @@ -118,11 +120,22 @@ export async function runSyncbackTask({task, runTask} = {}) { syncbackRequest.status = "INPROGRESS"; await syncbackRequest.save(); - // TODO `runTask` is a hack to allow tasks to be executed outside the - // context of an imap connection, specifically to allow running send tasks - // outside of the sync loop. This should be solved in a better way or - // probably refactored when we implement the sync scheduler - const responseJSON = await runTask(task) + let responseJSON; + for (let numTries = 0; numTries <= MAX_TASK_RETRIES; numTries++) { + try { + // TODO `runTask` is a hack to allow tasks to be executed outside the + // context of an imap connection, specifically to allow running send tasks + // outside of the sync loop. This should be solved in a better way or + // probably refactored when we implement the sync scheduler + responseJSON = await runTask(task) + break; + } catch (err) { + if (!(err instanceof IMAPErrors.RetryableError) || numTries >= MAX_TASK_RETRIES) { + throw err + } + } + } + syncbackRequest.status = "SUCCEEDED"; syncbackRequest.responseJSON = responseJSON || {}; const after = new Date();