Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deployment): add concurrency for top up deployment #519

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ 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 { ProviderCleanupParams } from "@src/billing/types/provider-cleanup";
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);
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/billing/types/provider-cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ConcurrencyOptions, DryRunOptions } from "@src/core/types/console";

export interface ProviderCleanupParams extends DryRunOptions, ConcurrencyOptions {
provider: string;
}
3 changes: 2 additions & 1 deletion apps/api/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ program
program
.command("top-up-deployments")
.description("Refill deployments with auto top up enabled")
.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 top up deployments", false)
.action(async (options, command) => {
await executeCliHandler(command.name(), async () => {
await container.resolve(TopUpDeploymentsController).topUpDeployments({ dryRun: options.dryRun });
await container.resolve(TopUpDeploymentsController).topUpDeployments(options);
});
});

Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/core/types/console.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export interface DryRunOptions {
dryRun: boolean;
}

export interface ConcurrencyOptions {
concurrency?: number;
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { singleton } from "tsyringe";

import {
CleanUpStaleDeploymentsParams,
StaleManagedDeploymentsCleanerService
} from "@src/deployment/services/stale-managed-deployments-cleaner/stale-managed-deployments-cleaner.service";
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";
import { TopUpManagedDeploymentsService } from "@src/deployment/services/top-up-managed-deployments/top-up-managed-deployments.service";
import { TopUpDeploymentsOptions } from "@src/deployment/types/deployments-refiller";
import { CleanUpStaleDeploymentsParams } from "@src/deployment/types/state-deployments";

@singleton()
export class TopUpDeploymentsController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { singleton } from "tsyringe";

import { ProviderCleanupParams, ProviderCleanupService } from "@src/billing/services/provider-cleanup/provider-cleanup.service";
import { ProviderCleanupService } from "@src/billing/services/provider-cleanup/provider-cleanup.service";
import { ProviderCleanupParams } from "@src/billing/types/provider-cleanup";
import { TrialProvidersService } from "@src/deployment/services/trial-providers/trial-providers.service";

@singleton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import { TxSignerService } from "@src/billing/services/tx-signer/tx-signer.servi
import { BlockRepository } from "@src/chain/repositories/block.repository";
import { ErrorService } from "@src/core/services/error/error.service";
import { DeploymentRepository } from "@src/deployment/repositories/deployment/deployment.repository";
import { CleanUpStaleDeploymentsParams } from "@src/deployment/types/state-deployments";
import { averageBlockTime } from "@src/utils/constants";

export interface CleanUpStaleDeploymentsParams {
concurrency: number;
}

@singleton()
export class StaleManagedDeploymentsCleanerService {
private readonly logger = LoggerService.forContext(StaleManagedDeploymentsCleanerService.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { DeploymentsRefiller, TopUpDeploymentsOptions } from "@src/deployment/ty

@singleton()
export class TopUpManagedDeploymentsService implements DeploymentsRefiller {
private readonly CONCURRENCY = 10;

private readonly logger = LoggerService.forContext(TopUpManagedDeploymentsService.name);

constructor(
Expand All @@ -35,7 +33,7 @@ export class TopUpManagedDeploymentsService implements DeploymentsRefiller {
const summary = new TopUpSummarizer();
summary.set("startBlockHeight", await this.blockHttpService.getCurrentHeight());

await this.userWalletRepository.paginate({ limit: this.CONCURRENCY }, async wallets => {
await this.userWalletRepository.paginate({ limit: options.concurrency || 10 }, async wallets => {
await Promise.all(
wallets.map(async wallet => {
await this.errorService.execWithErrorHandler(
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/deployment/types/deployments-refiller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DryRunOptions } from "@src/core/types/console";
import { ConcurrencyOptions, DryRunOptions } from "@src/core/types/console";

export interface TopUpDeploymentsOptions extends DryRunOptions {}
export interface TopUpDeploymentsOptions extends DryRunOptions, ConcurrencyOptions {}

export interface DeploymentsRefiller {
topUpDeployments(options: TopUpDeploymentsOptions): Promise<void>;
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/deployment/types/state-deployments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ConcurrencyOptions } from "@src/core/types/console";

export interface CleanUpStaleDeploymentsParams extends ConcurrencyOptions {}
Loading