@@ -3,8 +3,8 @@ import * as fsp from "node:fs/promises";
3
3
import * as path from "node:path" ;
4
4
import * as util from "node:util" ;
5
5
import {
6
+ cleanupContainers ,
6
7
generateContainerBuildId ,
7
- getContainerIdsByImageTags ,
8
8
resolveDockerHost ,
9
9
} from "@cloudflare/containers-shared/src/utils" ;
10
10
import { generateStaticRoutingRuleMatcher } from "@cloudflare/workers-shared/asset-worker/src/utils/rules-engine" ;
@@ -29,11 +29,7 @@ import {
29
29
kRequestType ,
30
30
ROUTER_WORKER_NAME ,
31
31
} from "./constants" ;
32
- import {
33
- getDockerPath ,
34
- prepareContainerImages ,
35
- removeContainersByIds ,
36
- } from "./containers" ;
32
+ import { getDockerPath , prepareContainerImages } from "./containers" ;
37
33
import {
38
34
addDebugToVitePrintUrls ,
39
35
getDebugPathHtml ,
@@ -103,8 +99,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
103
99
104
100
const additionalModulePaths = new Set < string > ( ) ;
105
101
const nodeJsCompatWarningsMap = new Map < WorkerConfig , NodeJsCompatWarnings > ( ) ;
106
- let containerImageTagsSeen : Set < string > | undefined ;
107
- let runningContainerIds : Array < string > ;
102
+ let containerImageTagsSeen = new Set < string > ( ) ;
108
103
109
104
/** Used to track whether hooks are being called because of a server restart or a server close event. */
110
105
let restartingServer = false ;
@@ -493,17 +488,6 @@ if (import.meta.hot) {
493
488
)
494
489
) ;
495
490
496
- // poll Docker every two seconds and update the list of ids of all
497
- // running containers
498
- const dockerPollIntervalId = setInterval ( async ( ) => {
499
- if ( containerImageTagsSeen ?. size ) {
500
- runningContainerIds = await getContainerIdsByImageTags (
501
- dockerPath ,
502
- containerImageTagsSeen
503
- ) ;
504
- }
505
- } , 2000 ) ;
506
-
507
491
/*
508
492
* Upon exiting the dev process we should ensure we perform any
509
493
* containers-specific cleanup work. Vite recommends using the
@@ -517,21 +501,11 @@ if (import.meta.hot) {
517
501
* `process.exit()` imperatively, and therefore causes `beforeExit`
518
502
* not to be emitted).
519
503
*
520
- * Furthermore, since the `exit` event handler cannot perform async
521
- * ops as per spec (https://nodejs.org/api/process.html#event-exit),
522
- * we also need a mechanism to ensure that list of containers to be
523
- * cleaned up on exit is up to date. This is what the interval with id
524
- * `dockerPollIntervalId` is for.
525
- *
526
- * It is possible, though very unlikely, that in some rare cases,
527
- * we might be left with some orphaned containers, due to the fact
528
- * that at the point of exiting the dev process, our internal list
529
- * of container ids is out of date. We accept this caveat for now.
530
- *
531
504
*/
532
- process . on ( "exit" , ( ) => {
533
- clearInterval ( dockerPollIntervalId ) ;
534
- removeContainersByIds ( dockerPath , runningContainerIds ) ;
505
+ process . on ( "exit" , async ( ) => {
506
+ if ( containerImageTagsSeen . size ) {
507
+ cleanupContainers ( dockerPath , containerImageTagsSeen ) ;
508
+ }
535
509
} ) ;
536
510
}
537
511
}
@@ -629,18 +603,10 @@ if (import.meta.hot) {
629
603
colors . dim ( colors . yellow ( "\n⚡️ Containers successfully built.\n" ) )
630
604
) ;
631
605
632
- const dockerPollIntervalId = setInterval ( async ( ) => {
633
- if ( containerImageTagsSeen ?. size ) {
634
- runningContainerIds = await getContainerIdsByImageTags (
635
- dockerPath ,
636
- containerImageTagsSeen
637
- ) ;
638
- }
639
- } , 2000 ) ;
640
-
641
606
process . on ( "exit" , ( ) => {
642
- clearInterval ( dockerPollIntervalId ) ;
643
- removeContainersByIds ( dockerPath , runningContainerIds ) ;
607
+ if ( containerImageTagsSeen . size ) {
608
+ cleanupContainers ( dockerPath , containerImageTagsSeen ) ;
609
+ }
644
610
} ) ;
645
611
}
646
612
@@ -665,14 +631,7 @@ if (import.meta.hot) {
665
631
containerImageTagsSeen ?. size
666
632
) {
667
633
const dockerPath = getDockerPath ( ) ;
668
- runningContainerIds = await getContainerIdsByImageTags (
669
- dockerPath ,
670
- containerImageTagsSeen
671
- ) ;
672
-
673
- await removeContainersByIds ( dockerPath , runningContainerIds ) ;
674
- containerImageTagsSeen . clear ( ) ;
675
- runningContainerIds = [ ] ;
634
+ cleanupContainers ( dockerPath , containerImageTagsSeen ) ;
676
635
}
677
636
678
637
debuglog ( "buildEnd:" , restartingServer ? "restarted" : "disposing" ) ;
0 commit comments