Skip to content

Commit

Permalink
Add logs, fix identifying Fly volumes by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Sep 24, 2024
1 parent d0f0b81 commit 15d2b45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/utils/fly/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {promises} from '../common'
import {CLOUD_AGENT_CONNECTION_ID, FLY_REGION} from '../env'
import {errorMessage} from '../errors'
import {client} from '../grpc'
import {logger} from '../logger'
import {toPlainObject} from '../plain'
import {scheduleTask} from '../scheduler'
import {
Expand Down Expand Up @@ -53,19 +54,25 @@ export async function getCurrentState() {
}

export async function reconcile(response: GetDesiredStateResponse, state: CurrentState): Promise<void> {
logger.info('Reconciling state')

for (const volume of response.newVolumes) {
logger.info(`new volume requested: ${JSON.stringify(volume)}`)
void scheduleTask(`volume/new/${volume.id}`, () => reconcileNewVolume(state.volumes, volume))
}

for (const volume of response.volumeChanges) {
logger.info(`volume change requested: ${JSON.stringify(volume)}`)
void scheduleTask(`volume/change/${volume.resourceId}`, () => reconcileVolume(state, volume))
}

for (const machine of response.newMachines) {
logger.info(`new machine requested: ${JSON.stringify(machine)}`)
void scheduleTask(`machine/new/${machine.id}`, () => reconcileNewMachine(state.machines, machine, state.volumes))
}

for (const machine of response.machineChanges) {
logger.info(`machine change requested: ${JSON.stringify(machine)}`)
void scheduleTask(`machine/change/${machine.resourceId}`, () => reconcileMachine(state.machines, machine))
}
}
Expand Down Expand Up @@ -123,7 +130,8 @@ async function reconcileNewMachine(state: V1Machine[], machine: GetDesiredStateR
if (!machine.flyOptions) return
const flyOptions = machine.flyOptions

const volume = volumes.find((v) => v.name === flyOptions.volumeId)
const volume =
volumes.find((v) => v.id === flyOptions.volumeId) ?? volumes.find((v) => v.name === flyOptions.volumeId)
if (!volume) return

if (machine.architecture !== GetDesiredStateResponse_Architecture.X86) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export async function scheduleTask(key: string, task: () => Promise<void>) {
return
}

const start = new Date()

try {
inProgressTasks.add(key)
console.log(`Accepted ${key}, starting task`)
Expand All @@ -19,6 +21,7 @@ export async function scheduleTask(key: string, task: () => Promise<void>) {
await reportError(err)
} finally {
inProgressTasks.delete(key)
console.log(`Task ${key} completed`)
const duration = new Date().getTime() - start.getTime()
console.log(`Task ${key} completed (${duration}ms)`)
}
}

0 comments on commit 15d2b45

Please sign in to comment.