-
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(observability): utilise new logger in stats-web
ref #436
- Loading branch information
Showing
100 changed files
with
1,735 additions
and
492 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -21,7 +21,8 @@ | |
"repo", | ||
"styling", | ||
"observability", | ||
"analytics" | ||
"analytics", | ||
"template" | ||
] | ||
] | ||
} | ||
|
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
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
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
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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
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 { ProviderCleanupSummarizer } from "@src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer"; | ||
import { DeploymentRepository } from "@src/deployment/repositories/deployment/deployment.repository"; | ||
import { TxSignerService } from "../tx-signer/tx-signer.service"; | ||
|
||
export interface ProviderCleanupParams { | ||
concurrency: number; | ||
provider: string; | ||
dryRun: boolean; | ||
} | ||
|
||
@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) { | ||
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( | ||
{ | ||
wallet, | ||
event: "PROVIDER_CLEAN_UP_ERROR", | ||
context: ProviderCleanupService.name | ||
}, | ||
() => 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, summary: ProviderCleanupSummarizer) { | ||
const client = await this.txSignerService.getClientForAddressIndex(wallet.id); | ||
const deployments = await this.deploymentRepository.findDeploymentsForProvider({ | ||
owner: wallet.address, | ||
provider: options.provider | ||
}); | ||
|
||
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 { | ||
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"); | ||
} | ||
}); | ||
|
||
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 { ProviderCleanupParams, ProviderCleanupService } from "@src/billing/services/provider-cleanup/provider-cleanup.service"; | ||
import { TrialProvidersService } from "@src/deployment/services/trial-providers/trial-providers.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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
apps/api/src/deployment/lib/provider-cleanup-summarizer/provider-cleanup-summarizer.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,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 | ||
}; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.