Merge branch 'pr-130-source' into test-pr-130
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||||
import { execSync } from 'child_process';
|
import { execSync, spawnSync } from 'child_process';
|
||||||
import { join, dirname } from 'path';
|
import { join, dirname } from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
@@ -260,10 +260,33 @@ async function main() {
|
|||||||
log('', colors.reset);
|
log('', colors.reset);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to start the PM2 worker after fresh install
|
||||||
|
try {
|
||||||
|
log('🚀 Starting worker service...', colors.cyan);
|
||||||
|
// On Windows, PM2 executable is pm2.cmd, not pm2
|
||||||
|
const localPm2Base = join(NODE_MODULES_PATH, '.bin', 'pm2');
|
||||||
|
const localPm2Cmd = process.platform === 'win32' ? localPm2Base + '.cmd' : localPm2Base;
|
||||||
|
const pm2Command = existsSync(localPm2Cmd) ? localPm2Cmd : 'pm2';
|
||||||
|
const ecosystemPath = join(PLUGIN_ROOT, 'ecosystem.config.cjs');
|
||||||
|
|
||||||
|
// Using spawnSync with array args to avoid command injection risks
|
||||||
|
const result = spawnSync(pm2Command, ['start', ecosystemPath], {
|
||||||
|
cwd: PLUGIN_ROOT,
|
||||||
|
stdio: 'pipe',
|
||||||
|
encoding: 'utf-8'
|
||||||
|
});
|
||||||
|
if (result.status !== 0) {
|
||||||
|
throw new Error(result.stderr || 'PM2 start failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Worker will be started lazily when needed (e.g., when save-hook sends data)
|
log('✅ Worker service started', colors.green);
|
||||||
// Context hook only needs database access, not the worker service
|
} catch (error) {
|
||||||
|
// Worker might already be running or PM2 not available - that's okay
|
||||||
|
// The ensureWorkerRunning() function will handle auto-start when needed
|
||||||
|
log('ℹ️ Worker will start automatically when needed', colors.dim);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Success - dependencies installed (if needed)
|
// Success - dependencies installed (if needed)
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { homedir } from "os";
|
import { homedir } from "os";
|
||||||
import { existsSync, readFileSync } from "fs";
|
import { existsSync, readFileSync } from "fs";
|
||||||
import { execSync } from "child_process";
|
import { spawnSync } from "child_process";
|
||||||
import { getPackageRoot } from "./paths.js";
|
import { getPackageRoot } from "./paths.js";
|
||||||
|
|
||||||
// Named constants for health checks
|
// Named constants for health checks
|
||||||
@@ -55,13 +55,23 @@ async function startWorker(): Promise<boolean> {
|
|||||||
throw new Error(`Ecosystem config not found at ${ecosystemPath}`);
|
throw new Error(`Ecosystem config not found at ${ecosystemPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to use local PM2 from node_modules first, fall back to global PM2
|
||||||
|
// On Windows, PM2 executable is pm2.cmd, not pm2
|
||||||
|
const localPm2Base = path.join(pluginRoot, 'node_modules', '.bin', 'pm2');
|
||||||
|
const localPm2Cmd = process.platform === 'win32' ? localPm2Base + '.cmd' : localPm2Base;
|
||||||
|
const pm2Command = existsSync(localPm2Cmd) ? localPm2Cmd : 'pm2';
|
||||||
|
|
||||||
// Start using PM2 with the ecosystem config
|
// Start using PM2 with the ecosystem config
|
||||||
// CRITICAL: Must set cwd to pluginRoot so PM2 starts from marketplace directory
|
// CRITICAL: Must set cwd to pluginRoot so PM2 starts from marketplace directory
|
||||||
execSync(`pm2 start "${ecosystemPath}"`, {
|
// Using spawnSync with array args to avoid command injection risks
|
||||||
|
const result = spawnSync(pm2Command, ['start', ecosystemPath], {
|
||||||
cwd: pluginRoot,
|
cwd: pluginRoot,
|
||||||
stdio: 'pipe',
|
stdio: 'pipe',
|
||||||
encoding: 'utf-8'
|
encoding: 'utf-8'
|
||||||
});
|
});
|
||||||
|
if (result.status !== 0) {
|
||||||
|
throw new Error(result.stderr || 'PM2 start failed');
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for worker to become healthy
|
// Wait for worker to become healthy
|
||||||
for (let i = 0; i < WORKER_STARTUP_RETRIES; i++) {
|
for (let i = 0; i < WORKER_STARTUP_RETRIES; i++) {
|
||||||
@@ -93,10 +103,13 @@ export async function ensureWorkerRunning(): Promise<void> {
|
|||||||
|
|
||||||
if (!started) {
|
if (!started) {
|
||||||
const port = getWorkerPort();
|
const port = getWorkerPort();
|
||||||
|
const pluginRoot = getPackageRoot();
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Worker service failed to start on port ${port}.\n\n` +
|
`Worker service failed to start on port ${port}.\n\n` +
|
||||||
`Try manually running: pm2 start ecosystem.config.cjs\n` +
|
`To start manually, run:\n` +
|
||||||
`Or restart: pm2 restart claude-mem-worker`
|
` cd ${pluginRoot}\n` +
|
||||||
|
` npx pm2 start ecosystem.config.cjs\n\n` +
|
||||||
|
`If already running, try: npx pm2 restart claude-mem-worker`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user