44 * Handles starting, stopping, and restarting the backend process with proper error handling and logging.
55 */
66
7+ import AnsiToHtml from "ansi-to-html" ;
78import { spawn } from "child_process" ;
89import { app , dialog } from "electron" ;
910import fs from "fs" ;
@@ -15,6 +16,9 @@ import {
1516 getUserConfigPath ,
1617} from "../utils/paths.js" ;
1718
19+ // Create ANSI to HTML converter
20+ const convert = new AnsiToHtml ( ) ;
21+
1822// Get the application root path
1923const appPath = getAppPath ( ) ;
2024
@@ -30,7 +34,7 @@ let lastBackendError = null;
3034 * @example
3135 * startBackend();
3236 */
33- function startBackend ( ) {
37+ function startBackend ( logWindow = null ) {
3438 return new Promise ( ( resolve , reject ) => {
3539 // Get paths for binary and config
3640 const backendBin = getBinaryPath ( "backend" ) ;
@@ -63,6 +67,12 @@ function startBackend() {
6367 // Log stdout output from backend
6468 backendProcess . stdout . on ( "data" , ( data ) => {
6569 logger . backend . info ( `${ data . toString ( ) . trim ( ) } ` ) ;
70+
71+ // Send log message to log window
72+ if ( logWindow ) {
73+ const htmlData = convert . toHtml ( data . toString ( ) . trim ( ) ) ;
74+ logWindow . webContents . send ( "log" , htmlData ) ;
75+ }
6676 } ) ;
6777
6878 // Capture stderr output (where Go errors/panics are written)
@@ -71,6 +81,12 @@ function startBackend() {
7181 logger . backend . error ( errorMsg ) ;
7282 // Store the last error message
7383 lastBackendError = errorMsg ;
84+
85+ // Send error message to log window
86+ if ( logWindow ) {
87+ const htmlError = convert . toHtml ( errorMsg ) ;
88+ logWindow . webContents . send ( "log" , htmlError ) ;
89+ }
7490 } ) ;
7591
7692 // Handle spawn errors
@@ -120,8 +136,20 @@ function stopBackend() {
120136 // Only stop if process exists and is still running
121137 if ( backendProcess && ! backendProcess . killed ) {
122138 logger . backend . info ( "Stopping backend..." ) ;
123- // Send termination signal
124- backendProcess . kill ( "SIGTERM" ) ;
139+
140+ backendProcess . stdin . end ( ) ;
141+
142+ const fallbackTimer = setTimeout ( ( ) => {
143+ if ( backendProcess && ! backendProcess . killed ) {
144+ logger . backend . warning (
145+ "Backend did not exit gracefully, force killing..."
146+ ) ;
147+ backendProcess . kill ( "SIGKILL" ) ;
148+ }
149+ } , 2000 ) ;
150+
151+ fallbackTimer . unref ( ) ;
152+
125153 // Clear the process reference
126154 backendProcess = null ;
127155 }
0 commit comments