Skip to content

Commit

Permalink
fix: prevent creating duplicate machine ids
Browse files Browse the repository at this point in the history
We saw the same machine id created more than one time.
This caused an issue where the cli would not be able
to connect to the correct machine.

We now wait for the instance to be created.
We don't care about what state the instance is in
only that it has been handled by AWS.

We believe duplication would happen when the scheduleTask
would finish quickly, receive the next update from the API,
but without AWS reporting the machine as pending.

Co-authored-by: Billy Batista <[email protected]>
Signed-off-by: Chris Goller <[email protected]>
  • Loading branch information
goller and billyb2 committed Nov 5, 2024
1 parent d53fbf1 commit 4d02a9c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/utils/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ systemctl start machine-agent.service
}

console.log(`Creating new instance for machine ID ${machine.id}`)
await client.send(
const instance = await client.send(
new RunInstancesCommand({
LaunchTemplate: {
LaunchTemplateId:
Expand Down Expand Up @@ -339,6 +339,25 @@ systemctl start machine-agent.service
UserData: Buffer.from(userData).toString('base64'),
}),
)

if (!instance.Instances || instance.Instances.length === 0 || !instance.Instances[0].InstanceId) {
// TODO: Will this happen?
// If this does happen you'll need to run a different kind of description
// with a filter on the tag machine id.
console.log(`No instances created for machine ID ${machine.id}`)
return
}

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
}
}

function currentMachineState(instance: Instance): GetDesiredStateResponse_MachineState {
Expand Down

0 comments on commit 4d02a9c

Please sign in to comment.