-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deployment): clean up trial deployments for a provider
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
apps/api/src/billing/services/provider-cleanup/provider-cleanup.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { LoggerService } from "@akashnetwork/logging"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { UserWalletOutput, UserWalletRepository } from "@src/billing/repositories"; | ||
import { ManagedUserWalletService, RpcMessageService } from "@src/billing/services"; | ||
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"; | ||
|
||
export interface ProviderCleanupParams { | ||
concurrency: number; | ||
providerAddress: string; | ||
} | ||
|
||
@singleton() | ||
export class ProviderCleanupService { | ||
private readonly logger = LoggerService.forContext(ProviderCleanupService.name); | ||
|
||
constructor( | ||
@InjectBillingConfig() private readonly config: BillingConfig, | ||
private readonly userWalletRepository: UserWalletRepository, | ||
private readonly managedUserWalletService: ManagedUserWalletService, | ||
private readonly txSignerService: TxSignerService, | ||
private readonly deploymentRepository: DeploymentRepository, | ||
private readonly rpcMessageService: RpcMessageService, | ||
private readonly errorService: ErrorService | ||
) {} | ||
|
||
async cleanup(options: ProviderCleanupParams) { | ||
await this.userWalletRepository.paginate({ query: { isTrialing: true }, limit: options.concurrency || 10 }, async wallets => { | ||
const cleanUpAllWallets = wallets.map(async wallet => { | ||
await this.errorService.execWithErrorHandler( | ||
{ | ||
wallet, | ||
event: "PROVIDER_CLEAN_UP_ERROR", | ||
context: ProviderCleanupService.name | ||
}, | ||
() => this.cleanUpForWallet(wallet, options) | ||
); | ||
}); | ||
|
||
await Promise.all(cleanUpAllWallets); | ||
}); | ||
} | ||
|
||
private async cleanUpForWallet(wallet: UserWalletOutput, options: ProviderCleanupParams) { | ||
const client = await this.txSignerService.getClientForAddressIndex(wallet.id); | ||
const deployments = await this.deploymentRepository.findDeploymentsForProvider({ | ||
owner: wallet.address, | ||
provider: options.providerAddress | ||
}); | ||
|
||
const closeAllWalletStaleDeployments = deployments.map(async deployment => { | ||
const message = this.rpcMessageService.getCloseDeploymentMsg(wallet.address, deployment.dseq); | ||
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 | ||
} | ||
}); | ||
|
||
await client.signAndBroadcast([message]); | ||
this.logger.info({ event: "PROVIDER_CLEAN_UP_SUCCESS" }); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}); | ||
|
||
await Promise.all(closeAllWalletStaleDeployments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
apps/api/src/deployment/controllers/provider/provider.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import { singleton } from "tsyringe"; | ||
|
||
import { TrialProvidersService } from "@src/deployment/services/trial-providers/trial-providers.service"; | ||
import { ProviderCleanupParams, ProviderCleanupService } from "@src/billing/services/provider-cleanup/provider-cleanup.service"; | ||
|
||
@singleton() | ||
export class ProviderController { | ||
constructor(private readonly trialProvidersService: TrialProvidersService) {} | ||
constructor( | ||
private readonly trialProvidersService: TrialProvidersService, | ||
private readonly providerCleanupService: ProviderCleanupService | ||
) {} | ||
|
||
async getTrialProviders(): Promise<string[]> { | ||
return await this.trialProvidersService.getTrialProviders(); | ||
} | ||
|
||
async cleanupProviderDeployments(options: ProviderCleanupParams) { | ||
return await this.providerCleanupService.cleanup(options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters