UNPKG

6.33 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Specialized PM2 CLI for Containers
5 */
6var commander = require('commander');
7var PM2 = require('../..');
8var Log = require('../../lib/API/Log');
9var cst = require('../../constants.js');
10var pkg = require('../../package.json');
11var path = require('path');
12var DEFAULT_FAIL_COUNT = 3;
13
14process.env.PM2_DISCRETE_MODE = true;
15
16commander.version(pkg.version)
17 .description('pm2-runtime is a drop-in replacement Node.js binary for containers')
18 .option('-i --instances <number>', 'launch [number] of processes automatically load-balanced. Increase overall performances and performance stability.')
19 .option('--secret [key]', '[MONITORING] PM2 plus secret key')
20 .option('--no-autorestart', 'start an app without automatic restart')
21 .option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
22 .option('-n --name <name>', 'set a <name> for script')
23 .option('--max-memory-restart <memory>', 'specify max memory amount used to autorestart (in octet or use syntax like 100M)')
24 .option('-c --cron <cron_pattern>', 'restart a running process based on a cron pattern')
25 .option('--interpreter <interpreter>', 'the interpreter pm2 should use for executing app (bash, python...)')
26 .option('--public [key]', '[MONITORING] PM2 plus public key')
27 .option('--machine-name [name]', '[MONITORING] PM2 plus machine name')
28 .option('--trace', 'enable transaction tracing with km')
29 .option('--v8', 'enable v8 data collecting')
30 .option('--format', 'output logs formated like key=val')
31 .option('--raw', 'raw output (default mode)')
32 .option('--formatted', 'formatted log output |id|app|log')
33 .option('--json', 'output logs in json format')
34 .option('--delay <seconds>', 'delay start of configuration file by <seconds>', 0)
35 .option('--web [port]', 'launch process web api on [port] (default to 9615)')
36 .option('--only <application-name>', 'only act on one application of configuration')
37 .option('--no-auto-exit', 'do not exit if all processes are errored/stopped or 0 apps launched')
38 .option('--env [name]', 'inject env_[name] env variables in process config file')
39 .option('--watch', 'watch and restart application on file change')
40 .option('--error <path>', 'error log file destination (default disabled)', '/dev/null')
41 .option('--output <path>', 'output log file destination (default disabled)', '/dev/null')
42 .option('--deep-monitoring', 'enable all monitoring tools (equivalent to --v8 --event-loop-inspector --trace)')
43 .allowUnknownOption()
44 .usage('app.js');
45
46commander.command('*')
47 .action(function(cmd){
48 Runtime.instanciate(cmd);
49 });
50
51commander.command('start <app.js|json_file>')
52 .description('start an application or json ecosystem file')
53 .action(function(cmd) {
54 Runtime.instanciate(cmd);
55 });
56
57if (process.argv.length == 2) {
58 commander.outputHelp();
59 process.exit(1);
60}
61
62var Runtime = {
63 pm2 : null,
64 instanciate : function(cmd) {
65 this.pm2 = new PM2.custom({
66 pm2_home : process.env.PM2_HOME || path.join(process.env.HOME, '.pm2'),
67 secret_key : cst.SECRET_KEY || commander.secret,
68 public_key : cst.PUBLIC_KEY || commander.public,
69 machine_name : cst.MACHINE_NAME || commander.machineName,
70 daemon_mode : process.env.PM2_RUNTIME_DEBUG || false
71 });
72
73 this.pm2.connect(function(err, pm2_meta) {
74 process.on('SIGINT', function() {
75 Runtime.exit();
76 });
77
78 process.on('SIGTERM', function() {
79 Runtime.exit();
80 });
81
82 Runtime.startLogStreaming();
83 Runtime.startApp(cmd, function(err) {
84 if (err) {
85 console.error(err.message || err);
86 return Runtime.exit();
87 }
88 });
89 });
90 },
91
92 /**
93 * Log Streaming Management
94 */
95 startLogStreaming : function() {
96 if (commander.json === true)
97 Log.jsonStream(this.pm2.Client, 'all');
98 else if (commander.format === true)
99 Log.formatStream(this.pm2.Client, 'all', false, 'YYYY-MM-DD-HH:mm:ssZZ');
100 else
101 Log.stream(this.pm2.Client, 'all', !commander.formatted, commander.timestamp, true);
102 },
103
104 /**
105 * Application Startup
106 */
107 startApp : function(cmd, cb) {
108 function exec() {
109 this.pm2.start(cmd, commander, function(err, obj) {
110 if (err)
111 return cb(err);
112 if (obj && obj.length == 0)
113 return cb(new Error(`0 application started (no apps to run on ${cmd})`))
114
115 if (commander.web) {
116 var port = commander.web === true ? cst.WEB_PORT : commander.web;
117 Runtime.pm2.web(port);
118 }
119
120 if (commander.autoExit) {
121 setTimeout(function() {
122 Runtime.autoExitWorker();
123 }, 4000);
124 }
125
126 // For Testing purpose (allow to auto exit CLI)
127 if (process.env.PM2_RUNTIME_DEBUG)
128 Runtime.pm2.disconnect(function() {});
129
130 return cb(null, obj);
131 });
132 }
133 // via --delay <seconds> option
134 setTimeout(exec.bind(this), commander.delay * 1000);
135 },
136
137 /**
138 * Exit runtime mgmt
139 */
140 exit : function(code) {
141 if (!this.pm2) return process.exit(1);
142
143 this.pm2.kill(function() {
144 process.exit(code || 0);
145 });
146 },
147
148 /**
149 * Exit current PM2 instance if 0 app is online
150 * function activated via --auto-exit
151 */
152 autoExitWorker : function(fail_count) {
153 var interval = 2000;
154
155 if (typeof(fail_count) =='undefined')
156 fail_count = DEFAULT_FAIL_COUNT;
157
158 var timer = setTimeout(function () {
159 Runtime.pm2.list(function (err, apps) {
160 if (err) {
161 console.error('Could not run pm2 list');
162 return Runtime.autoExitWorker();
163 }
164
165 var appOnline = 0;
166
167 apps.forEach(function (app) {
168 if (!app.pm2_env.pmx_module &&
169 (app.pm2_env.status === cst.ONLINE_STATUS ||
170 app.pm2_env.status === cst.LAUNCHING_STATUS)) {
171 appOnline++;
172 }
173 });
174
175 if (appOnline === 0) {
176 console.log('0 application online, retry =', fail_count);
177 if (fail_count <= 0)
178 return Runtime.exit(2);
179 return Runtime.autoExitWorker(--fail_count);
180 }
181
182 Runtime.autoExitWorker();
183 });
184 }, interval);
185
186 timer.unref();
187 }
188}
189
190commander.parse(process.argv);