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基本工作原理的更多相关文章

  1. [转自SA]浅谈nginx的工作原理和使用

    nginx apache 简单对比 nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而 apac ...

  2. 浅谈JSONP 的工作原理

    小编最近在工作中经常用到 jsonp 这个东西, 表示之前从来没用过  最近稍微研究了下 当然很多内容来源于网上 收集整理 你懂的 ~~~ 话说我们访问一个页面的时候 需要像另一个网站获取部分信息, ...

  3. 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...

  4. 浅谈springboot自动配置原理

    前言 springboot自动配置关键在于@SpringBootApplication注解,启动类之所以作为项目启动的入口,也是因为该注解,下面浅谈下这个注解的作用和实现原理 @SpringBootA ...

  5. 浅谈JavaScript DDOS 攻击原理与防御

    前言 DDoS(又名"分布式拒绝服务")攻击历史由来已久,但却被黑客广泛应用.我们可以这样定义典型的DDoS攻击:攻击者指使大量主机向服务器发送数据,直到超出处理能力进而无暇处理正 ...

  6. 浅谈 session 会话的原理

    先谈 cookie 网络传输基于的Http协议,是无状态的协议,即每次连接断开后再去连接,服务器是无法判断此次连接的客户端是谁. 如果每次数据传输都需要进行连接和断开,那造成的开销是很巨大的. 为了解 ...

  7. 浅谈Nginx负载均衡原理与实现

    1.Nginx能做什么? Nginx可以两件事: -- HTTP请求  经过官方测试Nginx可以承受5万的并发量.可用来做静态资源的图片服务器 --负载均衡,如下解释什么是负载均衡. 2.负载均衡 ...

  8. 浅谈JavaScript预编译原理

    这两天又把js的基础重新复习了一下,很多不懂得还是得回归基础,大家都知道js是解释性语言,就是编译一行执行一行,但是在执行的之前,系统会做一些工作: 1,语法分析: 2,预编译: 3,解释执行. 语法 ...

  9. 浅谈HashMap 的底层原理

    本文整理自漫画:什么是HashMap? -小灰的文章 .已获得作者授权. HashMap 是一个用于存储Key-Value 键值对的集合,每一个键值对也叫做Entry.这些个Entry 分散存储在一个 ...

随机推荐

  1. 常见css水平自适应布局

    左右布局,左边固定,右边自适应布局 BFC方法解决 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  2. pyqt的信号槽机制(转)

    PySide/PyQt Tutorial: Creating Your Own Signals and Slots This article is part 5 of 8 in the series  ...

  3. encodeURIComponent()中文乱码

    可能是Tomcat的编码格式问题   应该在8080端口下设置 <Connector port="8080" protocol="HTTP/1.1" co ...

  4. wpf 仿QQ图片查看器

    参考博客 WPF下的仿QQ图片查看器 wpf图片查看器,支持鼠标滚动缩放拖拽 实现效果 主要参考的WPF下的仿QQ图片查看器,原博主只给出了部分代码. 没有完成的部分 1.右下角缩略图是原图不是缩略图 ...

  5. GridView中实现DropDownList联动

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Python2 新手 编码问题 吐血总结

    什么是编码 任何一种语言.文字.符号等等,计算都是将其以一种类似字典的形式存起来的,比如最早的计算机系统将英文文字转为数字存储(ASCII码),这种文字与数字(或其他)一一对应的关系我们称之为编码.由 ...

  7. Linux内核笔记--内存管理之用户态进程内存分配

    内核版本:linux-2.6.11 Linux在加载一个可执行程序的时候做了种种复杂的工作,内存分配是其中非常重要的一环,作为一个linux程序员必然会想要知道这个过程到底是怎么样的,内核源码会告诉你 ...

  8. CentOS 6.5 Nginx 配置

    1.安装所有 http功能: ./configure --user=www-data --group=www-data --with-http_ssl_module --with-http_reali ...

  9. 【生活没有希望】poj1273网络流大水题

    你不能把数据规模改大点吗= =我优化都不加都过了 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M; ],l[],f ...

  10. 弱省互测#1 t3

    题意 给出一棵n个点的树,求包含1号点的第k小的连通块权值和.(\(n<=10^5\)) 分析 k小一般考虑堆... 题解 堆中关键字为\(s(x)+min(a)\),其中\(s(x)\)表示\ ...