fix: improve worker startup on fresh install
This fixes the issue where the worker service wouldn't start automatically after installing the plugin, leaving users with confusing error messages. Changes: - Update worker-utils.ts to use local PM2 from node_modules instead of requiring it in PATH - Improve error messages to suggest npx pm2 commands - Add auto-start attempt in smart-install.js after fresh installation - Fall back gracefully if worker can't start (will auto-start on first use) Fixes issue where users with PM2 not in their PATH couldn't start the worker.
This commit is contained in:
@@ -260,10 +260,27 @@ async function main() {
|
|||||||
log('', colors.reset);
|
log('', colors.reset);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Worker will be started lazily when needed (e.g., when save-hook sends data)
|
// Try to start the PM2 worker after fresh install
|
||||||
// Context hook only needs database access, not the worker service
|
try {
|
||||||
|
log('🚀 Starting worker service...', colors.cyan);
|
||||||
|
const localPm2 = join(NODE_MODULES_PATH, '.bin', 'pm2');
|
||||||
|
const pm2Command = existsSync(localPm2) ? localPm2 : 'pm2';
|
||||||
|
const ecosystemPath = join(PLUGIN_ROOT, 'ecosystem.config.cjs');
|
||||||
|
|
||||||
|
execSync(`"${pm2Command}" start "${ecosystemPath}"`, {
|
||||||
|
cwd: PLUGIN_ROOT,
|
||||||
|
stdio: 'pipe',
|
||||||
|
encoding: 'utf-8'
|
||||||
|
});
|
||||||
|
|
||||||
|
log('✅ Worker service started', colors.green);
|
||||||
|
} 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);
|
||||||
|
|||||||
@@ -55,9 +55,13 @@ 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
|
||||||
|
const localPm2 = path.join(pluginRoot, 'node_modules', '.bin', 'pm2');
|
||||||
|
const pm2Command = existsSync(localPm2) ? localPm2 : '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}"`, {
|
execSync(`"${pm2Command}" start "${ecosystemPath}"`, {
|
||||||
cwd: pluginRoot,
|
cwd: pluginRoot,
|
||||||
stdio: 'pipe',
|
stdio: 'pipe',
|
||||||
encoding: 'utf-8'
|
encoding: 'utf-8'
|
||||||
@@ -93,10 +97,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