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. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    java日期格式大全 format SimpleDateFormat(转) SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH ...

  2. ListFragment源码 (待分析)

    /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  3. 关于UnsupportedClassVersionError的错误处理

    错误:Java.lang.UnsupportedClassVersionError: Bad version number in .class file 造成这种错误的原因是你的支持Tomcat运行的 ...

  4. HDU3948 & 回文树模板

    Description: 求本质不同回文子串的个数 Solution: 回文树模板,学一学贴一贴啊... Code: /*================================= # Cre ...

  5. Nonblocking I/O and select()

    This sample program illustrates a server application that uses nonblocking and the select() API. Soc ...

  6. tomcat -ROOT 与webapps 的关系,关于部署的一些问题

    现象:之前遇到很奇怪的问题,发完版之后没有效果,页面还是读取上一版的. 反复查找原因发现  http://localhost:8080/mobie 这个路径下的页面是正常的, 而  http://lo ...

  7. echarts3 -arcgis echarts.js修改

     在echarts.js中修改修改 clone 方法    其中 source instance of Array 修改为Object.prototype.toString.call(source)  ...

  8. 安装windows服务批处理代码

    批处理是DOS时代比较常用的方法之一,目前来说也是一种高效的方法,复制代码到文本文件中,保存并修改文件扩展名为“*.bat”. 安装windows服务批处理代码如下: @echo off set fi ...

  9. Python之路Day14--html

    本节内容: 一.HTML 二.CSS 三.JS HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据 ...

  10. solr查询语句示例

    url示例:sort=id+desc&&fq=date_time:[20081001 TO 20091031]&wt=json&json.nl=map&q=st ...