哪里永远存放监控代码?

时间:2014-11-06 15:14:49

标签: node.js forever

我正在尝试永远设置监视器。

我将此添加到我的app.js:

var forever = require('forever-monitor');

var child = new(forever.Monitor)('app.js', {
    max: 3,
    silent: true,
    options: []
});

child.on('exit', function() {
    console.log('app.js has exited after 3 restarts');
});

child.start();

然而,当我从命令行启动我的应用程序时,它会记录' app.js已经在3次启动后退出'但它仍然运行。该代码应放在哪个文件中?我错过了关于forever-monitor使用的一些内容吗?

2 个答案:

答案 0 :(得分:6)

以下是永远监视器的工作原理

<强> app_fm.js

var forever = require('forever-monitor');

var child = new(forever.Monitor)('app.js', {
    max: 3,
    silent: true,
    options: []
});

child.on('exit', function() {
    console.log('app.js has exited after 3 restarts');
});

child.start();


app.js

// put in all your great nodejs app code
console.log('node app is now running');


现在从CLI通过键入启动您的应用程序
节点app_fm

答案 1 :(得分:0)

老实说,我只使用forever而不是forever-monitor(虽然我知道它在永远的文档中谈论它!)。我创建了一个名为start.js的文件,并使用node start.js运行我的应用。

'use strict';
var forever = require('forever');
var child = new (forever.Monitor )('app.js', {
  //options : options
} );

//These events not required, but I like to hear about it.
child.on( "exit", function() {
  console.log( 'app.js has exited!' );
} );
child.on( "restart", function() {
  console.log( 'app.js has restarted.' );
} );
child.on( 'watch:restart', function( info ) {
  console.error( 'Restarting script because ' + info.file + ' changed' );
} );

//These lines actually kicks things off
child.start();
forever.startServer( child );

//You can catch other signals too
process.on( 'SIGINT', function() {
  console.log( "\nGracefully shutting down \'node forever\' from SIGINT (Ctrl-C)" );
  // some other closing procedures go here
  process.exit();
} );

process.on( 'exit', function() {
  console.log( 'About to exit \'node forever\' process.' );
} );

//Sometimes it helps...
process.on( 'uncaughtException', function( err ) {
  console.log( 'Caught exception in \'node forever\': ' + err );
} );