Process management tools
The Necessity of Process Management Tools
Node.js, as a single-threaded runtime environment, can easily block the event loop when handling CPU-intensive tasks. Process management tools help developers fully utilize multi-core CPU resources, improving application performance and stability. Through process management, features like automatic application restart, load balancing, and log collection can be implemented.
Built-in Process Modules in Node.js
Node.js provides the child_process
module for creating and managing child processes. This is the most basic form of process management:
const { spawn } = require('child_process');
// Create a child process to execute the ls command
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
The cluster
module allows easy creation of child processes that share server ports:
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master process ${process.pid} is running`);
// Fork worker processes
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker process ${worker.process.pid} has exited`);
});
} else {
// Worker processes can share any TCP connection
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello World\n');
}).listen(8000);
console.log(`Worker process ${process.pid} started`);
}
PM2 Process Manager
PM2 is one of the most popular process managers for Node.js applications, offering rich features:
Install PM2:
npm install pm2 -g
Start an application:
pm2 start app.js
Common commands:
# List all applications
pm2 list
# Monitor resource usage
pm2 monit
# View logs
pm2 logs
# Restart application
pm2 restart app_name
# Stop application
pm2 stop app_name
# Delete application
pm2 delete app_name
Example PM2 configuration file ecosystem.config.js
:
module.exports = {
apps: [{
name: 'app',
script: './app.js',
instances: 'max',
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}]
};
Forever Tool
Forever is another simple Node.js process management tool suitable for small applications that don't require complex features:
Installation:
npm install forever -g
Basic usage:
# Start application
forever start app.js
# Stop application
forever stop app.js
# List all running applications
forever list
# Watch for file changes and auto-restart
forever -w app.js
Advanced Process Management Features
Load Balancing
Using the cluster module or PM2 makes load balancing easy. PM2 automatically allocates processes based on CPU cores:
pm2 start app.js -i max
Zero-Downtime Restart
PM2 supports the reload command for zero-downtime restarts:
pm2 reload app_name
Log Management
PM2 provides centralized log management:
# Standard output and error logs
pm2 logs
# Log file rotation
pm2 install pm2-logrotate
Monitoring and Alerts
PM2 can integrate with Keymetrics.io for real-time monitoring:
pm2 link <secret> <public>
Inter-Process Communication
Master and worker processes can communicate via IPC:
// Master process
cluster.on('message', (worker, message) => {
console.log(`Received message from worker ${worker.id}: ${message}`);
});
// Worker process
process.send({ hello: 'Message from worker' });
Error Handling and Process Recovery
Error handling in PM2 configuration:
module.exports = {
apps: [{
// ...
error_file: '/var/log/node-app/err.log',
out_file: '/var/log/node-app/out.log',
merge_logs: true,
min_uptime: '60s',
max_restarts: 10
}]
};
Process Management in Container Environments
Best practices for using PM2 in Docker containers:
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
# Install PM2
RUN npm install pm2 -g
# Start application
CMD ["pm2-runtime", "ecosystem.config.js"]
Performance Optimization Tips
- Set an appropriate number of processes based on CPU cores:
const numCPUs = require('os').cpus().length;
const workers = Math.min(numCPUs, 4); // No more than 4 processes
- Memory limits:
pm2 start app.js --max-memory-restart 500M
- Use cluster mode for exec_mode:
module.exports = {
apps: [{
exec_mode: 'cluster',
instances: 'max'
}]
};
Security Considerations
- Do not run Node.js processes as root user
- Limit process permissions:
pm2 start app.js --uid www-data --gid www-data
- Regularly update PM2 versions
- Secure PM2's HTTP interface (if enabled)
Debugging Process Management Issues
PM2 debugging commands:
# Show detailed logs
pm2 logs --lines 200
# Show process details
pm2 show app_name
# Check PM2 status
pm2 ping
# Generate diagnostic report
pm2 report
Custom Process Management Solutions
For special requirements, you can build custom solutions based on the child_process module:
const { fork } = require('child_process');
const path = require('path');
class ProcessManager {
constructor() {
this.workers = [];
this.maxWorkers = 4;
}
start() {
for (let i = 0; i < this.maxWorkers; i++) {
const worker = fork(path.join(__dirname, 'worker.js'));
this.workers.push(worker);
worker.on('message', (msg) => {
console.log(`Worker ${worker.pid}: ${msg}`);
});
worker.on('exit', (code) => {
console.log(`Worker ${worker.pid} exited with code ${code}`);
this.replaceWorker(worker);
});
}
}
replaceWorker(deadWorker) {
const index = this.workers.indexOf(deadWorker);
if (index !== -1) {
const newWorker = fork(path.join(__dirname, 'worker.js'));
this.workers[index] = newWorker;
console.log(`Replaced worker ${deadWorker.pid} with ${newWorker.pid}`);
}
}
}
const manager = new ProcessManager();
manager.start();
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn