import type { Container } from "@codemation/core"; import { Engine, InlineDrivingScheduler } from "@codemation/core/bootstrap"; import { RunEventBusTelemetryReporter } from "../application/telemetry/RunEventBusTelemetryReporter"; import { WorkflowRunRetentionPruneScheduler } from "../application/runs/WorkflowRunRetentionPruneScheduler"; import { WorkflowAuditLogPruneScheduler } from "../application/WorkflowAuditLogPruneScheduler"; import type { PrismaDatabaseClient } from "../infrastructure/persistence/PrismaDatabaseClient"; import { McpConnectionPool } from "../mcp/McpConnectionPool"; import { WorkflowAuditLogWriter } from "../audit/WorkflowAuditLogWriter"; import { ControlPlaneCatalogFetcher } from "../credentials/ControlPlaneCatalogFetcher"; import { SignedRunEventRelayPublisherToken } from "../pairing/SignedRunEventRelayPublisherToken"; export class AppContainerLifecycle { constructor( private readonly container: Container, private readonly ownedPrismaClient: PrismaDatabaseClient | null, ) {} async start(): Promise { if (this.container.isRegistered(ControlPlaneCatalogFetcher, true)) { await this.container.resolve(ControlPlaneCatalogFetcher).start(); } if (this.container.isRegistered(SignedRunEventRelayPublisherToken, true)) { await this.container.resolve(SignedRunEventRelayPublisherToken).start(); } } async startWorkerSubscribers(): Promise { if (this.container.isRegistered(WorkflowAuditLogWriter, true)) { await this.container.resolve(WorkflowAuditLogWriter).start(); } if (this.container.isRegistered(WorkflowAuditLogPruneScheduler, true)) { this.container.resolve(WorkflowAuditLogPruneScheduler).start(); } } async stop(): Promise { if (this.container.isRegistered(Engine, true)) { await this.container.resolve(Engine).stop(); } if (this.container.isRegistered(InlineDrivingScheduler, true)) { await this.container.resolve(InlineDrivingScheduler).stop(); } if (this.container.isRegistered(RunEventBusTelemetryReporter, true)) { await this.container.resolve(RunEventBusTelemetryReporter).stop(); } if (this.container.isRegistered(WorkflowAuditLogWriter, true)) { await this.container.resolve(WorkflowAuditLogWriter).stop(); } if (this.container.isRegistered(WorkflowRunRetentionPruneScheduler, true)) { await this.container.resolve(WorkflowRunRetentionPruneScheduler).stop(); } if (this.container.isRegistered(WorkflowAuditLogPruneScheduler, true)) { await this.container.resolve(WorkflowAuditLogPruneScheduler).stop(); } if (this.container.isRegistered(McpConnectionPool, true)) { await this.container.resolve(McpConnectionPool).closeAll(); } if (this.container.isRegistered(ControlPlaneCatalogFetcher, true)) { await this.container.resolve(ControlPlaneCatalogFetcher).stop(); } if (this.container.isRegistered(SignedRunEventRelayPublisherToken, true)) { await this.container.resolve(SignedRunEventRelayPublisherToken).stop(); } if (this.ownedPrismaClient) { await this.ownedPrismaClient.$disconnect(); } } }