@@ -12,6 +12,7 @@ import os from 'os';
12
12
13
13
const isWindows = os . platform ( ) === 'win32' ;
14
14
import { URL_PROTOCOL_PREFIX } from '@/utils/const' ;
15
+ import { logger } from '../../log/logger' ;
15
16
16
17
// Persist container state to file system to recover after service restarts
17
18
const CONTAINER_STATE_FILE = path . join ( process . cwd ( ) , 'container-state.json' ) ;
@@ -97,11 +98,11 @@ async function initializeState() {
97
98
// Check if base image exists
98
99
baseImageBuilt = await checkBaseImageExists ( ) ;
99
100
100
- console . log (
101
+ logger . info (
101
102
'State initialization complete, cleaned up non-running containers and expired port allocations'
102
103
) ;
103
104
} catch ( error ) {
104
- console . error ( 'Error initializing state:' , error ) ;
105
+ logger . error ( 'Error initializing state:' , error ) ;
105
106
// If loading fails, continue with empty state
106
107
runningContainers = new Map ( ) ;
107
108
allocatedPorts = new Set ( ) ;
@@ -134,7 +135,7 @@ async function saveState() {
134
135
JSON . stringify ( portsArray , null , 2 )
135
136
) ;
136
137
} catch ( error ) {
137
- console . error ( 'Error saving state:' , error ) ;
138
+ logger . error ( 'Error saving state:' , error ) ;
138
139
} finally {
139
140
isUpdatingState = false ;
140
141
}
@@ -257,12 +258,12 @@ async function ensureBaseImageExists(): Promise<void> {
257
258
258
259
// Check if base Dockerfile exists
259
260
if ( ! fs . existsSync ( path . join ( dockerfilePath , 'Dockerfile' ) ) ) {
260
- console . error ( 'Base Dockerfile not found at:' , dockerfilePath ) ;
261
+ logger . error ( 'Base Dockerfile not found at:' , dockerfilePath ) ;
261
262
throw new Error ( 'Base Dockerfile not found' ) ;
262
263
}
263
264
264
265
// Build the base image
265
- console . log (
266
+ logger . info (
266
267
`Building base image ${ BASE_IMAGE_NAME } from ${ dockerfilePath } ...`
267
268
) ;
268
269
await execWithTimeout (
@@ -271,9 +272,9 @@ async function ensureBaseImageExists(): Promise<void> {
271
272
) ;
272
273
273
274
baseImageBuilt = true ;
274
- console . log ( `Base image ${ BASE_IMAGE_NAME } built successfully` ) ;
275
+ logger . info ( `Base image ${ BASE_IMAGE_NAME } built successfully` ) ;
275
276
} catch ( error ) {
276
- console . error ( 'Error building base image:' , error ) ;
277
+ logger . error ( 'Error building base image:' , error ) ;
277
278
throw new Error ( 'Failed to build base image' ) ;
278
279
}
279
280
}
@@ -293,13 +294,13 @@ function execWithTimeout(
293
294
294
295
const executeWithRetry = ( ) : Promise < string > => {
295
296
return new Promise ( ( resolve , reject ) => {
296
- console . log ( `Executing command: ${ command } ` ) ;
297
+ logger . info ( `Executing command: ${ command } ` ) ;
297
298
exec ( command , { timeout : options . timeout } , ( error , stdout , stderr ) => {
298
299
if ( error ) {
299
- console . error ( `Command execution error: ${ stderr } ` ) ;
300
+ logger . error ( `Command execution error: ${ stderr } ` ) ;
300
301
if ( retryCount < maxRetries ) {
301
302
retryCount ++ ;
302
- console . log ( `Retry ${ retryCount } /${ maxRetries } ` ) ;
303
+ logger . info ( `Retry ${ retryCount } /${ maxRetries } ` ) ;
303
304
setTimeout ( ( ) => {
304
305
executeWithRetry ( ) . then ( resolve ) . catch ( reject ) ;
305
306
} , 2000 ) ; // Wait 2 seconds before retry
@@ -352,9 +353,9 @@ async function runDockerContainer(
352
353
await execWithTimeout ( `docker rm -f ${ existingContainerId } ` , {
353
354
timeout : 30000 ,
354
355
} ) ;
355
- console . log ( `Removed non-running container: ${ existingContainerId } ` ) ;
356
+ logger . info ( `Removed non-running container: ${ existingContainerId } ` ) ;
356
357
} catch ( error ) {
357
- console . error ( `Error removing non-running container:` , error ) ;
358
+ logger . error ( `Error removing non-running container:` , error ) ;
358
359
// Continue processing even if removal fails
359
360
}
360
361
}
@@ -376,7 +377,7 @@ async function runDockerContainer(
376
377
await execWithTimeout ( `docker inspect ${ containerName } ` , {
377
378
timeout : 10000 ,
378
379
} ) ;
379
- console . log (
380
+ logger . info (
380
381
`Found container with same name ${ containerName } , removing it first`
381
382
) ;
382
383
await execWithTimeout ( `docker rm -f ${ containerName } ` , {
@@ -423,7 +424,7 @@ async function runDockerContainer(
423
424
}
424
425
425
426
// Run container
426
- console . log ( `Executing run command: ${ runCommand } ` ) ;
427
+ logger . info ( `Executing run command: ${ runCommand } ` ) ;
427
428
const containerActualId = await execWithTimeout (
428
429
runCommand ,
429
430
{ timeout : 60000 , retries : 2 } // 1 minute timeout, 2 retries
@@ -448,12 +449,12 @@ async function runDockerContainer(
448
449
} ) ;
449
450
await saveState ( ) ;
450
451
451
- console . log (
452
+ logger . info (
452
453
`Container ${ containerName } is now running at ${ URL_PROTOCOL_PREFIX } ://${ domain } (port: ${ exposedPort } )`
453
454
) ;
454
455
return { domain, containerId : containerActualId , port : exposedPort } ;
455
456
} catch ( error : any ) {
456
- console . error ( `Error running container:` , error ) ;
457
+ logger . error ( `Error running container:` , error ) ;
457
458
458
459
// Clean up allocated port
459
460
allocatedPorts . delete ( exposedPort ) ;
@@ -465,7 +466,7 @@ async function runDockerContainer(
465
466
466
467
// Initialize state when service starts
467
468
initializeState ( ) . catch ( ( error ) => {
468
- console . error ( 'Error initializing state:' , error ) ;
469
+ logger . error ( 'Error initializing state:' , error ) ;
469
470
} ) ;
470
471
471
472
// Periodically check container status (hourly)
@@ -479,7 +480,7 @@ setInterval(
479
480
480
481
const isRunning = await checkContainerRunning ( container . containerId ) ;
481
482
if ( ! isRunning ) {
482
- console . log (
483
+ logger . info (
483
484
`Container ${ container . containerId } is no longer running, removing from state`
484
485
) ;
485
486
runningContainers . delete ( projectPath ) ;
@@ -529,7 +530,7 @@ export async function GET(req: Request) {
529
530
}
530
531
await saveState ( ) ;
531
532
532
- console . log (
533
+ logger . info (
533
534
`Container ${ existingContainer . containerId } is no longer running, will create a new one`
534
535
) ;
535
536
}
@@ -554,7 +555,7 @@ export async function GET(req: Request) {
554
555
containerId,
555
556
} ) ;
556
557
} catch ( error : any ) {
557
- console . error ( `Failed to start Docker container:` , error ) ;
558
+ logger . error ( `Failed to start Docker container:` , error ) ;
558
559
return NextResponse . json (
559
560
{ error : error . message || 'Failed to start Docker container' } ,
560
561
{ status : 500 }
0 commit comments