-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deployment): implement clean up of managed deployments
refs #395
- Loading branch information
1 parent
1f76b8b
commit 882fac4
Showing
12 changed files
with
182 additions
and
21 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
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
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/deployment/deployment.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 { StaleManagedDeploymentsCleanerService } from "@src/deployment/services/stale-managed-deployments-cleaner/stale-managed-deployments-cleaner.service"; | ||
import { TopUpCustodialDeploymentsService } from "@src/deployment/services/top-up-custodial-deployments/top-up-custodial-deployments.service"; | ||
|
||
@singleton() | ||
export class TopUpDeploymentsController { | ||
constructor(private readonly topUpDeploymentsService: TopUpCustodialDeploymentsService) {} | ||
constructor( | ||
private readonly topUpDeploymentsService: TopUpCustodialDeploymentsService, | ||
private readonly staleDeploymentsCleanerService: StaleManagedDeploymentsCleanerService | ||
) {} | ||
|
||
async topUpDeployments() { | ||
await this.topUpDeploymentsService.topUpDeployments(); | ||
} | ||
|
||
async cleanUpStaleDeployment() { | ||
await this.staleDeploymentsCleanerService.cleanup(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
apps/api/src/deployment/repositories/deployment/deployment.repository.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,46 @@ | ||
import { Deployment, Lease } from "@akashnetwork/database/dbSchemas/akash"; | ||
import { literal, Op } from "sequelize"; | ||
import { singleton } from "tsyringe"; | ||
|
||
export interface StaleDeploymentsOptions { | ||
createdHeight: number; | ||
owner: string; | ||
} | ||
|
||
export interface StaleDeploymentsOutput { | ||
dseq: number; | ||
} | ||
|
||
@singleton() | ||
export class DeploymentRepository { | ||
async findStaleDeployments(options: StaleDeploymentsOptions): Promise<StaleDeploymentsOutput[]> { | ||
const deployments = await Deployment.findAll({ | ||
attributes: ["dseq"], | ||
include: [ | ||
{ | ||
model: Lease, | ||
attributes: [], | ||
required: false | ||
} | ||
], | ||
where: { | ||
owner: options.owner, | ||
[Op.and]: [ | ||
{ | ||
createdHeight: { | ||
[Op.lt]: options.createdHeight | ||
} | ||
}, | ||
{ | ||
closedHeight: null | ||
} | ||
] | ||
}, | ||
group: ["deployment.dseq"], | ||
having: literal(`COUNT("leases"."deploymentId") = 0`), | ||
raw: true | ||
}); | ||
|
||
return deployments ? (deployments as unknown as StaleDeploymentsOutput[]) : []; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...t/services/stale-managed-deployments-cleaner/stale-managed-deployments-cleaner.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,57 @@ | ||
import { LoggerService } from "@akashnetwork/logging"; | ||
import { secondsInMinute } from "date-fns/constants"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { UserWalletOutput, UserWalletRepository } from "@src/billing/repositories"; | ||
import { RpcMessageService } from "@src/billing/services"; | ||
import { TxSignerService } from "@src/billing/services/tx-signer/tx-signer.service"; | ||
import { ErrorService } from "@src/core/services/error/error.service"; | ||
import { DeploymentRepository } from "@src/deployment/repositories/deployment/deployment.repository"; | ||
import { BlockHttpService } from "@src/deployment/services/block-http/block-http.service"; | ||
import { averageBlockTime } from "@src/utils/constants"; | ||
|
||
@singleton() | ||
export class StaleManagedDeploymentsCleanerService { | ||
private readonly logger = new LoggerService({ context: StaleManagedDeploymentsCleanerService.name }); | ||
|
||
private readonly MAX_LIVE_BLOCKS = Math.floor((10 * secondsInMinute) / averageBlockTime); | ||
|
||
constructor( | ||
private readonly userWalletRepository: UserWalletRepository, | ||
private readonly deploymentRepository: DeploymentRepository, | ||
private readonly blockHttpService: BlockHttpService, | ||
private readonly rpcMessageService: RpcMessageService, | ||
private readonly txSignerService: TxSignerService, | ||
private readonly errorService: ErrorService | ||
) {} | ||
|
||
async cleanup() { | ||
await this.userWalletRepository.paginate({ limit: 10 }, async wallets => { | ||
const cleanUpAllWallets = wallets.map(async wallet => { | ||
await this.errorService.execWithErrorHandler({ wallet, event: "DEPLOYMENT_CLEAN_UP_ERROR" }, () => this.cleanUpForWallet(wallet)); | ||
}); | ||
|
||
await Promise.all(cleanUpAllWallets); | ||
}); | ||
} | ||
|
||
private async cleanUpForWallet(wallet: UserWalletOutput) { | ||
const currentHeight = await this.blockHttpService.getCurrentHeight(); | ||
const client = await this.txSignerService.getClientForAddressIndex(wallet.id); | ||
const deployments = await this.deploymentRepository.findStaleDeployments({ | ||
owner: wallet.address, | ||
createdHeight: currentHeight - this.MAX_LIVE_BLOCKS | ||
}); | ||
|
||
const closeAllWalletStaleDeployments = deployments.map(async deployment => { | ||
const message = this.rpcMessageService.getCloseDeploymentMsg(wallet.address, deployment.dseq); | ||
this.logger.info({ event: "DEPLOYMENT_CLEAN_UP", params: { owner: wallet.address, dseq: deployment.dseq } }); | ||
|
||
await client.signAndBroadcast([message]); | ||
|
||
this.logger.info({ event: "DEPLOYMENT_CLEAN_UP_SUCCESS" }); | ||
}); | ||
|
||
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
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