@@ -3,8 +3,8 @@ import * as fsp from "node:fs/promises";
33import * as path from "node:path" ;
44import * as util from "node:util" ;
55import {
6+ cleanupContainers ,
67 generateContainerBuildId ,
7- getContainerIdsByImageTags ,
88 resolveDockerHost ,
99} from "@cloudflare/containers-shared/src/utils" ;
1010import { generateStaticRoutingRuleMatcher } from "@cloudflare/workers-shared/asset-worker/src/utils/rules-engine" ;
@@ -29,11 +29,7 @@ import {
2929 kRequestType ,
3030 ROUTER_WORKER_NAME ,
3131} from "./constants" ;
32- import {
33- getDockerPath ,
34- prepareContainerImages ,
35- removeContainersByIds ,
36- } from "./containers" ;
32+ import { getDockerPath , prepareContainerImages } from "./containers" ;
3733import {
3834 addDebugToVitePrintUrls ,
3935 getDebugPathHtml ,
@@ -103,8 +99,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
10399
104100 const additionalModulePaths = new Set < string > ( ) ;
105101 const nodeJsCompatWarningsMap = new Map < WorkerConfig , NodeJsCompatWarnings > ( ) ;
106- let containerImageTagsSeen : Set < string > | undefined ;
107- let runningContainerIds : Array < string > ;
102+ let containerImageTagsSeen = new Set < string > ( ) ;
108103
109104 /** Used to track whether hooks are being called because of a server restart or a server close event. */
110105 let restartingServer = false ;
@@ -493,17 +488,6 @@ if (import.meta.hot) {
493488 )
494489 ) ;
495490
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-
507491 /*
508492 * Upon exiting the dev process we should ensure we perform any
509493 * containers-specific cleanup work. Vite recommends using the
@@ -517,21 +501,11 @@ if (import.meta.hot) {
517501 * `process.exit()` imperatively, and therefore causes `beforeExit`
518502 * not to be emitted).
519503 *
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- *
531504 */
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+ }
535509 } ) ;
536510 }
537511 }
@@ -629,18 +603,10 @@ if (import.meta.hot) {
629603 colors . dim ( colors . yellow ( "\n⚡️ Containers successfully built.\n" ) )
630604 ) ;
631605
632- const dockerPollIntervalId = setInterval ( async ( ) => {
633- if ( containerImageTagsSeen ?. size ) {
634- runningContainerIds = await getContainerIdsByImageTags (
635- dockerPath ,
636- containerImageTagsSeen
637- ) ;
638- }
639- } , 2000 ) ;
640-
641606 process . on ( "exit" , ( ) => {
642- clearInterval ( dockerPollIntervalId ) ;
643- removeContainersByIds ( dockerPath , runningContainerIds ) ;
607+ if ( containerImageTagsSeen . size ) {
608+ cleanupContainers ( dockerPath , containerImageTagsSeen ) ;
609+ }
644610 } ) ;
645611 }
646612
@@ -665,14 +631,7 @@ if (import.meta.hot) {
665631 containerImageTagsSeen ?. size
666632 ) {
667633 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 ) ;
676635 }
677636
678637 debuglog ( "buildEnd:" , restartingServer ? "restarted" : "disposing" ) ;
0 commit comments