TODO:浅谈pm2基本工作原理
TODO:浅谈pm2基本工作原理
要谈Node.js pm2的工作原理,需要先来了解撒旦(Satan)和上帝(God)的关系。
撒旦(Satan),主要指《圣经》中的堕天使(也称堕天使撒旦),他是反叛上帝耶和华的堕天使(Fallen Angels),曾经是上帝座前的天使,后来他因骄傲自大妄想与神同等而堕落成为魔鬼,被看作与上帝的力量相对的邪恶、黑暗之源。
简单的说Satan是破坏神,就是进程的异常退出、kill等;God是守护神,保护进程、重启进程等。

一图胜千言,pm2的 RPC基本框架。Client与Daemon是采用了RPC进行通讯。
RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。
1. Client启动关联Daemon
daemon.innerStart(function() {
KMDaemon.launchAndInteract(that.conf, {
machine_name : that.machine_name,
public_key : that.public_key,
secret_key : that.secret_key
}, function(err, data, interactor_proc) {
that.interactor_process = interactor_proc;
});
that.launchRPC(function(err, meta) {
return cb(null, {
daemon_mode : that.conf.daemon_mode,
new_pm2_instance : false,
rpc_socket_file : that.rpc_socket_file,
pub_socket_file : that.pub_socket_file,
pm2_home : that.pm2_home
});
});
});
child.once(‘message’, function(msg) {
debug(‘PM2 daemon launched with return message: ‘, msg);
child.removeListener(‘error’, onError);
child.disconnect();
if (opts && opts.interactor == false)
return cb(null, child);
/**
* Here the Keymetrics agent is launched automaticcaly if
* it has been already configured before (via pm2 link)
*/
KMDaemon.launchAndInteract(that.conf, {
machine_name : that.machine_name,
public_key : that.public_key,
secret_key : that.secret_key
}, function(err, data, interactor_proc) {
that.interactor_process = interactor_proc;
return cb(null, child);
});
});
2. Daemon有start,stop,close,kill进程的方法,与God的事件发射器(EventEmitter)关联
God.bus.on(‘axm:monitor’, function axmMonitor(msg) {
if (!msg.process)
return console.error(‘[axm:monitor] no process defined’);
if (!msg.process || !God.clusters_db[msg.process.pm_id])
return console.error(‘Unknown id %s’, msg.process.pm_id);
util._extend(God.clusters_db[msg.process.pm_id].pm2_env.axm_monitor, Utility.clone(msg.data));
msg = null;
});
3. Satan.js
3.1. Ping进程是否活着或者关闭
Satan.pingDaemon = function pingDaemon(cb) {
var req = axon.socket(‘req’);
var client = new rpc.Client(req);
debug(‘[PING PM2] Trying to connect to server’);
client.sock.once(‘reconnect attempt’, function() {
client.sock.close();
debug(‘Daemon not launched’);
process.nextTick(function() {
return cb(false);
});
});
client.sock.once(‘connect’, function() {
client.sock.once(‘close’, function() {
return cb(true);
});
client.sock.close();
debug(‘Daemon alive’);
});
req.connect(cst.DAEMON_RPC_PORT);
};
3.2. 通知God
Satan.notifyGod = function(action_name, id, cb) {
Satan.executeRemote(‘notifyByProcessId’, {
id : id,
action_name : action_name,
manually : true
}, function() {
debug(‘God notified’);
return cb ? cb() : false;
});
};
4. God是事件监听
var God = module.exports = {
next_id : 0,
clusters_db : {},
bus : new EventEmitter2({
wildcard: true,
delimiter: ‘:’,
maxListeners: 1000
})
};
5. God的监听进程方法有
God.ping
God.notifyKillPM2
God.duplicateProcessId
God.startProcessId
God.stopProcessId
God.resetMetaProcessId
God.deleteProcessId
God.restartProcessId
God.restartProcessName
God.sendSignalToProcessId
God.sendSignalToProcessName
God.stopWatch
God.toggleWatch
God.startWatch
God.reloadLogs
God.sendDataToProcessId
God.msgProcess
God.getVersion
6. God的进程运行模式用两种
6.1. Cluster集群模式
6.2. Fork模式
一般开发环境用fork模式,生产环境使用cluster模式

简单pm2进程管理描述,更多方法请阅读源码。
wxgzh:ludong86

TODO:浅谈pm2基本工作原理的更多相关文章
- [转自SA]浅谈nginx的工作原理和使用
nginx apache 简单对比 nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而 apac ...
- 浅谈JSONP 的工作原理
小编最近在工作中经常用到 jsonp 这个东西, 表示之前从来没用过 最近稍微研究了下 当然很多内容来源于网上 收集整理 你懂的 ~~~ 话说我们访问一个页面的时候 需要像另一个网站获取部分信息, ...
- 浅谈SpringBoot核心注解原理
SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...
- 浅谈springboot自动配置原理
前言 springboot自动配置关键在于@SpringBootApplication注解,启动类之所以作为项目启动的入口,也是因为该注解,下面浅谈下这个注解的作用和实现原理 @SpringBootA ...
- 浅谈JavaScript DDOS 攻击原理与防御
前言 DDoS(又名"分布式拒绝服务")攻击历史由来已久,但却被黑客广泛应用.我们可以这样定义典型的DDoS攻击:攻击者指使大量主机向服务器发送数据,直到超出处理能力进而无暇处理正 ...
- 浅谈 session 会话的原理
先谈 cookie 网络传输基于的Http协议,是无状态的协议,即每次连接断开后再去连接,服务器是无法判断此次连接的客户端是谁. 如果每次数据传输都需要进行连接和断开,那造成的开销是很巨大的. 为了解 ...
- 浅谈Nginx负载均衡原理与实现
1.Nginx能做什么? Nginx可以两件事: -- HTTP请求 经过官方测试Nginx可以承受5万的并发量.可用来做静态资源的图片服务器 --负载均衡,如下解释什么是负载均衡. 2.负载均衡 ...
- 浅谈JavaScript预编译原理
这两天又把js的基础重新复习了一下,很多不懂得还是得回归基础,大家都知道js是解释性语言,就是编译一行执行一行,但是在执行的之前,系统会做一些工作: 1,语法分析: 2,预编译: 3,解释执行. 语法 ...
- 浅谈HashMap 的底层原理
本文整理自漫画:什么是HashMap? -小灰的文章 .已获得作者授权. HashMap 是一个用于存储Key-Value 键值对的集合,每一个键值对也叫做Entry.这些个Entry 分散存储在一个 ...
随机推荐
- echarts之tooltip-showContent
当trigger:为'axis'时 tooltip : { trigger: 'axis', showContent:false } 当trigger:为'item'时 tooltip : { tri ...
- Mysql数据库的使用总结之Innodb简介
最近在对开发的软件的服务器部分制作安装包,但服务器部分需要有mysql数据库的支持.因此,采用免安装版的mysql策略:将mysql数据库需要的文件在安装程序中进行设置和打包即可.但也遇到了很多问题 ...
- iOS CoreData primitive accessor
Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...
- 【转】OBJECT_ID和DATA_OBJECT_ID的区别
在user_objects等视图里面有两个比较容易搞混的字段object_id和data_object_id这两个字段基本上有什么大的区别呢?object_id其实是对每个数据库中数据对象的唯一标识d ...
- Mybatis总结
jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/testdb username=root pa ...
- 德国W家HIPP 奶粉有货播报:2014.6.25 HIPP 1+ 4盒装有货啦!
德国W家HIPP 奶粉有货播报:2014.6.25 HIPP 1+ 4盒装有货啦!
- Day 2:增加SplashScreen
If you want to add just single image, then create a pic in the size of 480*800 and name it as Splash ...
- 使用GDB 追踪依赖poco的so程序,core dump文件分析.
前言 在windows 下 系统核心态程序蓝屏,会产生dump文件. 用户级程序在设置后,程序崩溃也会产生dump文件.以方便开发者用windbg进行分析. so,linux 系统也有一套这样的东东- ...
- ST
这次说一下测试的基础部分 软件测试 软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.换句话说,软件测试是一种实际输出与预期输出间的审核 ...
- ubuntu vps 安装 jdk
Introduction Java is a programming technology originally developed by Sun Microsystems and later acq ...