Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions lib/zinnia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -145,7 +146,6 @@ const runUpdateSourceFilesLoop = async ({
controller,
signal,
onActivity,
childProcesses,
moduleVersionsDir,
moduleSourcesDir
}) => {
Expand All @@ -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({
Expand All @@ -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
}
}
}

Expand All @@ -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) {
Expand All @@ -208,6 +203,7 @@ const runUpdateContractsLoop = async ({ signal, provider, abi, contracts }) => {

const catchChildProcessExit = async ({
childProcesses,
controller,
signal,
onActivity
}) => {
Expand Down Expand Up @@ -244,9 +240,7 @@ const catchChildProcessExit = async ({
throw err
}
} finally {
for (const childProcess of childProcesses) {
childProcess.kill()
}
controller.abort()
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -316,6 +310,8 @@ export async function run ({
}
}

const controller = new AbortController()
const { signal } = controller
const childProcesses = []

for (const { module } of ZINNIA_MODULES) {
Expand All @@ -331,7 +327,8 @@ export async function run ({
FIL_WALLET_ADDRESS,
STATE_ROOT,
CACHE_ROOT
}
},
signal
}
)
childProcesses.push(Object.assign(childProcess, { moduleName: module }))
Expand Down Expand Up @@ -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
Expand Down