@aidra/pmx
v9.48.0
Published
require('pm2')-compatible programmatic API for the pmx process manager
Readme
@aidra/pmx
A drop-in replacement for require('pm2') that drives the pmx daemon. It has no runtime
dependencies and contains no daemon: it talks to $PMX_HOME/rpc.sock (RPC) and
$PMX_HOME/pub.sock (event bus) using the axon-compatible wire protocol of
specs/20-daemon.md §5.
const pm2 = require('@aidra/pmx'); // or: npm i pm2@npm:@aidra/pmx and keep require('pm2')
pm2.connect((err) => {
if (err) throw err;
pm2.start({ script: 'app.js', name: 'api', instances: 2 }, (err, procs) => {
pm2.list((err, list) => {
console.log(list.map((p) => `${p.name} ${p.pm2_env.status}`));
pm2.disconnect();
});
});
});- CommonJS and ESM (
import pm2 from '@aidra/pmx');index.d.tsdeclares every method. - Callbacks only, as in pm2.
pm2.promises.<method>(...)is an optional Promise façade. new pm2.custom({ pm2_home, independent, daemon_mode, cwd })creates independent instances; the PM2 home is per instance.- Loading the module sets
process.env.PMX_PROGRAMMATICandprocess.env.PM2_PROGRAMMATICto'true', which silences the CLI-style messages. - Environment variables: every setting pm2 reads as
PM2_<X>(PM2_HOME,PM2_NODE,PM2_SILENT,PM2_CONCURRENT_ACTIONS, the per-file path overrides,PM2_PUBLIC_KEY, …) is also read asPMX_<X>, and thePMX_name wins (thenPM2_<X>; one warning when both are set and differ).KEYMETRICS_*and the barePUBLIC_KEY/SECRET_KEYare no longer read (pmx 9.33). Variables the package gives the daemon and apps (PMX_HOME/PM2_HOME, theservesettings) are set under both names. ThePMX_forms need @aidra/pmx and pmx 9.33 or later. See the environment variable reference.
Daemon autostart
connect() (or any method called before connect()) first probes rpc.sock. When no daemon
answers, the package starts one by running the pmx binary with its hidden daemon entry point:
<pmx binary> daemon # env PMX_HOME=PM2_HOME=<home>, SILENT=trueThe binary is located in this order:
PMX_BIN— used as given;PMX_BINARY(orPM2_BINARY), thenpmxonPATH, thenpm2onPATH— each only if it is a native executable that answers--versionwith a pmx version (9.x or later). Script files (an npm-installed Nodepm2, which starts its own daemon) are never executed.
If none is found, calls that need a daemon fail with
Error('pmx binary not found (set PMX_BIN or put pmx on the PATH)').
- Daemon mode (default): the daemon is spawned detached (new session) with stdout/stderr
appended to
$PMX_HOME/pm2.log; the API waits until both sockets accept connections. The systemd service guard (PMX_SERVICE_GUARD=off, orPM2_SERVICE_GUARD=off, disables it) can refuse the start; the refusal is passed to the callback. - No-daemon mode (
connect(true, cb),daemon_mode: false,independent: true): the daemon is a non-detached child with inherited stdio; it is stopped bykill()/killDaemon()/destroy()or when the calling process exits. killDaemon()installs a temporarySIGQUIThandler: the daemon signals completion withSIGQUITto the caller's pid.
Delegated methods
Methods that are whole CLI workflows run the pmx CLI against the same PMX_HOME and map its
exit status to the callback: install, uninstall, package, publish,
generateModuleSample, deploy, serve, startup, uninstallStartup, logrotate,
update, deepUpdate, report, generateSample, boilerplate, dockerMode,
generateDockerfile, streamLogs, printLogs, pullAndReload, pullCommitId, backward,
forward, link, unlink, linkManagement, openDashboard. Everything else speaks the
RPC/bus protocol directly.
Tests
npm test # fake daemon, golden-byte framing, config handling
PMX_BIN=/path/to/pmx npm test # also runs test/real-daemon.test.js against a real daemon