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
63 changes: 26 additions & 37 deletions lib/zinnia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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...'
Expand All @@ -178,9 +181,6 @@ const runUpdateSourceFilesLoop = async ({
}
return
}
if (signal.aborted) {
return
}
}
}

Expand All @@ -205,7 +205,7 @@ const runUpdateContractsLoop = async ({ signal, provider, abi, contracts }) => {

const catchChildProcessExit = async ({
childProcesses,
shouldRestart,
signal,
onActivity
}) => {
try {
Expand All @@ -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}`
Expand Down Expand Up @@ -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()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could remove this condition because here we know we always want to restart

// 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 = {}
Expand Down