Skip to content

Commit 1f5accd

Browse files
committed
fix(vms): retry mechanism for VM startup
1 parent 1e74a4c commit 1f5accd

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

packages/backend/src/functions/circuit.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
sleep,
5858
uploadFileToBucket
5959
} from "../lib/utils"
60+
import { EC2Client } from "@aws-sdk/client-ec2"
6061

6162
dotenv.config()
6263

@@ -387,6 +388,34 @@ export const coordinateCeremonyParticipant = functionsV1
387388
}
388389
})
389390

391+
392+
/**
393+
* Recursive function to check whether an EC2 is in a running state
394+
* @notice required step to run commands
395+
* @param ec2 <EC2Client> - the EC2Client object
396+
* @param vmInstanceId <string> - the instance Id
397+
* @param attempts <number> - how many times to retry before failing
398+
* @returns <Promise<boolean>> - whether the VM was started
399+
*/
400+
const checkIfVMRunning = async (
401+
ec2: EC2Client,
402+
vmInstanceId: string,
403+
attempts = 5
404+
): Promise<boolean> => {
405+
// if we tried 5 times, then throw an error
406+
if (attempts <= 0) logAndThrowError(SPECIFIC_ERRORS.SE_VM_NOT_RUNNING)
407+
408+
await sleep(60000); // Wait for 1 min
409+
const isVMRunning = await checkIfRunning(ec2, vmInstanceId)
410+
411+
if (!isVMRunning) {
412+
printLog(`VM not running, ${attempts - 1} attempts remaining. Retrying in 1 minute...`, LogLevel.DEBUG)
413+
return await checkIfVMRunning(ec2, vmInstanceId, attempts - 1)
414+
} else {
415+
return true
416+
}
417+
}
418+
390419
/**
391420
* Verify the contribution of a participant computed while contributing to a specific circuit of a ceremony.
392421
* @dev a huge amount of resources (memory, CPU, and execution time) is required for the contribution verification task.
@@ -729,10 +758,10 @@ export const verifycontribution = functionsV2.https.onCall(
729758
// Step (1.A.3.1).
730759
await startEC2Instance(ec2, vmInstanceId)
731760

732-
await sleep(180000) // nb. wait for VM startup (3 mins).
761+
await sleep(60000) // nb. wait for VM startup (1 mins + retry).
733762

734763
// Check if the startup is running.
735-
isVMRunning = await checkIfRunning(ec2, vmInstanceId)
764+
isVMRunning = await checkIfVMRunning(ec2, vmInstanceId)
736765

737766
printLog(`VM running: ${isVMRunning}`, LogLevel.DEBUG)
738767

0 commit comments

Comments
 (0)