Skip to content

Commit

Permalink
feat(deployment): add dry run option for provider clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Nov 27, 2024
1 parent b3de893 commit 4cc6186
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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");
}
});

Expand Down
1 change: 1 addition & 0 deletions apps/api/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ program
.command("cleanup-provider-deployments")
.description("Close trial deployments for a provider")
.option("-c, --concurrency <number>", "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 <string>", "Provider address", value => z.string().parse(value))
.action(async (options, command) => {
await executeCliHandler(command.name(), async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
};
}
}

0 comments on commit 4cc6186

Please sign in to comment.