Skip to content

Commit

Permalink
Merge pull request #506 from ItinerisLtd/clickup/86bwqzkdc/kinsta-sit…
Browse files Browse the repository at this point in the history
…es-new-watch-operations

feat(kinsta): wait for operation to complete for site create
  • Loading branch information
codepuncher authored Dec 13, 2023
2 parents 6e621c1 + 7ff6077 commit cf5c416
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/commands/kinsta/sites/new.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 {createSite, getRegions} from '../../../lib/kinsta.js'
import {createSite, getOperationStatus, getRegions} from '../../../lib/kinsta.js'

export default class New extends KinstaCommand {
static description = 'Create a new Kinsta site'
Expand All @@ -24,12 +24,35 @@ export default class New extends KinstaCommand {
public async run(): Promise<void> {
const {flags} = await this.parse(New)

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

this.log(`Site created. Check MyKinsta to check progress.`)
const {operation_id: operationId} = response

let operationStatus = null
let operationStatusCode = 404
let requestsCount = 0
while (requestsCount < 10 && operationStatusCode === 404) {
// 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(5000)

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

if (operationStatusCode !== 202 && operationStatus !== null) {
this.log(`Operation ID: ${operationId}`)
this.error(operationStatus.data.message, {exit: 2})
}

ux.action.stop(`Site created. Operation ID: ${operationId}`)
}
}

0 comments on commit cf5c416

Please sign in to comment.