-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathelectron.main.js
More file actions
140 lines (119 loc) · 4.31 KB
/
electron.main.js
File metadata and controls
140 lines (119 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const { app, BrowserWindow } = require('electron');
const path = require('path');
const { fork } = require('child_process');
let serverProcess;
let metricsProcesses = new Map();
function startServer() {
if (app.isPackaged) {
const serverPath = path.join(process.resourcesPath, 'server-backend.js');
console.log(`Starting backend server from: ${serverPath}`);
serverProcess = fork(serverPath);
serverProcess.on('close', (code) => {
console.log(`Backend server exited with code ${code}`);
});
serverProcess.on('error', (err) => {
console.error(`Backend server error: ${err}`);
});
}
}
function startMetrics(serverConnectionId, serverConnectionDetails) {
const dataDir = path.join(app.getPath('userData'), 'metrics-data', serverConnectionId);
let metricsServerPath;
let configPath;
if (app.isPackaged) {
metricsServerPath = path.join(process.resourcesPath, 'server-metrics.js');
configPath = path.join(process.resourcesPath, 'config.yml'); // Path for production
} else {
metricsServerPath = path.join(__dirname, '../../metrics/src/index.js');
configPath = path.join(__dirname, '../../metrics/config.yml'); // Path for development
}
console.log(`Starting metrics server for connection ${serverConnectionId}...`);
const metricsProcess = fork(metricsServerPath, [], {
env: {
...process.env,
PORT: 0,
DATA_DIR: dataDir,
VALKEY_URL: `valkey://${serverConnectionDetails.host}:${serverConnectionDetails.port}`,
CONFIG_PATH: configPath // Explicitly provide the config path
}
});
metricsProcesses.set(serverConnectionId, metricsProcess);
metricsProcess.on('message', (message) => {
if (message && message.type === 'metrics-started') {
console.log(`Metrics server for ${serverConnectionId} started successfully on host: ${message.payload.metricsHost} port ${message.payload.metricsPort}`);
}
});
metricsProcess.on('close', (code) => {
console.log(`Metrics server for connection ${serverConnectionId} exited with code ${code}`);
metricsProcesses.delete(serverConnectionId);
});
metricsProcess.on('error', (err) => {
console.error(`Metrics server for connection ${serverConnectionId} error: ${err}`);
});
}
// Disconnect functionality in the server has not been implemented. Once that is implemented, this can be used.
function stopMetricServer(serverConnectionId) {
metricsProcesses.get(serverConnectionId).kill();
}
function stopMetricServers() {
metricsProcesses.forEach((_serverConnectionId, metricProcess) => {
metricProcess.kill();
})
}
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true
}
});
if (app.isPackaged) {
win.loadFile(path.join(__dirname, 'dist', 'index.html'));
} else {
win.loadURL('http://localhost:5173');
win.webContents.openDevTools();
}
}
app.whenReady().then(() => {
startServer();
if (serverProcess) {
serverProcess.on('message', (message) => {
switch (message.type) {
case 'websocket-ready':
createWindow();
break;
case 'valkeyConnection/standaloneConnectFulfilled':
startMetrics(message.payload.connectionId, message.payload.connectionDetails);
break;
default:
try {
console.log(`Received unknown server message: ${JSON.stringify(message)}`);
} catch (_) {
console.log(`Received unknown server message: ${message}`);
}
}
});
} else {
createWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('before-quit', () => {
if (serverProcess) {
serverProcess.kill();
}
if (metricsProcesses.length > 0) {
stopMetricServers();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});