Skip to content

Commit

Permalink
fix(kinsta): wait for php version to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
codepuncher committed Dec 13, 2023
1 parent 8f5716c commit dab814a
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/commands/kinsta/sites/tools/php-version-set.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Flags} from '@oclif/core'
import {Flags, ux} from '@oclif/core'
import {KinstaCommand} from '../../../../lib/commands/kinsta-command.js'
import {setPhpVersion} from '../../../../lib/kinsta.js'
import {getOperationStatus, setPhpVersion} from '../../../../lib/kinsta.js'

export default class PhpVersionSet extends KinstaCommand {
static description = 'Set an environments PHP version.'
Expand All @@ -21,12 +21,38 @@ export default class PhpVersionSet extends KinstaCommand {
public async run(): Promise<void> {
const {flags} = await this.parse(PhpVersionSet)

ux.action.start('Changing PHP version')
const response = await setPhpVersion(flags.apiKey, flags)
if (response?.status !== 202) {
console.log(response)
this.error(response.message, {exit: 2})
}

this.log(`${response.message}\nOperation ID: ${response.operation_id}`)
const {operation_id: operationId} = response
const secondsToWait = 5
let operationStatus = null
let operationStatusCode = 404

do {
// This is to make sure that Kinsta have created the operation for us to query.
// If we send the request too soon, it will not be ready to view.
// eslint-disable-next-line no-await-in-loop
await ux.wait(secondsToWait * 1000)

// eslint-disable-next-line no-await-in-loop
operationStatus = await getOperationStatus(flags.apiKey, operationId)
operationStatusCode = operationStatus.status

if (operationStatusCode >= 500) {
this.error(operationStatus.data.message)
}
} while (operationStatusCode !== 200)

if (operationStatus === null) {
this.error('Failed to change PHP version. Try again via MyKinsta UI')
}

ux.action.stop('Done.')
this.log(`Operation ID: ${operationId}`)
}
}

0 comments on commit dab814a

Please sign in to comment.