1 |
|
2 | 'use strict';
|
3 |
|
4 | var commander = require('commander');
|
5 |
|
6 | var PM2 = require('../..');
|
7 | var Log = require('../../lib/API/Log');
|
8 | var cst = require('../../constants.js');
|
9 | var pkg = require('../../package.json');
|
10 | var path = require('path');
|
11 |
|
12 | var pm2;
|
13 |
|
14 |
|
15 | process.env.PM2_DISCRETE_MODE = true;
|
16 |
|
17 | commander.version(pkg.version)
|
18 | .description('pm2-runtime is an automatic pmx injection that runs in simulated no-daemon environment')
|
19 | .option('--auto-manage', 'keep application online after command exit')
|
20 | .option('--fast-boot', 'boot app faster by keeping pm2 runtime online in background (effective at second exit/start)')
|
21 | .option('--web [port]', 'launch process web api on [port] default to 9615')
|
22 | .option('--secret [key]', 'PM2 plus secret key')
|
23 | .option('--public [key]', 'PM2 plus public key')
|
24 | .option('--machine-name [name]', 'PM2 plus machine name')
|
25 | .option('--env [name]', 'select env_[name] env variables in process config file')
|
26 | .option('--watch', 'Watch and Restart')
|
27 | .option('-i --instances <number>', 'launch [number] instances with load-balancer')
|
28 | .usage('pm2-runtime app.js');
|
29 |
|
30 | commander.command('*')
|
31 | .action(function(cmd){
|
32 | pm2 = new PM2.custom({
|
33 | pm2_home : path.join(process.env.HOME, '.pm3'),
|
34 | secret_key : cst.SECRET_KEY || commander.secret,
|
35 | public_key : cst.PUBLIC_KEY || commander.public,
|
36 | machine_name : cst.MACHINE_NAME || commander.machineName
|
37 | });
|
38 |
|
39 | pm2.connect(function() {
|
40 | if (commander.web) {
|
41 | var port = commander.web === true ? cst.WEB_PORT : commander.web;
|
42 | pm2.web(port);
|
43 | }
|
44 |
|
45 | pm2.start(cmd, commander, function(err, obj) {
|
46 | if (process.env.PM2_RUNTIME_DEBUG) {
|
47 | return pm2.disconnect(function() {});
|
48 | }
|
49 |
|
50 | if (err) {
|
51 | console.error(err);
|
52 | return process.exit(1);
|
53 | }
|
54 |
|
55 | var pm_id = obj[0].pm2_env.pm_id;
|
56 |
|
57 | if (commander.instances == undefined) {
|
58 | return pm2.attach(pm_id, function() {
|
59 | exitPM2();
|
60 | });
|
61 | }
|
62 |
|
63 | if (commander.json === true)
|
64 | Log.jsonStream(pm2.Client, pm_id);
|
65 | else if (commander.format === true)
|
66 | Log.formatStream(pm2.Client, pm_id, false, 'YYYY-MM-DD-HH:mm:ssZZ');
|
67 | else
|
68 | Log.stream(pm2.Client, 'all', true);
|
69 | });
|
70 | });
|
71 | });
|
72 |
|
73 | if (process.argv.length == 2) {
|
74 | commander.outputHelp();
|
75 | process.exit(1);
|
76 | }
|
77 |
|
78 | process.on('SIGINT', function() {
|
79 | exitPM2();
|
80 | });
|
81 |
|
82 | process.on('SIGTERM', function() {
|
83 | exitPM2();
|
84 | });
|
85 |
|
86 | commander.parse(process.argv);
|
87 |
|
88 | function exitPM2() {
|
89 | console.log('Exited at %s', new Date());
|
90 | if (commander.autoManage)
|
91 | return process.exit(0);
|
92 |
|
93 | if (commander.fastBoot) {
|
94 | return pm2.delete('all', function() {
|
95 | process.exit(0);
|
96 | });
|
97 | }
|
98 | pm2.kill(function() {
|
99 | process.exit(0);
|
100 | });
|
101 | }
|