diff --git a/apps/api/src/billing/services/provider-cleanup/provider-cleanup.service.ts b/apps/api/src/billing/services/provider-cleanup/provider-cleanup.service.ts index 04f7e926d..df6ff4bb8 100644 --- a/apps/api/src/billing/services/provider-cleanup/provider-cleanup.service.ts +++ b/apps/api/src/billing/services/provider-cleanup/provider-cleanup.service.ts @@ -7,10 +7,12 @@ import { ManagedUserWalletService, RpcMessageService } from "@src/billing/servic import { ErrorService } from "@src/core/services/error/error.service"; import { DeploymentRepository } from "@src/deployment/repositories/deployment/deployment.repository"; import { TxSignerService } from "../tx-signer/tx-signer.service"; +import { ProviderCleanupSummarizer } from "@src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer"; export interface ProviderCleanupParams { concurrency: number; providerAddress: string; + dryRun: boolean; } @singleton() @@ -28,6 +30,7 @@ export class ProviderCleanupService { ) {} async cleanup(options: ProviderCleanupParams) { + const summary = new ProviderCleanupSummarizer(); await this.userWalletRepository.paginate({ query: { isTrialing: true }, limit: options.concurrency || 10 }, async wallets => { const cleanUpAllWallets = wallets.map(async wallet => { await this.errorService.execWithErrorHandler( @@ -36,15 +39,17 @@ export class ProviderCleanupService { event: "PROVIDER_CLEAN_UP_ERROR", context: ProviderCleanupService.name }, - () => this.cleanUpForWallet(wallet, options) + () => this.cleanUpForWallet(wallet, options, summary) ); }); await Promise.all(cleanUpAllWallets); }); + + this.logger.info({ event: "PROVIDER_CLEAN_UP_SUMMARY", summary: summary.summarize(), dryRun: options.dryRun }); } - private async cleanUpForWallet(wallet: UserWalletOutput, options: ProviderCleanupParams) { + private async cleanUpForWallet(wallet: UserWalletOutput, options: ProviderCleanupParams, summary: ProviderCleanupSummarizer) { const client = await this.txSignerService.getClientForAddressIndex(wallet.id); const deployments = await this.deploymentRepository.findDeploymentsForProvider({ owner: wallet.address, @@ -56,22 +61,27 @@ export class ProviderCleanupService { this.logger.info({ event: "PROVIDER_CLEAN_UP", params: { owner: wallet.address, dseq: deployment.dseq } }); try { - await client.signAndBroadcast([message]); - this.logger.info({ event: "PROVIDER_CLEAN_UP_SUCCESS" }); - } catch (error) { - if (error.message.includes("not allowed to pay fees")) { - await this.managedUserWalletService.authorizeSpending({ - address: wallet.address, - limits: { - fees: this.config.FEE_ALLOWANCE_REFILL_AMOUNT - } - }); - + if (!options.dryRun) { await client.signAndBroadcast([message]); this.logger.info({ event: "PROVIDER_CLEAN_UP_SUCCESS" }); + } + } catch (error) { + if (error.message.includes("not allowed to pay fees")) { + if (!options.dryRun) { + await this.managedUserWalletService.authorizeSpending({ + address: wallet.address, + limits: { + fees: this.config.FEE_ALLOWANCE_REFILL_AMOUNT + } + }); + await client.signAndBroadcast([message]); + this.logger.info({ event: "PROVIDER_CLEAN_UP_SUCCESS" }); + } } else { throw error; } + } finally { + summary.inc("deploymentCount"); } }); diff --git a/apps/api/src/console.ts b/apps/api/src/console.ts index e9482a49a..115bc32b1 100644 --- a/apps/api/src/console.ts +++ b/apps/api/src/console.ts @@ -54,6 +54,7 @@ program .command("cleanup-provider-deployments") .description("Close trial deployments for a provider") .option("-c, --concurrency ", "How many wallets is processed concurrently", value => z.number({ coerce: true }).optional().default(10).parse(value)) + .option("-d, --dry-run", "Dry run the trial provider cleanup", false) .option("-p, --provider ", "Provider address", value => z.string().parse(value)) .action(async (options, command) => { await executeCliHandler(command.name(), async () => { diff --git a/apps/api/src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer.ts b/apps/api/src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer.ts new file mode 100644 index 000000000..a2e82a84e --- /dev/null +++ b/apps/api/src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer.ts @@ -0,0 +1,25 @@ +interface ProviderCleanupSummary { + deploymentCount: number; +} + +export class ProviderCleanupSummarizer { + private deploymentCount = 0; + + inc(param: keyof ProviderCleanupSummary, value = 1) { + this[param] += value; + } + + set(param: keyof ProviderCleanupSummary, value: number) { + this[param] = value; + } + + get(param: keyof ProviderCleanupSummary) { + return this[param]; + } + + summarize(): ProviderCleanupSummary { + return { + deploymentCount: this.deploymentCount + }; + } +}