jamsocket
Version:
A CLI for the Jamsocket platform
70 lines (69 loc) • 3.68 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const jamsocket_1 = require("../../jamsocket");
const formatting_1 = require("../../lib/formatting");
class TerminateBackends extends core_1.Command {
async run() {
const { args, flags } = await this.parse(TerminateBackends);
const jamsocket = jamsocket_1.Jamsocket.fromEnvironment();
const service = args.service;
const before = new Date(flags.before).toISOString();
const dryRun = flags['dry-run'] ?? false;
const force = flags.force ?? false;
const beforeFormatted = `${(0, formatting_1.lightBlue)(new Date(before).toString())}`;
const dryRunResult = await jamsocket.terminateAllBackends(service, {
before,
hard: false,
dry_run: true,
});
if (dryRunResult.backend_count === 0) {
this.log(`There are no running backends spawned before ${beforeFormatted}.`);
return;
}
const isPlural = dryRunResult.backend_count > 1;
this.log(`There ${isPlural ? 'are' : 'is'} ${(0, formatting_1.lightBlue)(`${dryRunResult.backend_count}`)} running backend${isPlural ? 's' : ''} spawned before ${beforeFormatted} that will be terminated.`);
if (dryRun)
return;
this.log();
this.log((0, formatting_1.lightMagenta)('This command should only be run in emergencies. Terminating lots of backends at once may result in a large number of new backends being spawned which may hit rate limits.'));
this.log();
this.log('This command may take several seconds to complete. And may need to be run multiple times with the same input in order to terminate all backends before the given timestamp.');
this.log();
const confirmation = await core_1.CliUx.ux.prompt(`Are you sure you want to terminate ${(0, formatting_1.lightBlue)(`${dryRunResult.backend_count}`)} backends? (Type "yes" to continue)`);
if (confirmation.trim().toLowerCase() !== 'yes') {
this.log('Termination canceled.');
return;
}
const result = await jamsocket.terminateAllBackends(service, {
before,
hard: force,
dry_run: dryRun,
});
this.log(`Successfully sent termination requests for ${(0, formatting_1.lightBlue)(`${result.terminated_count}`)} of ${(0, formatting_1.lightBlue)(`${dryRunResult.backend_count}`)} backends.`);
if (!result.is_complete) {
this.log('Termination is not complete. Run the command again with the same input to terminate the remaining backends.');
}
}
}
TerminateBackends.description = 'Terminates all backends for the given service that were spawned before the given timestamp.';
TerminateBackends.aliases = ['terminate-all-backends'];
TerminateBackends.examples = ['<%= config.bin %> <%= command.id %> my-service --before 2025-01-01T00:00:00Z'];
TerminateBackends.hidden = true;
TerminateBackends.args = [{ name: 'service', required: true }];
TerminateBackends.flags = {
before: core_1.Flags.string({
char: 'b',
description: 'all the service\'s backends that were spawned before this timestamp will be terminated (e.g. "2025-01-01T00:00:00Z")',
required: true,
}),
'dry-run': core_1.Flags.boolean({
char: 'd',
description: 'whether to perform a dry run (defaults to false)',
}),
force: core_1.Flags.boolean({
char: 'f',
description: 'whether to force the backends to hard terminate (defaults to false)',
}),
};
exports.default = TerminateBackends;
;