@@ -6,17 +6,17 @@ import {
66 type TimeStep ,
77 type TimeRange ,
88} from "./utils/container-health.ts" ;
9- import { restartContainer , getRestartCount } from "./utils/auto-restart.ts" ;
9+ import { restartContainer , stopContainer , getRestartCount , getStopCount } from "./utils/auto-restart.ts" ;
1010import { getMonitorStatus , triggerHealthCheck } from "./health-monitor.ts" ;
1111import { checkJWT } from "./utils/jwt.ts" ;
1212
1313const TIME_RANGE_PRESETS : Record < TimeStep , TimeRange > = {
14- '1s' : { step : '1s' , duration : '5m' } ,
15- '15s' : { step : '15s' , duration : '15m' } ,
16- '1m' : { step : '1m' , duration : '1h' } ,
17- '5m' : { step : '5m' , duration : '6h' } ,
18- '1h' : { step : '1h' , duration : '24h' } ,
19- '1d' : { step : '1d' , duration : '7d' } ,
14+ '1s' : { step : '1s' , duration : '5m' } ,
15+ '15s' : { step : '15s' , duration : '15m' } ,
16+ '1m' : { step : '1m' , duration : '1h' } ,
17+ '5m' : { step : '5m' , duration : '6h' } ,
18+ '1h' : { step : '1h' , duration : '24h' } ,
19+ '1d' : { step : '1d' , duration : '7d' } ,
2020} ;
2121
2222
@@ -44,6 +44,7 @@ export async function getContainerHealth(ctx: Context): Promise<void> {
4444 memoryPercent : Math . round ( c . memoryPercent * 100 ) / 100 ,
4545 memoryUsageMB : Math . round ( c . memoryUsage / ( 1024 * 1024 ) ) ,
4646 restartCount : getRestartCount ( c . name ) ,
47+ stopCount : getStopCount ( c . name ) ,
4748 isHealthy : ! isUnhealthy ( c ) ,
4849 lastUpdated : c . lastUpdated . toISOString ( ) ,
4950 } ) ) ,
@@ -160,6 +161,42 @@ export async function restartContainerHandler(ctx: Context): Promise<void> {
160161 }
161162}
162163
164+ export async function stopContainerHandler ( ctx : Context ) : Promise < void > {
165+ const subdomain = ctx . params . subdomain ;
166+
167+ const body = await ctx . request . body ( ) . value ;
168+ let document ;
169+ try {
170+ document = typeof body === 'string' ? JSON . parse ( body ) : body ;
171+ } catch {
172+ document = body ;
173+ }
174+
175+ const author = document ?. author ;
176+ const token = document ?. token ;
177+ const provider = document ?. provider ;
178+
179+ if ( author !== await checkJWT ( provider , token ) ) {
180+ ctx . throw ( 401 ) ;
181+ }
182+
183+ try {
184+ await stopContainer ( subdomain ) ;
185+
186+ ctx . response . headers . set ( "Access-Control-Allow-Origin" , "*" ) ;
187+ ctx . response . body = {
188+ status : "success" ,
189+ message : `Container ${ subdomain } stop initiated` ,
190+ } ;
191+ } catch ( error ) {
192+ ctx . response . status = 500 ;
193+ ctx . response . body = {
194+ status : "error" ,
195+ message : `Failed to stop ${ subdomain } : ${ error } ` ,
196+ } ;
197+ }
198+ }
199+
163200
164201export async function triggerHealthCheckHandler ( ctx : Context ) : Promise < void > {
165202 const body = await ctx . request . body ( ) . value ;
0 commit comments