2016-03-25 5 views
4

私のsocket.ioサーバーはバックグラウンドで実行されています。しかし、死亡した状況はいくつかあります。死んだり終了したりするたびにsocket.ioサーバーを自動的に再起動させるにはどうすればよいですか?自動再始動または適切な処理socket.ioサーバーはLinuxの例外です。

これは、サーバーが例外をスローし、死亡したしかし、私は私のノードサーバ

> node server & 

を開始する方法です。

events.js:141 
     throw er; // Unhandled 'error' event 
    ^

Error: Redis connection to pub-redis-us-east1-garantiabata.com:12087 failed - connect ETIMEDOUT 54.179.28.165:12087 
    at RedisClient.on_error (/home/org-chatserver/node_modules/redis/index.js:185:24) 
    at Socket.<anonymous> (/home/org-chatserver/node_modules/redis/index.js:95:14) 
    at emitOne (events.js:77:13) 
    at Socket.emit (events.js:169:7) 
    at emitErrorNT (net.js:1253:8) 
    at doNTCallback2 (node.js:450:9) 
    at process._tickCallback (node.js:364:17) 
^C 

これは私の環境

Linux version 2.6.32-042stab090.5 ([email protected]) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)) #1 SMP Sat Jun 21 00:15:09 MSK 2014 

であり、私は私のチームはこれを使用していると信じて:

https://github.com/jbavari/ionic-socket.io-redis-chat 
+0

使用しているnodeとsocket.ioのバージョンはどれですか? –

+0

は、基本的に私はこのhttps://github.com/jbavari/ionic-socket.io-redis-chat/blob/master/server.jsを出しています。ノード-vはそのv5.0.0を言う – Axil

+0

私はこのhttps://gist.githubをしようとしました。com/survivaltravel/f29c38e055d0785c56ff https://github.com/NodeRedis/node_redis#usageに基づいて、私は今日もそれは死んだ、確信していない今日は一日それを実行知っている。コンソールの代わりにログを保存する方法を見つける必要があります – Axil

答えて

0

あなたは、たとえばなどのログ、自動再起動を監視するために... pm2を使用することができます。

sudo npm install -g pm2 
pm2 start app.js --watch 

もっとwatch & restartについて。

+0

のドキュメントも読んでください。専用のコンソールを持っていたいので、バックグラウンドで実行できますか?私は何かしたいですが、このノードサーバーは、シャットダウンする可能性がありますが、継続してイベントを実行しています – Axil

+0

はい、 'pm2 start'はプロセスをバックグラウンドで起動します。' pm2 list'、 'pm2 monit'、' pm2 logs' 。リモートで監視する場合は、[ダッシュボードサービス](http://pm2.keymetrics.io/docs/usage/monitoring/)を使用する必要があります。 –

+0

これは今のところ好きです。それは私が思うに、これまでのところ12時間テストされています。私はこれがそれだと思う。しかし、一つは、私はpm2リスト、pm2 monitのようなすべてのコマンドをした...私はテーブルのリストで私のプロセスを見つけることはできません? – Axil

1

あなたがスーパーバイザーを使用することができます - インストール後http://supervisord.org

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

を、作成するあなたのアプリのスーパーバイザ設定ファイル。 -

[program:my-server] 
    command=node /path/to/server.js 
    numprocs=1 
    autostart=true 
    autorestart=true 
    startretries=99999 
    environment=NODE_ENV=production 
    stderr_logfile=/var/log/my-server.err.log 
    stdout_logfile=/var/log/my-server.log 
    user=myuser 

編集スーパーバイザメイン設定ファイル - /etc/supervisor/supervisord.conf

[unix_http_server] 
file=/var/run/supervisor.sock ; (the path to the socket file) 
chmod=0700      ; socket file mode (default 0700) 

[supervisord] 
logfile=/var/log/supervisor/supervisord.log ; (main log file;default ] $CWD/supervisord.log) 
pidfile=/var/run/supervisord.pid   ; (supervisord pidfile;default supervisord.pid) 
childlogdir=/var/log/supervisor    ; ('AUTO' child log dir, default $TEMP) 

; the below section must remain in the config file for RPC 
; (supervisorctl/web interface) to work, additional interfaces may be 
; added by defining them in separate rpcinterface: sections 
[rpcinterface:supervisor] 
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 

[supervisorctl] 
serverurl=unix:///var/run//supervisor.sock ; use a unix:// URL for a unix socket 

; The [include] section can just contain the "files" setting. This 
; setting can list multiple files (separated by whitespace or 
; newlines). It can also contain wildcards. The filenames are 
; interpreted as relative to this file. Included files *cannot* 
; include files themselves. 

[include] 
files = /etc/supervisor/conf.d/*.conf 

使用/etc/supervisor/conf.d/myserver.conf下たとえば

、: /etc/init.d/supervisord {start|stop|restart|force-reload|status|force-stop}

の詳細/ドキュメントについて - http://supervisord.org/

をお読みください
+0

「スーパーバイザ設定ファイル」の作成方法がわかりません。私はecho_supervisord_conf https://gist.github.com/anonymous/eb524d1931dd621f051bをしようとするときにこれを取得します。私はこのサンプルを見つけました、これを使用できますか? https://gist.github.com/didip/802561。また、私はスーパーバイザーを始めますか? – Axil

+0

ubuntu-12.04-x86で動作しています – Axil

+0

さらに多くの例が追加されました。 - http://supervisord.org/ –

-1

サーバーを接続するには、サーバーの状態をチェックして終了時にサーバーを再起動するプログラム(永遠など)を使用する必要があります。しかし、これはクライアントの問題を解決しません。また、クライアントは、サーバーが切断されたときにサーバーへの接続を再初期化する必要があります。クライアントをサーバーに接続する方法の最小例を紹介しましょう。

主な考え方は、クライアントソケットから "close"イベントをリッスンし、必要に応じて接続を再開することです。これをよりよく説明するためにOOPを使用しましょう。コネクタオブジェクトで

function Connector(host, port) 
{ 
    // Connection parameters 
    this.host = host; 
    this.port = port; 

    this.socket = null; 

    // Attempt to establish a connection with the server. 
    this._connect(); 
} 

Connector.prototype._connect = function() 
{ 
    this.socket = net.connect(this.port, this.host); 

    this.socket.on('connect', this._onConnect); 
    this.socket.on('error', this._onError); 
    this.socket.on('close', this._onClose); 
    this.socket.on('data', this._onData); 
} 


Connector.prototype._onConnect = function() 
{ 
    console.log('Incoming connection'); 
} 

Connector.prototype._onError = function(err) 
{ 
    console.error('Connection error: ' + err); 
} 

Connector.prototype._onClose = function(had_error) 
{ 
    console.log('Reconnecting to server.'); 
    self._scheduleReconnection(); 
} 


Connector.prototype._scheduleReconnection = function() 
{ 
    self.socket.removeAllListeners(); 
    self.socket = null; 

    // Try to connect to the server again after 2 seconds. 
    setTimeout(self._connect.bind(self), 2000); 
} 

Connector.prototype._onData = function(data) 
{ 
    console.log('Incoming data.') 
} 

あなたは今との接続を開始することができます

connection = new Connector(127.0.0.1, 8080) 

を、あなたは、この接続は、常にサーバーとの接続を維持しようとします知っています。

関連する問題