{"action":"created","issue":{"url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/8","repository_url":"https://api.github.com/repos/benbria/node-amqp-connection-manager","labels_url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/8/comments","events_url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/8/events","html_url":"https://github.com/benbria/node-amqp-connection-manager/issues/8","id":134174435,"number":8,"title":"Reconnect missing when broker closes connection gracefully","user":{"login":"mxscho","id":8847270,"avatar_url":"https://avatars.githubusercontent.com/u/8847270?v=3","gravatar_id":"","url":"https://api.github.com/users/mxscho","html_url":"https://github.com/mxscho","followers_url":"https://api.github.com/users/mxscho/followers","following_url":"https://api.github.com/users/mxscho/following{/other_user}","gists_url":"https://api.github.com/users/mxscho/gists{/gist_id}","starred_url":"https://api.github.com/users/mxscho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mxscho/subscriptions","organizations_url":"https://api.github.com/users/mxscho/orgs","repos_url":"https://api.github.com/users/mxscho/repos","events_url":"https://api.github.com/users/mxscho/events{/privacy}","received_events_url":"https://api.github.com/users/mxscho/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":{"login":"jwalton","id":1771003,"avatar_url":"https://avatars.githubusercontent.com/u/1771003?v=3","gravatar_id":"","url":"https://api.github.com/users/jwalton","html_url":"https://github.com/jwalton","followers_url":"https://api.github.com/users/jwalton/followers","following_url":"https://api.github.com/users/jwalton/following{/other_user}","gists_url":"https://api.github.com/users/jwalton/gists{/gist_id}","starred_url":"https://api.github.com/users/jwalton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwalton/subscriptions","organizations_url":"https://api.github.com/users/jwalton/orgs","repos_url":"https://api.github.com/users/jwalton/repos","events_url":"https://api.github.com/users/jwalton/events{/privacy}","received_events_url":"https://api.github.com/users/jwalton/received_events","type":"User","site_admin":false},"milestone":null,"comments":3,"created_at":"2016-02-17T04:24:09Z","updated_at":"2016-02-17T22:44:37Z","closed_at":null,"body":"Hi,\r\n\r\nMy CoffeeScript skills are not exactly brilliant - but after investigating [this part of the code](https://github.com/benbria/node-amqp-connection-manager/blob/master/src/AmqpConnectionManager.coffee#L104) I would guess that a reconnection attempt is only initiated if the broker went away due to an unexpected error.\r\nHowever, besides the `error` event there's also a `close` event which can be fired from the connection. This happens if you e.g. manually shutdown the RabbitMQ server. The underlying amqplib then generates a `CONNECTION_FORCED` error and emits a `close` event for the connection.\r\n\r\nAs far as I can tell there just needs to be another event handler for connections' `close` events implemented which also tries to do a reconnect. However as mentioned, I haven't been doing anything in CoffeeScript yet and that's why I didn't want to do a pull request right away.\r\n\r\nEDIT: I just saw the (last) test which makes sure that there's no reconnection attempt after a connection close. So furthermore there should be a distinction between close events fired by us manually closing the connection in the client and those fired by the server as described in the above example.\r\n\r\nFeel free to correct me if I'm wrong."},"comment":{"url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/comments/185443139","html_url":"https://github.com/benbria/node-amqp-connection-manager/issues/8#issuecomment-185443139","issue_url":"https://api.github.com/repos/benbria/node-amqp-connection-manager/issues/8","id":185443139,"user":{"login":"mxscho","id":8847270,"avatar_url":"https://avatars.githubusercontent.com/u/8847270?v=3","gravatar_id":"","url":"https://api.github.com/users/mxscho","html_url":"https://github.com/mxscho","followers_url":"https://api.github.com/users/mxscho/followers","following_url":"https://api.github.com/users/mxscho/following{/other_user}","gists_url":"https://api.github.com/users/mxscho/gists{/gist_id}","starred_url":"https://api.github.com/users/mxscho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mxscho/subscriptions","organizations_url":"https://api.github.com/users/mxscho/orgs","repos_url":"https://api.github.com/users/mxscho/repos","events_url":"https://api.github.com/users/mxscho/events{/privacy}","received_events_url":"https://api.github.com/users/mxscho/received_events","type":"User","site_admin":false},"created_at":"2016-02-17T22:44:37Z","updated_at":"2016-02-17T22:44:37Z","body":"Thanks for looking into this.\r\n\r\nI would assume that `launchctl` probably just killed the RabbitMQ server process in your case so there's nothing the server can send to the client.\r\nI'm working with RabbitMQ 3.3.5 on Debian 8 (Jessie).\r\nIf I do a\r\n\r\n    service rabbitmq-server stop\r\n\r\n[this method](https://github.com/squaremo/amqp.node/blob/master/lib/connection.js#L76) in amqplib gets called.\r\nI don't know anything about the AMQP, but I think that there's something like a close packet a server can send to the client to tell the client that it's going to shut down.\r\n\r\nWhat's happening now is that this method [checks if the error occurred is fatal](https://github.com/squaremo/amqp.node/blob/master/lib/connection.js#L92). In this case the generated error contains the code `defs.constants.CONNECTION_FORCED` which I think represents a closed connection initiated by the server.\r\n\r\nDue to the fact that it's not a fatal error, the connection does not emit an `error` event and the amqp-connection-manager does not see this loss of the connection. Instead, [the function which is called at the end](https://github.com/squaremo/amqp.node/blob/master/lib/connection.js#L95) just emits a `close` event on the connection which is currently not handled by amqp-connection-manager.\r\nAs far as I understood - in e.g. our case of a graceful shutdown only a `close` event is emitted, whereas in case of an error both, an `error` and a `close` event are fired. Not quite sure though because I haven't been testing this.\r\n\r\nIf I do the above described service stop command, the `maybeErr` taken by [the `toClosed(...)` function](https://github.com/squaremo/amqp.node/blob/master/lib/connection.js#L398) contains following message:\r\n\r\n    Connection closed: 320 (CONNECTION-FORCED) with message \"CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'\"\r\n\r\nI don't know if there are any other reasons besides `shutdown` right now, but I think this should definitely issue a reconnect in amqp-connection-manager.\r\n\r\nIf you need any more information or reproduction steps/descriptions feel free to ask me about it."}}