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

qrcode_for_gh_6bb1f39ae99c_258

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
05-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值