diff --git a/lib/zinnia.js b/lib/zinnia.js index 363f3b08..2b75e810 100644 --- a/lib/zinnia.js +++ b/lib/zinnia.js @@ -139,12 +139,12 @@ async function getContractsWithRetry (provider, abi) { } const runUpdateSourceFilesLoop = async ({ + controller, signal, onActivity, childProcesses, moduleVersionsDir, - moduleSourcesDir, - shouldRestart + moduleSourcesDir }) => { while (true) { const delay = 10 * 60 * 1000 // 10 minutes @@ -156,10 +156,13 @@ const runUpdateSourceFilesLoop = async ({ throw err } try { - shouldRestart.set(await updateAllSourceFiles({ + const shouldRestart = await updateAllSourceFiles({ moduleVersionsDir, moduleSourcesDir - })) + }) + if (shouldRestart) { + controller.abort() + } } catch (err) { onActivity({ type: 'error', @@ -168,7 +171,7 @@ const runUpdateSourceFilesLoop = async ({ console.error(err) maybeReportErrorToSentry(err) } - if (shouldRestart.get()) { + if (signal.aborted) { onActivity({ type: 'info', message: 'Updated Zinnia module source code, restarting...' @@ -178,9 +181,6 @@ const runUpdateSourceFilesLoop = async ({ } return } - if (signal.aborted) { - return - } } } @@ -205,7 +205,7 @@ const runUpdateContractsLoop = async ({ signal, provider, abi, contracts }) => { const catchChildProcessExit = async ({ childProcesses, - shouldRestart, + signal, onActivity }) => { try { @@ -223,7 +223,7 @@ const catchChildProcessExit = async ({ await Promise.race(tasks) } catch (err) { - if (!shouldRestart.get()) { + if (!signal.aborted) { const moduleName = capitalize(err.moduleName ?? 'Zinnia') const exitReason = err.exitReason ?? 'for unknown reason' const message = `${moduleName} crashed ${exitReason}` @@ -356,47 +356,36 @@ export async function run ({ }) } - const shouldRestart = { - value: false, - set (value) { - this.value = value - }, - get () { - return this.value - } - } const controller = new AbortController() const { signal } = controller await Promise.all([ runUpdateSourceFilesLoop({ + controller, signal, onActivity, childProcesses, moduleVersionsDir, - moduleSourcesDir, - shouldRestart + moduleSourcesDir }), runUpdateContractsLoop({ signal, provider, abi, contracts }), - catchChildProcessExit({ childProcesses, shouldRestart, onActivity }), + catchChildProcessExit({ childProcesses, onActivity, signal }), waitForStdioClose({ childProcesses, controller }) ]) - if (shouldRestart.get()) { - // This infinite recursion has no risk of exceeding the maximum call stack - // size, as awaiting promises unwinds the stack - return run({ - FIL_WALLET_ADDRESS, - ethAddress, - STATE_ROOT, - CACHE_ROOT, - moduleVersionsDir, - moduleSourcesDir, - onActivity, - onMetrics, - isUpdated: true - }) - } + // This infinite recursion has no risk of exceeding the maximum call stack + // size, as awaiting promises unwinds the stack + return run({ + FIL_WALLET_ADDRESS, + ethAddress, + STATE_ROOT, + CACHE_ROOT, + moduleVersionsDir, + moduleSourcesDir, + onActivity, + onMetrics, + isUpdated: true + }) } const jobsCompleted = {}