1- import fs from 'fs' ;
2- import path from 'path' ;
3- import { spawn } from 'child_process' ;
1+ import { spawn , execSync } from 'child_process' ;
42
53import { app , dialog } from 'electron' ;
64
@@ -15,21 +13,15 @@ export function createCEAProcess(url, BrowserWindow, callback) {
1513 console . log ( `createCEAProcess(${ url } )` ) ;
1614 // For windows
1715 if ( process . platform === 'win32' ) {
18- let scriptPath = path . join (
19- path . dirname ( process . execPath ) ,
20- '/../' ,
21- 'dashboard.bat' ,
22- ) ;
23- // Fallback to default install path
24- if ( ! fs . existsSync ( scriptPath ) )
25- scriptPath = path . join (
26- process . env . USERPROFILE ,
27- 'Documents' ,
28- 'CityEnergyAnalyst' ,
29- 'dashboard.bat' ,
30- ) ;
31- console . log ( scriptPath ) ;
32- cea = spawn ( 'cmd.exe' , [ '/c' , scriptPath ] ) ;
16+ cea = spawn ( getMicromambaPath ( ) , [
17+ '-r' ,
18+ getCEARootPath ( ) ,
19+ '-n' ,
20+ 'cea' ,
21+ 'run' ,
22+ 'cea' ,
23+ 'dashboard' ,
24+ ] ) ;
3325 } else if ( process . platform === 'darwin' ) {
3426 cea = spawn ( getMicromambaPath ( ) , [
3527 '-r' ,
@@ -43,6 +35,8 @@ export function createCEAProcess(url, BrowserWindow, callback) {
4335 }
4436
4537 if ( cea ) {
38+ console . debug ( 'CEA process started with PID:' , cea . pid ) ;
39+
4640 // Attach cea output to console
4741 cea . stdout . on ( 'data' , function ( data ) {
4842 console . log ( data . toString ( 'utf8' ) . trim ( ) ) ;
@@ -54,7 +48,10 @@ export function createCEAProcess(url, BrowserWindow, callback) {
5448
5549 // Show Error message box when CEA encounters any error on startup
5650 cea . stderr . on ( 'data' , saveStartupError ) ;
57- cea . on ( 'exit' , showStartupError ) ;
51+ cea . on ( 'exit' , ( ) => {
52+ cea = null ;
53+ showStartupError ( ) ;
54+ } ) ;
5855 }
5956
6057 function saveStartupError ( message ) {
@@ -85,10 +82,41 @@ export function createCEAProcess(url, BrowserWindow, callback) {
8582}
8683
8784// Kill process and stop all timed events
88- export function killCEAProcess ( ) {
85+ export function killCEAProcess ( killTimeout = 10000 ) {
8986 if ( cea ) {
9087 cea . removeAllListeners ( 'exit' ) ;
91- process . kill ( cea . pid ) ;
88+ let result = false ;
89+
90+ if ( process . platform === 'win32' ) {
91+ // FIXME: Force kill on windows for now
92+ console . debug ( 'Forcing process kill using taskkill on Windows' ) ;
93+ try {
94+ execSync ( `taskkill /PID ${ cea . pid } /T /F` ) ;
95+ result = true ;
96+ } catch ( error ) {
97+ console . error ( `Error killing process: ${ error ?. message } ` ) ;
98+ }
99+ } else {
100+ try {
101+ setTimeout ( ( ) => process . kill ( cea . pid , 'SIGKILL' ) , killTimeout ) ;
102+
103+ result = process . kill ( cea . pid , 'SIGTERM' ) ;
104+ } catch ( error ) {
105+ if ( error . code === 'ESRCH' ) {
106+ console . error ( 'Unable to kill CEA process. (Process not found)' ) ;
107+ } else {
108+ console . error ( error ) ;
109+ }
110+ }
111+ }
112+
113+ if ( result ) {
114+ console . debug ( 'CEA process killed with PID:' , cea . pid ) ;
115+ } else {
116+ console . error ( 'Failed to kill CEA process with PID:' , cea . pid ) ;
117+ }
118+ } else {
119+ console . debug ( 'CEA process not found. Ignoring...' ) ;
92120 }
93121 interval && clearInterval ( interval ) ;
94122 timeout && clearTimeout ( timeout ) ;
0 commit comments