11let gnomonProcess = null ;
2+ let gnomonLogBuffer = [ ] ;
3+ const MAX_GNOMON_LOG_LINES = 5000 ;
4+
25let telaServerProcess = null ;
36
47
@@ -111,7 +114,26 @@ let gnomonNodeAddress = loadGnomonConfig().node;
111114
112115// ---------------- Gnomon ----------------
113116
114- /* ---- Gnomon status check ---- */
117+ /* ---------- Log buffer helper ---------- */
118+ function pushGnomonLog ( str ) {
119+ if ( ! str ) return ;
120+
121+ gnomonLogBuffer . push ( str ) ;
122+
123+ // Prevent memory bloat
124+ if ( gnomonLogBuffer . length > MAX_GNOMON_LOG_LINES ) {
125+ gnomonLogBuffer . splice (
126+ 0 ,
127+ gnomonLogBuffer . length - MAX_GNOMON_LOG_LINES
128+ ) ;
129+ }
130+
131+ if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
132+ mainWindow . webContents . send ( "gnomon-log" , str ) ;
133+ }
134+ }
135+
136+ /* ---------- IPC: status check ---------- */
115137ipcMain . handle ( "check-gnomon" , ( ) => {
116138 return new Promise ( ( resolve ) => {
117139 const req = http . get (
@@ -131,42 +153,21 @@ ipcMain.handle("check-gnomon", () => {
131153 } ) ;
132154} ) ;
133155
134-
135- /* ---- Gnomon getInfo ---- */
136- ipcMain . handle ( "gnomon:get-info" , async ( ) => {
137- return new Promise ( ( resolve ) => {
138- const req = http . get (
139- "http://127.0.0.1:8099/api/getinfo" ,
140- { timeout : 1500 } ,
141- ( res ) => {
142- let data = "" ;
143-
144- res . on ( "data" , chunk => data += chunk ) ;
145- res . on ( "end" , ( ) => {
146- try {
147- resolve ( JSON . parse ( data ) ) ;
148- } catch {
149- resolve ( null ) ;
150- }
151- } ) ;
152- }
153- ) ;
154-
155- req . on ( "error" , ( ) => resolve ( null ) ) ;
156- req . on ( "timeout" , ( ) => {
157- req . destroy ( ) ;
158- resolve ( null ) ;
159- } ) ;
160- } ) ;
156+ /* ---------- IPC: get log buffer ---------- */
157+ ipcMain . handle ( "gnomon:get-log-buffer" , ( ) => {
158+ // Send only last N lines
159+ const slice = gnomonLogBuffer . slice ( - 500 ) ;
160+ return slice . join ( "" ) ;
161161} ) ;
162162
163- // -------------------- Gnomon Start/Stop ---------------------------------
164-
163+ /* ---------- IPC: start Gnomon ---------- */
165164ipcMain . handle ( "gnomon:start" , async ( ) => {
166165 if ( gnomonProcess ) return { running : true } ;
167166
168167 const gnomonPath = path . join ( __dirname , "resources" , "gnomonindexer" ) ;
169168
169+ gnomonLogBuffer . push ( "\n--- Gnomon starting ---\n" ) ;
170+
170171 gnomonProcess = spawn ( gnomonPath , [
171172 `--daemon-rpc-address=${ gnomonNodeAddress } ` ,
172173 "--fastsync" ,
@@ -175,55 +176,49 @@ ipcMain.handle("gnomon:start", async () => {
175176 '--search-filter="telaVersion"'
176177 ] ) ;
177178
178- // Send live stdout logs to renderer
179- gnomonProcess . stdout . on ( 'data' , ( data ) => {
179+ gnomonProcess . stdout . on ( "data" , ( data ) => {
180180 const str = data . toString ( ) ;
181- mainWindow . webContents . send ( 'gnomon-log' , str ) ;
181+ pushGnomonLog ( str ) ;
182182
183- // 🔹 Detect new SCID indexed lines (adjust regex to your output)
183+ // Detect indexed SCIDs
184184 const match = str . match ( / I n d e x e d S C I D : \s + ( [ a - z 0 - 9 ] + ) / i) ;
185- if ( match ) {
186- const scid = match [ 1 ] ;
187- mainWindow . webContents . send ( 'gnomon-new-scid' , scid ) ;
185+ if ( match && mainWindow && ! mainWindow . isDestroyed ( ) ) {
186+ mainWindow . webContents . send ( "gnomon-new-scid" , match [ 1 ] ) ;
188187 }
189188 } ) ;
190189
191- // Send live stderr logs to renderer
192190 gnomonProcess . stderr . on ( "data" , ( data ) => {
193- if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
194- mainWindow . webContents . send ( "gnomon-log" , data . toString ( ) ) ;
195- }
191+ pushGnomonLog ( data . toString ( ) ) ;
196192 } ) ;
197193
198- // Handle process exit
199- gnomonProcess . on ( "exit" , ( code ) => {
194+ gnomonProcess . once ( "exit" , ( code ) => {
195+ pushGnomonLog ( `\n--- Gnomon exited (code ${ code } ) ---\n` ) ;
200196 gnomonProcess = null ;
197+
201198 if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
202199 mainWindow . webContents . send ( "gnomon-exit" , { code } ) ;
203200 }
204201 } ) ;
205202
206- // 🔹 Notify the renderer (start.html) that Gnomon started
207203 if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
208- mainWindow . webContents . send ( ' gnomon-started' ) ;
204+ mainWindow . webContents . send ( " gnomon-started" ) ;
209205 }
210206
211207 return { started : true } ;
212208} ) ;
213209
210+ /* ---------- IPC: stop Gnomon ---------- */
214211ipcMain . handle ( "gnomon:stop" , async ( ) => {
215212 if ( ! gnomonProcess ) return { running : false } ;
216213
217- gnomonProcess . kill ( "SIGINT" ) ;
218- gnomonProcess = null ;
214+ pushGnomonLog ( "\n--- Stopping Gnomon ---\n" ) ;
219215
220- if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
221- mainWindow . webContents . send ( "gnomon-exit" , { code : null } ) ;
222- }
216+ gnomonProcess . kill ( "SIGINT" ) ;
223217
224- return { stopped : true } ;
218+ return { stopping : true } ;
225219} ) ;
226220
221+
227222// ---------- IPC to apply the node address to Gnomon -------------------
228223ipcMain . handle ( "gnomon:apply-node" , async ( event , nodeAddress ) => {
229224 if ( ! nodeAddress || typeof nodeAddress !== "string" ) {
0 commit comments