|
| 1 | +import {color, pg, utils} from '@heroku/heroku-cli-util' |
| 2 | +import {HTTPError} from '@heroku/http-call' |
| 3 | +import {flags as Flags} from '@heroku-cli/command' |
| 4 | +import {Args, ux} from '@oclif/core' |
| 5 | +import tsheredoc from 'tsheredoc' |
| 6 | + |
| 7 | +import BaseCommand from '../../../lib/data/baseCommand.js' |
| 8 | +import {WaitStatus} from '../../../lib/data/types.js' |
| 9 | +import notify from '../../../lib/notify.js' |
| 10 | + |
| 11 | +const heredoc = tsheredoc.default |
| 12 | + |
| 13 | +export default class DataPgWait extends BaseCommand { |
| 14 | + static args = { |
| 15 | + database: Args.string({ |
| 16 | + description: 'database name, database attachment name, or related config var on an app', |
| 17 | + required: true, |
| 18 | + }), |
| 19 | + } |
| 20 | + |
| 21 | + static description = 'show status of an operation until it\'s complete' |
| 22 | + |
| 23 | + static examples = [ |
| 24 | + heredoc(` |
| 25 | + # Wait for database to be available |
| 26 | + ${color.command('heroku data:pg:wait DATABASE --app myapp')} |
| 27 | + `), |
| 28 | + ] |
| 29 | + |
| 30 | + static flags = { |
| 31 | + app: Flags.app({required: true}), |
| 32 | + 'no-notify': Flags.boolean({ |
| 33 | + description: 'do not show OS notification', |
| 34 | + }), |
| 35 | + remote: Flags.remote(), |
| 36 | + 'wait-interval': Flags.integer({ |
| 37 | + default: 5, |
| 38 | + description: 'how frequently to poll in seconds (to avoid rate limiting)', |
| 39 | + min: 1, |
| 40 | + }), |
| 41 | + } |
| 42 | + |
| 43 | + public async notify(...args: Parameters<typeof notify>): Promise<void> { |
| 44 | + return notify(...args) |
| 45 | + } |
| 46 | + |
| 47 | + public async run(): Promise<void> { |
| 48 | + const {args, flags} = await this.parse(DataPgWait) |
| 49 | + const {database} = args |
| 50 | + const {app, 'no-notify': noNotify, 'wait-interval': waitInterval} = flags |
| 51 | + const databaseResolver = new utils.pg.DatabaseResolver(this.heroku) |
| 52 | + const db = await databaseResolver.getAttachment(app, database) |
| 53 | + const {addon} = db |
| 54 | + |
| 55 | + if (!utils.pg.isAdvancedDatabase(addon)) { |
| 56 | + ux.error(heredoc` |
| 57 | + You can only use this command on Advanced-tier databases. |
| 58 | + Run ${color.code(`heroku pg:wait ${addon.name} -a ${app}`)} instead.`) |
| 59 | + } |
| 60 | + |
| 61 | + await this.waitFor(addon, waitInterval || 5, noNotify) |
| 62 | + } |
| 63 | + |
| 64 | + public async wait(ms: number): Promise<void> { |
| 65 | + return new Promise(resolve => { |
| 66 | + setTimeout(resolve, ms) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + private async waitFor(addon: pg.ExtendedAddonAttachment['addon'], interval: number, noNotify: boolean): Promise<void> { |
| 71 | + let status: WaitStatus = {message: null, waiting: true} |
| 72 | + let waiting = false |
| 73 | + let retries = 20 |
| 74 | + const notFoundMessage = 'Waiting to provision...' |
| 75 | + |
| 76 | + while (status.waiting) { |
| 77 | + try { |
| 78 | + const response = await this.dataApi.get<WaitStatus>(`/data/postgres/v1/${addon.id}/wait_status`) |
| 79 | + status = response.body |
| 80 | + } catch (error) { |
| 81 | + const httpError = error as HTTPError |
| 82 | + if (!retries || httpError.statusCode !== 404) { |
| 83 | + if (waiting) { |
| 84 | + ux.action.stop(color.red('!')) |
| 85 | + } |
| 86 | + |
| 87 | + throw httpError |
| 88 | + } |
| 89 | + |
| 90 | + retries-- |
| 91 | + status = {message: notFoundMessage, waiting: true} |
| 92 | + } |
| 93 | + |
| 94 | + if (!status.waiting) { |
| 95 | + if (waiting) { |
| 96 | + ux.action.stop(status.message || 'available') |
| 97 | + } else { |
| 98 | + ux.stdout(`${color.datastore(addon.name)} is available.`) |
| 99 | + } |
| 100 | + |
| 101 | + break |
| 102 | + } |
| 103 | + |
| 104 | + if (!waiting) { |
| 105 | + waiting = true |
| 106 | + ux.action.start(`Waiting for database ${color.addon(addon.name)}`, status.message || undefined) |
| 107 | + } |
| 108 | + |
| 109 | + ux.action.status = status.message || undefined |
| 110 | + |
| 111 | + await this.wait(interval * 1000) |
| 112 | + } |
| 113 | + |
| 114 | + if (!noNotify && waiting) { |
| 115 | + this.notify('heroku data:pg:wait', `Database ${addon.name} is now available`) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments