TODO: Talking about the basic working principle of PM2

Source: Internet
Author: User

TODO: Talking about the basic working principle of PM2

To talk about how node. js PM2 Works, you need to first understand the relationship between Satan (Satan) and God.

Satan (Satan), which mainly refers to the fallen angels in the Bible (also known as The Fallen Angels Satan), is the Fallen Angel (fallen Angels) of the Lord who rebelled against God, who was once the angel of God, who later fell into the devil as a result of his arrogant delusion and God, and was seen as a relative evil to the power of God. The source of darkness.

Simply speaking, Satan is the destruction of God, that is, the process of abnormal exit, kill, etc., God is the Guardian God, the protection process, restart process and so on.

A picture wins thousands of words, PM2 RPC basic framework. Client and daemon are using RPC for communication.

RPC (remote Procedure call Protocol)-a remoting protocol that requests services from a remote computer program over a network without needing to know the underlying network technology. The RPC protocol assumes that some transport protocols exist, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport and application tiers. RPC makes it easier to develop applications that include distributed, multi-program networks.

1. Client Initiates Association 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 with Start,stop,close,kill process, associated with God's event launcher (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. Whether the ping process is alive or closed

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. Inform 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 is an event listener

var God = Module.exports = {

next_id:0,

clusters_db: {},

Bus:new EventEmitter2 ({

Wildcard:true,

Delimiter: ': ',

maxlisteners:1000

})

};

5. God's listening Process method has

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's process-running model uses two types of

6.1. Cluster cluster mode

6.2. Fork Mode

General development environment with fork mode, production environment using cluster mode

Simple PM2 Process Management description, more methods please read the source code.

Wxgzh:ludong86

TODO: Talking about the basic working principle of PM2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.