Skip to content

Commit

Permalink
refactor: use aws waiters
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Goller <[email protected]>
  • Loading branch information
goller committed Nov 5, 2024
1 parent 4d02a9c commit ffc5a68
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/utils/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TerminateInstancesCommand,
Volume,
paginateDescribeInstances,
waitUntilInstanceExists,
} from '@aws-sdk/client-ec2'
import {
GetDesiredStateResponse,
Expand Down Expand Up @@ -349,14 +350,16 @@ systemctl start machine-agent.service
}

const instanceID = instance.Instances[0].InstanceId
let instanceCreated = false

// We are waiting for the instance to exist because we are blocking scheduleTask
// from starting another task with the same machine ID until this one completes.
while (!instanceCreated) {
const res = await client.send(new DescribeInstancesCommand({InstanceIds: [instanceID]}))
const instances = res.Reservations?.flatMap((r) => r.Instances || []) || []
instanceCreated = instances.length > 0

const MAX_WAIT = 30
try {
await waitUntilInstanceExists({client, maxWaitTime: MAX_WAIT}, {InstanceIds: [instanceID]})
} catch (caught) {
if (caught instanceof Error && caught.name === 'TimeoutError') {
console.log(`instance ${instanceID} did not exist after ${MAX_WAIT} seconds`)
} else {
console.log(`Error waiting for instance ${instanceID} to exist`, caught)
}
}
}

Expand Down

0 comments on commit ffc5a68

Please sign in to comment.