1717import axios , { type AxiosError } from 'axios' ;
1818import chokidar from 'chokidar' ;
1919import EventEmitter from 'events' ;
20+ import * as fsSync from 'fs' ;
2021import fs from 'fs/promises' ;
2122import path from 'path' ;
2223import {
@@ -390,6 +391,10 @@ export class RuntimeManager {
390391 */
391392 private async handleNewDevUi ( filePath : string ) {
392393 try {
394+ if ( ! fsSync . existsSync ( filePath ) ) {
395+ // file already got deleted, ignore...
396+ return ;
397+ }
393398 const { content, toolsInfo } = await retriable (
394399 async ( ) => {
395400 const content = await fs . readFile ( filePath , 'utf-8' ) ;
@@ -433,6 +438,10 @@ export class RuntimeManager {
433438 */
434439 private async handleNewRuntime ( filePath : string ) {
435440 try {
441+ if ( ! fsSync . existsSync ( filePath ) ) {
442+ // file already got deleted, ignore...
443+ return ;
444+ }
436445 const { content, runtimeInfo } = await retriable (
437446 async ( ) => {
438447 const content = await fs . readFile ( filePath , 'utf-8' ) ;
@@ -448,7 +457,12 @@ export class RuntimeManager {
448457 runtimeInfo . name = runtimeInfo . id ;
449458 }
450459 const fileName = path . basename ( filePath ) ;
451- if ( await checkServerHealth ( runtimeInfo . reflectionServerUrl ) ) {
460+ if (
461+ await checkServerHealth (
462+ runtimeInfo . reflectionServerUrl ,
463+ runtimeInfo . id
464+ )
465+ ) {
452466 if (
453467 runtimeInfo . reflectionApiSpecVersion !=
454468 GENKIT_REFLECTION_API_SPEC_VERSION
@@ -529,7 +543,9 @@ export class RuntimeManager {
529543 private async performHealthChecks ( ) {
530544 const healthCheckPromises = Object . entries ( this . filenameToRuntimeMap ) . map (
531545 async ( [ fileName , runtime ] ) => {
532- if ( ! ( await checkServerHealth ( runtime . reflectionServerUrl ) ) ) {
546+ if (
547+ ! ( await checkServerHealth ( runtime . reflectionServerUrl , runtime . id ) )
548+ ) {
533549 await this . removeRuntime ( fileName ) ;
534550 }
535551 }
@@ -541,19 +557,14 @@ export class RuntimeManager {
541557 * Removes the runtime file which will trigger the removal watcher.
542558 */
543559 private async removeRuntime ( fileName : string ) {
544- const runtime = this . filenameToRuntimeMap [ fileName ] ;
545- if ( runtime ) {
546- try {
547- const runtimesDir = await findRuntimesDir ( this . projectRoot ) ;
548- const runtimeFilePath = path . join ( runtimesDir , fileName ) ;
549- await fs . unlink ( runtimeFilePath ) ;
550- } catch ( error ) {
551- logger . debug ( `Failed to delete runtime file: ${ error } ` ) ;
552- }
553- logger . debug (
554- `Removed unhealthy runtime with ID ${ runtime . id } from manager.`
555- ) ;
560+ try {
561+ const runtimesDir = await findRuntimesDir ( this . projectRoot ) ;
562+ const runtimeFilePath = path . join ( runtimesDir , fileName ) ;
563+ await fs . unlink ( runtimeFilePath ) ;
564+ } catch ( error ) {
565+ logger . debug ( `Failed to delete runtime file: ${ error } ` ) ;
556566 }
567+ logger . debug ( `Removed unhealthy runtime ${ fileName } from manager.` ) ;
557568 }
558569}
559570
0 commit comments