diff --git a/lib/zinnia.js b/lib/zinnia.js index 94e1d213..c34bd565 100644 --- a/lib/zinnia.js +++ b/lib/zinnia.js @@ -119,8 +119,9 @@ async function getContractAddresses () { return revision.value.split('\n').filter(Boolean) } -async function getContractsWithRetry (provider, abi) { +async function getContractsWithRetry ({ provider, abi, signal }) { const contractAddresses = await pRetry(getContractAddresses, { + signal, retries: 10, onFailedAttempt: err => { console.error(err) @@ -145,7 +146,6 @@ const runUpdateSourceFilesLoop = async ({ controller, signal, onActivity, - childProcesses, moduleVersionsDir, moduleSourcesDir }) => { @@ -164,7 +164,12 @@ const runUpdateSourceFilesLoop = async ({ moduleSourcesDir }) if (shouldRestart) { + onActivity({ + type: 'info', + message: 'Updated Zinnia module source code, restarting...' + }) controller.abort() + return } } catch (err) { onActivity({ @@ -174,16 +179,6 @@ const runUpdateSourceFilesLoop = async ({ console.error(err) maybeReportErrorToSentry(err) } - if (signal.aborted) { - onActivity({ - type: 'info', - message: 'Updated Zinnia module source code, restarting...' - }) - for (const childProcess of childProcesses) { - childProcess.kill() - } - return - } } } @@ -197,7 +192,7 @@ const runUpdateContractsLoop = async ({ signal, provider, abi, contracts }) => { if (err.name === 'AbortError') return throw err } - const newContracts = await getContractsWithRetry(provider, abi) + const newContracts = await getContractsWithRetry({ provider, abi, signal }) contracts.splice(0) contracts.push(...newContracts) if (signal.aborted) { @@ -208,6 +203,7 @@ const runUpdateContractsLoop = async ({ signal, provider, abi, contracts }) => { const catchChildProcessExit = async ({ childProcesses, + controller, signal, onActivity }) => { @@ -244,9 +240,7 @@ const catchChildProcessExit = async ({ throw err } } finally { - for (const childProcess of childProcesses) { - childProcess.kill() - } + controller.abort() } } @@ -293,7 +287,7 @@ export async function run ({ ) ) - const contracts = await getContractsWithRetry(provider, abi) + const contracts = await getContractsWithRetry({ provider, abi, signal: null }) const zinniadExe = getBinaryModuleExecutable({ module: 'zinnia', executable: 'zinniad' }) if (!isUpdated) { @@ -316,6 +310,8 @@ export async function run ({ } } + const controller = new AbortController() + const { signal } = controller const childProcesses = [] for (const { module } of ZINNIA_MODULES) { @@ -331,7 +327,8 @@ export async function run ({ FIL_WALLET_ADDRESS, STATE_ROOT, CACHE_ROOT - } + }, + signal } ) childProcesses.push(Object.assign(childProcess, { moduleName: module })) @@ -359,22 +356,22 @@ export async function run ({ }) } - const controller = new AbortController() - const { signal } = controller - - await Promise.all([ - runUpdateSourceFilesLoop({ - controller, - signal, - onActivity, - childProcesses, - moduleVersionsDir, - moduleSourcesDir - }), - runUpdateContractsLoop({ signal, provider, abi, contracts }), - catchChildProcessExit({ childProcesses, onActivity, signal }), - waitForStdioClose({ childProcesses, controller }) - ]) + try { + await Promise.all([ + runUpdateSourceFilesLoop({ + controller, + signal, + onActivity, + moduleVersionsDir, + moduleSourcesDir + }), + runUpdateContractsLoop({ signal, provider, abi, contracts }), + catchChildProcessExit({ childProcesses, onActivity, controller, signal }), + waitForStdioClose({ childProcesses, controller }) + ]) + } finally { + controller.abort() + } // This infinite recursion has no risk of exceeding the maximum call stack // size, as awaiting promises unwinds the stack