|
1 | 1 | let gnomonProcess = null; |
| 2 | +let gnomonLogBuffer = []; |
| 3 | +const MAX_GNOMON_LOG_LINES = 5000; |
| 4 | + |
2 | 5 | let telaServerProcess = null; |
3 | 6 |
|
4 | 7 |
|
@@ -111,7 +114,26 @@ let gnomonNodeAddress = loadGnomonConfig().node; |
111 | 114 |
|
112 | 115 | // ---------------- Gnomon ---------------- |
113 | 116 |
|
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 ---------- */ |
115 | 137 | ipcMain.handle("check-gnomon", () => { |
116 | 138 | return new Promise((resolve) => { |
117 | 139 | const req = http.get( |
@@ -131,42 +153,21 @@ ipcMain.handle("check-gnomon", () => { |
131 | 153 | }); |
132 | 154 | }); |
133 | 155 |
|
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(""); |
161 | 161 | }); |
162 | 162 |
|
163 | | -// -------------------- Gnomon Start/Stop --------------------------------- |
164 | | - |
| 163 | +/* ---------- IPC: start Gnomon ---------- */ |
165 | 164 | ipcMain.handle("gnomon:start", async () => { |
166 | 165 | if (gnomonProcess) return { running: true }; |
167 | 166 |
|
168 | 167 | const gnomonPath = path.join(__dirname, "resources", "gnomonindexer"); |
169 | 168 |
|
| 169 | + gnomonLogBuffer.push("\n--- Gnomon starting ---\n"); |
| 170 | + |
170 | 171 | gnomonProcess = spawn(gnomonPath, [ |
171 | 172 | `--daemon-rpc-address=${gnomonNodeAddress}`, |
172 | 173 | "--fastsync", |
@@ -175,55 +176,49 @@ ipcMain.handle("gnomon:start", async () => { |
175 | 176 | '--search-filter="telaVersion"' |
176 | 177 | ]); |
177 | 178 |
|
178 | | - // Send live stdout logs to renderer |
179 | | - gnomonProcess.stdout.on('data', (data) => { |
| 179 | + gnomonProcess.stdout.on("data", (data) => { |
180 | 180 | const str = data.toString(); |
181 | | - mainWindow.webContents.send('gnomon-log', str); |
| 181 | + pushGnomonLog(str); |
182 | 182 |
|
183 | | - // 🔹 Detect new SCID indexed lines (adjust regex to your output) |
| 183 | + // Detect indexed SCIDs |
184 | 184 | const match = str.match(/Indexed SCID:\s+([a-z0-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]); |
188 | 187 | } |
189 | 188 | }); |
190 | 189 |
|
191 | | - // Send live stderr logs to renderer |
192 | 190 | gnomonProcess.stderr.on("data", (data) => { |
193 | | - if (mainWindow && !mainWindow.isDestroyed()) { |
194 | | - mainWindow.webContents.send("gnomon-log", data.toString()); |
195 | | - } |
| 191 | + pushGnomonLog(data.toString()); |
196 | 192 | }); |
197 | 193 |
|
198 | | - // Handle process exit |
199 | | - gnomonProcess.on("exit", (code) => { |
| 194 | + gnomonProcess.once("exit", (code) => { |
| 195 | + pushGnomonLog(`\n--- Gnomon exited (code ${code}) ---\n`); |
200 | 196 | gnomonProcess = null; |
| 197 | + |
201 | 198 | if (mainWindow && !mainWindow.isDestroyed()) { |
202 | 199 | mainWindow.webContents.send("gnomon-exit", { code }); |
203 | 200 | } |
204 | 201 | }); |
205 | 202 |
|
206 | | - // 🔹 Notify the renderer (start.html) that Gnomon started |
207 | 203 | if (mainWindow && !mainWindow.isDestroyed()) { |
208 | | - mainWindow.webContents.send('gnomon-started'); |
| 204 | + mainWindow.webContents.send("gnomon-started"); |
209 | 205 | } |
210 | 206 |
|
211 | 207 | return { started: true }; |
212 | 208 | }); |
213 | 209 |
|
| 210 | +/* ---------- IPC: stop Gnomon ---------- */ |
214 | 211 | ipcMain.handle("gnomon:stop", async () => { |
215 | 212 | if (!gnomonProcess) return { running: false }; |
216 | 213 |
|
217 | | - gnomonProcess.kill("SIGINT"); |
218 | | - gnomonProcess = null; |
| 214 | + pushGnomonLog("\n--- Stopping Gnomon ---\n"); |
219 | 215 |
|
220 | | - if (mainWindow && !mainWindow.isDestroyed()) { |
221 | | - mainWindow.webContents.send("gnomon-exit", { code: null }); |
222 | | - } |
| 216 | + gnomonProcess.kill("SIGINT"); |
223 | 217 |
|
224 | | - return { stopped: true }; |
| 218 | + return { stopping: true }; |
225 | 219 | }); |
226 | 220 |
|
| 221 | + |
227 | 222 | // ---------- IPC to apply the node address to Gnomon ------------------- |
228 | 223 | ipcMain.handle("gnomon:apply-node", async (event, nodeAddress) => { |
229 | 224 | if (!nodeAddress || typeof nodeAddress !== "string") { |
|