@@ -18,7 +18,14 @@ import {
1818 CheckpointClient ,
1919 isKubernetesEnvironment ,
2020} from "@trigger.dev/core/v3/serverOnly" ;
21- import { createK8sApi , RUNTIME_ENV } from "./clients/kubernetes.js" ;
21+ import { createK8sApi } from "./clients/kubernetes.js" ;
22+ import { collectDefaultMetrics } from "prom-client" ;
23+ import { register } from "./metrics.js" ;
24+ import { PodCleaner } from "./services/podCleaner.js" ;
25+
26+ if ( env . METRICS_COLLECT_DEFAULTS ) {
27+ collectDefaultMetrics ( { register } ) ;
28+ }
2229
2330class ManagedSupervisor {
2431 private readonly workerSession : SupervisorSession ;
@@ -28,6 +35,7 @@ class ManagedSupervisor {
2835 private readonly logger = new SimpleStructuredLogger ( "managed-worker" ) ;
2936 private readonly resourceMonitor : ResourceMonitor ;
3037 private readonly checkpointClient ?: CheckpointClient ;
38+ private readonly podCleaner ?: PodCleaner ;
3139
3240 private readonly isKubernetes = isKubernetesEnvironment ( env . KUBERNETES_FORCE_ENABLED ) ;
3341 private readonly warmStartUrl = env . TRIGGER_WARM_START_URL ;
@@ -37,6 +45,14 @@ class ManagedSupervisor {
3745 const workloadApiDomain = env . TRIGGER_WORKLOAD_API_DOMAIN ;
3846 const workloadApiPortExternal = env . TRIGGER_WORKLOAD_API_PORT_EXTERNAL ;
3947
48+ if ( env . POD_CLEANER_ENABLED ) {
49+ this . podCleaner = new PodCleaner ( {
50+ namespace : env . KUBERNETES_NAMESPACE ,
51+ batchSize : env . POD_CLEANER_BATCH_SIZE ,
52+ intervalMs : env . POD_CLEANER_INTERVAL_MS ,
53+ } ) ;
54+ }
55+
4056 if ( this . warmStartUrl ) {
4157 this . logger . log ( "[ManagedWorker] 🔥 Warm starts enabled" , {
4258 warmStartUrl : this . warmStartUrl ,
@@ -273,6 +289,10 @@ class ManagedSupervisor {
273289 async start ( ) {
274290 this . logger . log ( "[ManagedWorker] Starting up" ) ;
275291
292+ if ( this . podCleaner ) {
293+ await this . podCleaner . start ( ) ;
294+ }
295+
276296 if ( env . TRIGGER_WORKLOAD_API_ENABLED ) {
277297 this . logger . log ( "[ManagedWorker] Workload API enabled" , {
278298 protocol : env . TRIGGER_WORKLOAD_API_PROTOCOL ,
@@ -292,6 +312,10 @@ class ManagedSupervisor {
292312 async stop ( ) {
293313 this . logger . log ( "[ManagedWorker] Shutting down" ) ;
294314 await this . httpServer . stop ( ) ;
315+
316+ if ( this . podCleaner ) {
317+ await this . podCleaner . stop ( ) ;
318+ }
295319 }
296320}
297321
0 commit comments