@appkit/llamacpp-cli 1.10.1 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +356 -3
- package/dist/cli.js +99 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/admin/config.d.ts +10 -0
- package/dist/commands/admin/config.d.ts.map +1 -0
- package/dist/commands/admin/config.js +100 -0
- package/dist/commands/admin/config.js.map +1 -0
- package/dist/commands/admin/logs.d.ts +10 -0
- package/dist/commands/admin/logs.d.ts.map +1 -0
- package/dist/commands/admin/logs.js +114 -0
- package/dist/commands/admin/logs.js.map +1 -0
- package/dist/commands/admin/restart.d.ts +2 -0
- package/dist/commands/admin/restart.d.ts.map +1 -0
- package/dist/commands/admin/restart.js +29 -0
- package/dist/commands/admin/restart.js.map +1 -0
- package/dist/commands/admin/start.d.ts +2 -0
- package/dist/commands/admin/start.d.ts.map +1 -0
- package/dist/commands/admin/start.js +30 -0
- package/dist/commands/admin/start.js.map +1 -0
- package/dist/commands/admin/status.d.ts +2 -0
- package/dist/commands/admin/status.d.ts.map +1 -0
- package/dist/commands/admin/status.js +82 -0
- package/dist/commands/admin/status.js.map +1 -0
- package/dist/commands/admin/stop.d.ts +2 -0
- package/dist/commands/admin/stop.d.ts.map +1 -0
- package/dist/commands/admin/stop.js +21 -0
- package/dist/commands/admin/stop.js.map +1 -0
- package/dist/commands/logs.d.ts +1 -0
- package/dist/commands/logs.d.ts.map +1 -1
- package/dist/commands/logs.js +22 -0
- package/dist/commands/logs.js.map +1 -1
- package/dist/commands/server-show.js +1 -1
- package/dist/commands/server-show.js.map +1 -1
- package/dist/lib/admin-manager.d.ts +111 -0
- package/dist/lib/admin-manager.d.ts.map +1 -0
- package/dist/lib/admin-manager.js +413 -0
- package/dist/lib/admin-manager.js.map +1 -0
- package/dist/lib/admin-server.d.ts +148 -0
- package/dist/lib/admin-server.d.ts.map +1 -0
- package/dist/lib/admin-server.js +1161 -0
- package/dist/lib/admin-server.js.map +1 -0
- package/dist/lib/download-job-manager.d.ts +64 -0
- package/dist/lib/download-job-manager.d.ts.map +1 -0
- package/dist/lib/download-job-manager.js +164 -0
- package/dist/lib/download-job-manager.js.map +1 -0
- package/dist/tui/MultiServerMonitorApp.js +2 -2
- package/dist/tui/MultiServerMonitorApp.js.map +1 -1
- package/dist/types/admin-config.d.ts +19 -0
- package/dist/types/admin-config.d.ts.map +1 -0
- package/dist/types/admin-config.js +3 -0
- package/dist/types/admin-config.js.map +1 -0
- package/dist/utils/format-utils.d.ts +6 -0
- package/dist/utils/format-utils.d.ts.map +1 -1
- package/dist/utils/format-utils.js +18 -0
- package/dist/utils/format-utils.js.map +1 -1
- package/dist/utils/log-parser.d.ts +9 -0
- package/dist/utils/log-parser.d.ts.map +1 -1
- package/dist/utils/log-parser.js +11 -0
- package/dist/utils/log-parser.js.map +1 -1
- package/docs/images/web-ui-servers.png +0 -0
- package/package.json +1 -1
- package/src/cli.ts +100 -0
- package/src/commands/admin/config.ts +121 -0
- package/src/commands/admin/logs.ts +91 -0
- package/src/commands/admin/restart.ts +26 -0
- package/src/commands/admin/start.ts +27 -0
- package/src/commands/admin/status.ts +84 -0
- package/src/commands/admin/stop.ts +16 -0
- package/src/commands/logs.ts +24 -0
- package/src/commands/server-show.ts +2 -2
- package/src/lib/admin-manager.ts +435 -0
- package/src/lib/admin-server.ts +1243 -0
- package/src/lib/download-job-manager.ts +213 -0
- package/src/tui/MultiServerMonitorApp.ts +3 -3
- package/src/types/admin-config.ts +25 -0
- package/src/utils/format-utils.ts +18 -0
- package/src/utils/log-parser.ts +13 -0
- package/web/README.md +429 -0
- package/web/eslint.config.js +23 -0
- package/web/index.html +13 -0
- package/web/llamacpp-web-dist.tar.gz +0 -0
- package/web/package-lock.json +4017 -0
- package/web/package.json +38 -0
- package/web/postcss.config.js +6 -0
- package/web/public/vite.svg +1 -0
- package/web/src/App.css +42 -0
- package/web/src/App.tsx +86 -0
- package/web/src/assets/react.svg +1 -0
- package/web/src/components/ApiKeyPrompt.tsx +71 -0
- package/web/src/components/CreateServerModal.tsx +372 -0
- package/web/src/components/DownloadProgress.tsx +123 -0
- package/web/src/components/Nav.tsx +89 -0
- package/web/src/components/RouterConfigModal.tsx +240 -0
- package/web/src/components/SearchModal.tsx +306 -0
- package/web/src/components/ServerConfigModal.tsx +291 -0
- package/web/src/hooks/useApi.ts +259 -0
- package/web/src/index.css +42 -0
- package/web/src/lib/api.ts +226 -0
- package/web/src/main.tsx +10 -0
- package/web/src/pages/Dashboard.tsx +103 -0
- package/web/src/pages/Models.tsx +258 -0
- package/web/src/pages/Router.tsx +270 -0
- package/web/src/pages/RouterLogs.tsx +201 -0
- package/web/src/pages/ServerLogs.tsx +553 -0
- package/web/src/pages/Servers.tsx +358 -0
- package/web/src/types/api.ts +140 -0
- package/web/tailwind.config.js +31 -0
- package/web/tsconfig.app.json +28 -0
- package/web/tsconfig.json +7 -0
- package/web/tsconfig.node.json +26 -0
- package/web/vite.config.ts +25 -0
- package/MONITORING-ACCURACY-FIX.md +0 -199
- package/PER-PROCESS-METRICS.md +0 -190
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { adminManager } from '../../lib/admin-manager';
|
|
3
|
+
|
|
4
|
+
export async function adminStopCommand(): Promise<void> {
|
|
5
|
+
try {
|
|
6
|
+
console.log(chalk.blue('⏸ Stopping admin service...\n'));
|
|
7
|
+
|
|
8
|
+
await adminManager.stop();
|
|
9
|
+
|
|
10
|
+
console.log(chalk.green('✓ Admin service stopped successfully'));
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.error(chalk.red('✗ Failed to stop admin service'));
|
|
13
|
+
console.error(chalk.gray((error as Error).message));
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/commands/logs.ts
CHANGED
|
@@ -27,6 +27,7 @@ interface LogsOptions {
|
|
|
27
27
|
rotate?: boolean;
|
|
28
28
|
clearArchived?: boolean;
|
|
29
29
|
clearAll?: boolean;
|
|
30
|
+
includeHealth?: boolean;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export async function logsCommand(identifier: string, options: LogsOptions): Promise<void> {
|
|
@@ -142,6 +143,9 @@ export async function logsCommand(identifier: string, options: LogsOptions): Pro
|
|
|
142
143
|
let filterDesc = '';
|
|
143
144
|
let useCompactMode = false;
|
|
144
145
|
|
|
146
|
+
// Whether to include health check requests (filtered by default)
|
|
147
|
+
const includeHealth = options.includeHealth ?? false;
|
|
148
|
+
|
|
145
149
|
if (options.verbose) {
|
|
146
150
|
// Show everything (no filter)
|
|
147
151
|
filterDesc = ' (all messages)';
|
|
@@ -195,7 +199,15 @@ export async function logsCommand(identifier: string, options: LogsOptions): Pro
|
|
|
195
199
|
|
|
196
200
|
rl.on('line', (line) => {
|
|
197
201
|
if (line.includes('log_server_r')) {
|
|
202
|
+
// Skip health check requests unless --include-health is set
|
|
203
|
+
if (!includeHealth && logParser.isHealthCheckRequest(line)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
198
206
|
logParser.processLine(line, (compactLine) => {
|
|
207
|
+
// Double-check the parsed line for health checks (in case buffered)
|
|
208
|
+
if (!includeHealth && logParser.isHealthCheckRequest(compactLine)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
199
211
|
console.log(compactLine);
|
|
200
212
|
});
|
|
201
213
|
}
|
|
@@ -266,13 +278,25 @@ export async function logsCommand(identifier: string, options: LogsOptions): Pro
|
|
|
266
278
|
|
|
267
279
|
const compactLines: string[] = [];
|
|
268
280
|
for (const line of logLines) {
|
|
281
|
+
// Skip health check requests unless --include-health is set
|
|
282
|
+
if (!includeHealth && logParser.isHealthCheckRequest(line)) {
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
269
285
|
logParser.processLine(line, (compactLine) => {
|
|
286
|
+
// Double-check the parsed line for health checks (in case buffered)
|
|
287
|
+
if (!includeHealth && logParser.isHealthCheckRequest(compactLine)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
270
290
|
compactLines.push(compactLine);
|
|
271
291
|
});
|
|
272
292
|
}
|
|
273
293
|
|
|
274
294
|
// Flush any remaining buffered logs (handles simple format)
|
|
275
295
|
logParser.flush((compactLine) => {
|
|
296
|
+
// Filter health checks from flushed lines too
|
|
297
|
+
if (!includeHealth && logParser.isHealthCheckRequest(compactLine)) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
276
300
|
compactLines.push(compactLine);
|
|
277
301
|
});
|
|
278
302
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import { stateManager } from '../lib/state-manager';
|
|
3
3
|
import { statusChecker } from '../lib/status-checker';
|
|
4
|
-
import { formatUptime, formatBytes } from '../utils/format-utils';
|
|
4
|
+
import { formatUptime, formatBytes, formatContextSize } from '../utils/format-utils';
|
|
5
5
|
import { getProcessMemory } from '../utils/process-utils';
|
|
6
6
|
import { getFileSize, formatFileSize, getArchivedLogInfo } from '../utils/log-utils';
|
|
7
7
|
import { fileExists } from '../utils/file-utils';
|
|
@@ -89,7 +89,7 @@ export async function serverShowCommand(identifier: string): Promise<void> {
|
|
|
89
89
|
console.log(chalk.bold('Configuration:'));
|
|
90
90
|
console.log('─'.repeat(70));
|
|
91
91
|
console.log(`${chalk.bold('Threads:')} ${updatedServer.threads}`);
|
|
92
|
-
console.log(`${chalk.bold('Context Size:')} ${updatedServer.ctxSize
|
|
92
|
+
console.log(`${chalk.bold('Context Size:')} ${formatContextSize(updatedServer.ctxSize)} (total)`);
|
|
93
93
|
console.log(`${chalk.bold('GPU Layers:')} ${updatedServer.gpuLayers}`);
|
|
94
94
|
console.log(`${chalk.bold('Embeddings:')} ${updatedServer.embeddings ? 'enabled' : 'disabled'}`);
|
|
95
95
|
console.log(`${chalk.bold('Jinja:')} ${updatedServer.jinja ? 'enabled' : 'disabled'}`);
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as fs from 'fs/promises';
|
|
3
|
+
import * as crypto from 'crypto';
|
|
4
|
+
import { AdminConfig } from '../types/admin-config';
|
|
5
|
+
import { execCommand, execAsync } from '../utils/process-utils';
|
|
6
|
+
import {
|
|
7
|
+
ensureDir,
|
|
8
|
+
writeJsonAtomic,
|
|
9
|
+
readJson,
|
|
10
|
+
fileExists,
|
|
11
|
+
getConfigDir,
|
|
12
|
+
getLogsDir,
|
|
13
|
+
getLaunchAgentsDir,
|
|
14
|
+
writeFileAtomic,
|
|
15
|
+
} from '../utils/file-utils';
|
|
16
|
+
|
|
17
|
+
export interface AdminServiceStatus {
|
|
18
|
+
isRunning: boolean;
|
|
19
|
+
pid: number | null;
|
|
20
|
+
exitCode: number | null;
|
|
21
|
+
lastExitReason?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class AdminManager {
|
|
25
|
+
private configDir: string;
|
|
26
|
+
private logsDir: string;
|
|
27
|
+
private configPath: string;
|
|
28
|
+
private launchAgentsDir: string;
|
|
29
|
+
|
|
30
|
+
constructor() {
|
|
31
|
+
this.configDir = getConfigDir();
|
|
32
|
+
this.logsDir = getLogsDir();
|
|
33
|
+
this.configPath = path.join(this.configDir, 'admin.json');
|
|
34
|
+
this.launchAgentsDir = getLaunchAgentsDir();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Initialize admin directories
|
|
39
|
+
*/
|
|
40
|
+
async initialize(): Promise<void> {
|
|
41
|
+
await ensureDir(this.configDir);
|
|
42
|
+
await ensureDir(this.logsDir);
|
|
43
|
+
await ensureDir(this.launchAgentsDir);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Generate a secure random API key
|
|
48
|
+
*/
|
|
49
|
+
generateApiKey(): string {
|
|
50
|
+
return crypto.randomBytes(32).toString('hex');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get default admin configuration
|
|
55
|
+
*/
|
|
56
|
+
getDefaultConfig(): AdminConfig {
|
|
57
|
+
return {
|
|
58
|
+
id: 'admin',
|
|
59
|
+
port: 9200,
|
|
60
|
+
host: '127.0.0.1',
|
|
61
|
+
apiKey: this.generateApiKey(),
|
|
62
|
+
label: 'com.llama.admin',
|
|
63
|
+
plistPath: path.join(this.launchAgentsDir, 'com.llama.admin.plist'),
|
|
64
|
+
stdoutPath: path.join(this.logsDir, 'admin.stdout'),
|
|
65
|
+
stderrPath: path.join(this.logsDir, 'admin.stderr'),
|
|
66
|
+
requestTimeout: 30000,
|
|
67
|
+
verbose: false,
|
|
68
|
+
status: 'stopped',
|
|
69
|
+
createdAt: new Date().toISOString(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Load admin configuration
|
|
75
|
+
*/
|
|
76
|
+
async loadConfig(): Promise<AdminConfig | null> {
|
|
77
|
+
if (!(await fileExists(this.configPath))) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return await readJson<AdminConfig>(this.configPath);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Save admin configuration
|
|
85
|
+
*/
|
|
86
|
+
async saveConfig(config: AdminConfig): Promise<void> {
|
|
87
|
+
await writeJsonAtomic(this.configPath, config);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Update admin configuration with partial changes
|
|
92
|
+
*/
|
|
93
|
+
async updateConfig(updates: Partial<AdminConfig>): Promise<void> {
|
|
94
|
+
const existingConfig = await this.loadConfig();
|
|
95
|
+
if (!existingConfig) {
|
|
96
|
+
throw new Error('Admin configuration not found');
|
|
97
|
+
}
|
|
98
|
+
const updatedConfig = { ...existingConfig, ...updates };
|
|
99
|
+
await this.saveConfig(updatedConfig);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Delete admin configuration
|
|
104
|
+
*/
|
|
105
|
+
async deleteConfig(): Promise<void> {
|
|
106
|
+
if (await fileExists(this.configPath)) {
|
|
107
|
+
await fs.unlink(this.configPath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Regenerate API key
|
|
113
|
+
*/
|
|
114
|
+
async regenerateApiKey(): Promise<string> {
|
|
115
|
+
const config = await this.loadConfig();
|
|
116
|
+
if (!config) {
|
|
117
|
+
throw new Error('Admin configuration not found');
|
|
118
|
+
}
|
|
119
|
+
const newApiKey = this.generateApiKey();
|
|
120
|
+
await this.updateConfig({ apiKey: newApiKey });
|
|
121
|
+
return newApiKey;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Generate plist XML content for the admin service
|
|
126
|
+
*/
|
|
127
|
+
generatePlist(config: AdminConfig): string {
|
|
128
|
+
// Find the compiled admin-server.js file
|
|
129
|
+
// In dev mode (tsx), __dirname is src/lib/
|
|
130
|
+
// In production, __dirname is dist/lib/
|
|
131
|
+
// Always use the compiled dist version for launchctl
|
|
132
|
+
let adminServerPath: string;
|
|
133
|
+
if (__dirname.includes('/src/')) {
|
|
134
|
+
// Dev mode - point to dist/lib/admin-server.js
|
|
135
|
+
const projectRoot = path.resolve(__dirname, '../..');
|
|
136
|
+
adminServerPath = path.join(projectRoot, 'dist/lib/admin-server.js');
|
|
137
|
+
} else {
|
|
138
|
+
// Production mode - already in dist/lib/
|
|
139
|
+
adminServerPath = path.join(__dirname, 'admin-server.js');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Use the current Node.js executable path (resolves symlinks)
|
|
143
|
+
const nodePath = process.execPath;
|
|
144
|
+
|
|
145
|
+
const args = [
|
|
146
|
+
nodePath,
|
|
147
|
+
adminServerPath,
|
|
148
|
+
'--config', this.configPath,
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
const argsXml = args.map(arg => ` <string>${arg}</string>`).join('\n');
|
|
152
|
+
|
|
153
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
154
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
|
|
155
|
+
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
156
|
+
<plist version="1.0">
|
|
157
|
+
<dict>
|
|
158
|
+
<key>Label</key>
|
|
159
|
+
<string>${config.label}</string>
|
|
160
|
+
|
|
161
|
+
<key>ProgramArguments</key>
|
|
162
|
+
<array>
|
|
163
|
+
${argsXml}
|
|
164
|
+
</array>
|
|
165
|
+
|
|
166
|
+
<key>RunAtLoad</key>
|
|
167
|
+
<false/>
|
|
168
|
+
|
|
169
|
+
<key>KeepAlive</key>
|
|
170
|
+
<dict>
|
|
171
|
+
<key>Crashed</key>
|
|
172
|
+
<true/>
|
|
173
|
+
<key>SuccessfulExit</key>
|
|
174
|
+
<false/>
|
|
175
|
+
</dict>
|
|
176
|
+
|
|
177
|
+
<key>StandardOutPath</key>
|
|
178
|
+
<string>${config.stdoutPath}</string>
|
|
179
|
+
|
|
180
|
+
<key>StandardErrorPath</key>
|
|
181
|
+
<string>${config.stderrPath}</string>
|
|
182
|
+
|
|
183
|
+
<key>WorkingDirectory</key>
|
|
184
|
+
<string>/tmp</string>
|
|
185
|
+
|
|
186
|
+
<key>ThrottleInterval</key>
|
|
187
|
+
<integer>10</integer>
|
|
188
|
+
</dict>
|
|
189
|
+
</plist>
|
|
190
|
+
`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Create and write plist file
|
|
195
|
+
*/
|
|
196
|
+
async createPlist(config: AdminConfig): Promise<void> {
|
|
197
|
+
const plistContent = this.generatePlist(config);
|
|
198
|
+
await writeFileAtomic(config.plistPath, plistContent);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Delete plist file
|
|
203
|
+
*/
|
|
204
|
+
async deletePlist(config: AdminConfig): Promise<void> {
|
|
205
|
+
if (await fileExists(config.plistPath)) {
|
|
206
|
+
await fs.unlink(config.plistPath);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Load service (register with launchctl)
|
|
212
|
+
*/
|
|
213
|
+
async loadService(plistPath: string): Promise<void> {
|
|
214
|
+
await execCommand(`launchctl load "${plistPath}"`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Unload service (unregister from launchctl)
|
|
219
|
+
*/
|
|
220
|
+
async unloadService(plistPath: string): Promise<void> {
|
|
221
|
+
try {
|
|
222
|
+
await execCommand(`launchctl unload "${plistPath}"`);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
// Ignore errors if service is not loaded
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Start service
|
|
230
|
+
*/
|
|
231
|
+
async startService(label: string): Promise<void> {
|
|
232
|
+
await execCommand(`launchctl start ${label}`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Stop service
|
|
237
|
+
*/
|
|
238
|
+
async stopService(label: string): Promise<void> {
|
|
239
|
+
await execCommand(`launchctl stop ${label}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Get service status from launchctl
|
|
244
|
+
*/
|
|
245
|
+
async getServiceStatus(label: string): Promise<AdminServiceStatus> {
|
|
246
|
+
try {
|
|
247
|
+
const { stdout } = await execAsync(`launchctl list | grep ${label}`);
|
|
248
|
+
const lines = stdout.trim().split('\n');
|
|
249
|
+
|
|
250
|
+
for (const line of lines) {
|
|
251
|
+
const parts = line.split(/\s+/);
|
|
252
|
+
if (parts.length >= 3) {
|
|
253
|
+
const pidStr = parts[0].trim();
|
|
254
|
+
const exitCodeStr = parts[1].trim();
|
|
255
|
+
const serviceLabel = parts[2].trim();
|
|
256
|
+
|
|
257
|
+
if (serviceLabel === label) {
|
|
258
|
+
const pid = pidStr !== '-' ? parseInt(pidStr, 10) : null;
|
|
259
|
+
const exitCode = exitCodeStr !== '-' ? parseInt(exitCodeStr, 10) : null;
|
|
260
|
+
const isRunning = pid !== null;
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
isRunning,
|
|
264
|
+
pid,
|
|
265
|
+
exitCode,
|
|
266
|
+
lastExitReason: this.interpretExitCode(exitCode),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
isRunning: false,
|
|
274
|
+
pid: null,
|
|
275
|
+
exitCode: null,
|
|
276
|
+
};
|
|
277
|
+
} catch (error) {
|
|
278
|
+
return {
|
|
279
|
+
isRunning: false,
|
|
280
|
+
pid: null,
|
|
281
|
+
exitCode: null,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Interpret exit code to human-readable reason
|
|
288
|
+
*/
|
|
289
|
+
private interpretExitCode(code: number | null): string | undefined {
|
|
290
|
+
if (code === null || code === 0) return undefined;
|
|
291
|
+
if (code === -9) return 'Force killed (SIGKILL)';
|
|
292
|
+
if (code === -15) return 'Terminated (SIGTERM)';
|
|
293
|
+
return `Exit code: ${code}`;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Wait for service to start (with timeout)
|
|
298
|
+
*/
|
|
299
|
+
async waitForServiceStart(label: string, timeoutMs = 5000): Promise<boolean> {
|
|
300
|
+
const startTime = Date.now();
|
|
301
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
302
|
+
const status = await this.getServiceStatus(label);
|
|
303
|
+
if (status.isRunning) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
307
|
+
}
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Wait for service to stop (with timeout)
|
|
313
|
+
*/
|
|
314
|
+
async waitForServiceStop(label: string, timeoutMs = 5000): Promise<boolean> {
|
|
315
|
+
const startTime = Date.now();
|
|
316
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
317
|
+
const status = await this.getServiceStatus(label);
|
|
318
|
+
if (!status.isRunning) {
|
|
319
|
+
return true;
|
|
320
|
+
}
|
|
321
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
322
|
+
}
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Start admin service
|
|
328
|
+
*/
|
|
329
|
+
async start(): Promise<void> {
|
|
330
|
+
await this.initialize();
|
|
331
|
+
|
|
332
|
+
let config = await this.loadConfig();
|
|
333
|
+
if (!config) {
|
|
334
|
+
// Create default config
|
|
335
|
+
config = this.getDefaultConfig();
|
|
336
|
+
await this.saveConfig(config);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Check if already running
|
|
340
|
+
if (config.status === 'running') {
|
|
341
|
+
throw new Error('Admin service is already running');
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Check for throttled state (exit code 78)
|
|
345
|
+
const currentStatus = await this.getServiceStatus(config.label);
|
|
346
|
+
if (currentStatus.exitCode === 78) {
|
|
347
|
+
// Service is throttled - clean up and start fresh
|
|
348
|
+
await this.unloadService(config.plistPath);
|
|
349
|
+
await this.deletePlist(config);
|
|
350
|
+
// Give launchd a moment to clean up
|
|
351
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Create plist
|
|
355
|
+
await this.createPlist(config);
|
|
356
|
+
|
|
357
|
+
// Load and start service
|
|
358
|
+
try {
|
|
359
|
+
await this.loadService(config.plistPath);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
// May already be loaded
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
await this.startService(config.label);
|
|
365
|
+
|
|
366
|
+
// Wait for startup
|
|
367
|
+
const started = await this.waitForServiceStart(config.label, 5000);
|
|
368
|
+
if (!started) {
|
|
369
|
+
throw new Error('Admin service failed to start');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Update config
|
|
373
|
+
const status = await this.getServiceStatus(config.label);
|
|
374
|
+
await this.updateConfig({
|
|
375
|
+
status: 'running',
|
|
376
|
+
pid: status.pid || undefined,
|
|
377
|
+
lastStarted: new Date().toISOString(),
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Stop admin service
|
|
383
|
+
*/
|
|
384
|
+
async stop(): Promise<void> {
|
|
385
|
+
const config = await this.loadConfig();
|
|
386
|
+
if (!config) {
|
|
387
|
+
throw new Error('Admin configuration not found');
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (config.status !== 'running') {
|
|
391
|
+
throw new Error('Admin service is not running');
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Unload service
|
|
395
|
+
await this.unloadService(config.plistPath);
|
|
396
|
+
|
|
397
|
+
// Wait for shutdown
|
|
398
|
+
await this.waitForServiceStop(config.label, 5000);
|
|
399
|
+
|
|
400
|
+
// Update config
|
|
401
|
+
await this.updateConfig({
|
|
402
|
+
status: 'stopped',
|
|
403
|
+
pid: undefined,
|
|
404
|
+
lastStopped: new Date().toISOString(),
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Restart admin service
|
|
410
|
+
*/
|
|
411
|
+
async restart(): Promise<void> {
|
|
412
|
+
try {
|
|
413
|
+
await this.stop();
|
|
414
|
+
} catch (error) {
|
|
415
|
+
// May not be running
|
|
416
|
+
}
|
|
417
|
+
await this.start();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Get admin status
|
|
422
|
+
*/
|
|
423
|
+
async getStatus(): Promise<{ config: AdminConfig; status: AdminServiceStatus } | null> {
|
|
424
|
+
const config = await this.loadConfig();
|
|
425
|
+
if (!config) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const status = await this.getServiceStatus(config.label);
|
|
430
|
+
return { config, status };
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Export singleton instance
|
|
435
|
+
export const adminManager = new AdminManager();
|