{"_id":"574de6d43755ad1700dc9b8e","project":"5633ebff7e9e880d00af1a53","version":{"_id":"5633ec007e9e880d00af1a56","project":"5633ebff7e9e880d00af1a53","__v":16,"createdAt":"2015-10-30T22:15:28.105Z","releaseDate":"2015-10-30T22:15:28.105Z","categories":["5633ec007e9e880d00af1a57","5633f072737ea01700ea329d","5637a37d0704070d00f06cf4","5637cf4e7ca5de0d00286aeb","564503082c74cf1900da48b4","564503cb7f1fff210078e70a","567af26cb56bac0d0019d87d","567afeb8802b2b17005ddea0","567aff47802b2b17005ddea1","567b0005802b2b17005ddea3","568adfffcbd4ca0d00aebf7e","56ba80078cf7c9210009673e","574d127f6f075519007da3d0","574fde60aef76a0e00840927","57a22ba6cd51b22d00f623a0","5a062c15a66ae1001a3f5b09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":15,"user":"5633ec9b35355017003ca3f2","category":{"_id":"564503082c74cf1900da48b4","__v":13,"project":"5633ebff7e9e880d00af1a53","version":"5633ec007e9e880d00af1a56","pages":["564503117f1fff210078e707","564504a8b360ea0d00914852","564504aeb360ea0d00914854","564504b32229d7170010926d","564504bf6b0ca50d00f6bd6b","564504c62c74cf1900da48b9","564504ccd608df0d00d26a21","564504d72c74cf1900da48bb","564504df7f1fff210078e70b","564504e66b0ca50d00f6bd6e","564504ebb360ea0d00914858","568ade3697d8960d0012ab4c","568b2c687a42220d004982bf"],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-11-12T21:22:16.300Z","from_sync":false,"order":7,"slug":"troubleshooting-common-errors","title":"Troubleshooting"},"parentDoc":null,"updates":["59856738cdb6c9000f6a5287"],"next":{"pages":[],"description":""},"createdAt":"2016-05-31T19:32:36.092Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Often we get asked about a connection error developers get when hooking up their initializer with their Bonsai URL, especially while using [our Heroku addon](https://elements.heroku.com/addons/bonsai). The error usually looks something like this in the application logs:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Faraday::ConnectionFailed (Connection refused - connect(2) for \\\"localhost\\\" port 9200):\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\nFor developers that follow getting started guides around the web, or even our recommended set up for the Elasticsearch Rails client — [Ruby on Rails and Bonsai Quickstart Guide](using-the-elasticsearch-rails-client) — this can be a frustrating error. To understand what's going on, a little bit of background is needed:\n\n**Why port 9200? **\nPort 9200 is the default port for Elasticsearch to run on in local development. For anyone who's downloaded and ran Elasticsearch locally, this is familiar. If you're using the `elasticsearch-rails` gem, this client by default tries to connect to port 9200. If for some reason the initializer isn't picking up your cluster URL, in production it will try to connect to the default port, which of course doesn't exist (and therefore throws the ConnectionFailed Error).\n\n**So what can I do?**\nA lot of people are flabbergasted by this error because they have followed a development guide and set up their bonsai.rb or elasticsearch.rb with what seems to be the correct code to initialize an Elasticsearch client in their Rails project. If you have your client initialized, then the root of the problem is 1 of 3 things:\n\n1. Your initializer code isn't running.\n2. Your environment variable doesn't exist.\n3. You've accidentally initialized the client somewhere else in your app, and your intended initializer code is being overwritten.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"1. Initializer code isn't running\"\n}\n[/block]\nWe usually see this when if blocks are wrapping the initializer:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if ENV['BONSAI_URL']\\n\\n\\tElasticsearch::Model.client = Elasticsearch::Client.new({url: ENV['BONSAI_URL'], logs: true})\\n\\nend\",\n      \"language\": \"ruby\"\n    }\n  ]\n}\n[/block]\nTry putting a print statement inside the block. If it doesn't print out, then your real problem is: the ENV variable doesn't exist.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"2. Bonsai ENV variable doesn't exist\"\n}\n[/block]\nBy default when you add Bonsai to a Heroku app, Heroku it sets up variables in your remote config. However, you can also manage your production variables through the Heroku CLI tool.\n\nTo check if your Bonsai URL exists in your config:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"heroku config:get BONSAI_URL --app YOUR_APP_NAME\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\nIf your terminal prints an empty line, you know you've got to fix this by setting it yourself!:\n\nGet your Bonsai URL by [opening up your Bonsai add-on dashboard](bonsai-elasticsearch-dashboard) and copying your cluster URL. Then, set your URL​ with the Heroku CLI tool:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"heroku config:set BONSAI_URL=insert_url_here\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"The [bonsai-elasticsearch-rails](https://github.com/omc/bonsai-elasticsearch-rails) gem, written by one of our engineers, will take care of the Rails client initialization for you with very minimal code. Try it out in place of creating your own initializer!\",\n  \"title\": \"\"\n}\n[/block]\n Read more about Heroku configuration management here: \n- https://devcenter.heroku.com/articles/development-configuration\n- https://devcenter.heroku.com/articles/config-vars\n- https://devcenter.heroku.com/articles/deploying-to-a-custom-rails-environment\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"3. Overwritten initializer code\"\n}\n[/block]\nIn this problem, the only solution is to search through your app for any Elasticsearch client initializer code that might be connecting to 9200, and remove it. A simple search via your text editor, or a grep search through your project could help identify where you have initializer code and where you might start your debugging process:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"grep -r Elasticsearch::Client dir_name/\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\n\nGood luck debugging! If you're a Bonsai customer and still enable to debug your app, send us an email at [support:::at:::bonsai.io](mailto:support@bonsai.io) with a list of things you've tried, and we'll be happy to dig further with you, or point you in the right direction. Additionally, feel free to suggest edits if you've experienced this problem but from a different cause.","excerpt":"","slug":"heroku-faraday-connection-failed-error","type":"basic","title":"Connection Failed"}
Often we get asked about a connection error developers get when hooking up their initializer with their Bonsai URL, especially while using [our Heroku addon](https://elements.heroku.com/addons/bonsai). The error usually looks something like this in the application logs: [block:code] { "codes": [ { "code": "Faraday::ConnectionFailed (Connection refused - connect(2) for \"localhost\" port 9200):", "language": "curl" } ] } [/block] For developers that follow getting started guides around the web, or even our recommended set up for the Elasticsearch Rails client — [Ruby on Rails and Bonsai Quickstart Guide](using-the-elasticsearch-rails-client) — this can be a frustrating error. To understand what's going on, a little bit of background is needed: **Why port 9200? ** Port 9200 is the default port for Elasticsearch to run on in local development. For anyone who's downloaded and ran Elasticsearch locally, this is familiar. If you're using the `elasticsearch-rails` gem, this client by default tries to connect to port 9200. If for some reason the initializer isn't picking up your cluster URL, in production it will try to connect to the default port, which of course doesn't exist (and therefore throws the ConnectionFailed Error). **So what can I do?** A lot of people are flabbergasted by this error because they have followed a development guide and set up their bonsai.rb or elasticsearch.rb with what seems to be the correct code to initialize an Elasticsearch client in their Rails project. If you have your client initialized, then the root of the problem is 1 of 3 things: 1. Your initializer code isn't running. 2. Your environment variable doesn't exist. 3. You've accidentally initialized the client somewhere else in your app, and your intended initializer code is being overwritten. [block:api-header] { "type": "basic", "title": "1. Initializer code isn't running" } [/block] We usually see this when if blocks are wrapping the initializer: [block:code] { "codes": [ { "code": "if ENV['BONSAI_URL']\n\n\tElasticsearch::Model.client = Elasticsearch::Client.new({url: ENV['BONSAI_URL'], logs: true})\n\nend", "language": "ruby" } ] } [/block] Try putting a print statement inside the block. If it doesn't print out, then your real problem is: the ENV variable doesn't exist. [block:api-header] { "type": "basic", "title": "2. Bonsai ENV variable doesn't exist" } [/block] By default when you add Bonsai to a Heroku app, Heroku it sets up variables in your remote config. However, you can also manage your production variables through the Heroku CLI tool. To check if your Bonsai URL exists in your config: [block:code] { "codes": [ { "code": "heroku config:get BONSAI_URL --app YOUR_APP_NAME", "language": "curl" } ] } [/block] If your terminal prints an empty line, you know you've got to fix this by setting it yourself!: Get your Bonsai URL by [opening up your Bonsai add-on dashboard](bonsai-elasticsearch-dashboard) and copying your cluster URL. Then, set your URL​ with the Heroku CLI tool: [block:code] { "codes": [ { "code": "heroku config:set BONSAI_URL=insert_url_here", "language": "text" } ] } [/block] [block:callout] { "type": "info", "body": "The [bonsai-elasticsearch-rails](https://github.com/omc/bonsai-elasticsearch-rails) gem, written by one of our engineers, will take care of the Rails client initialization for you with very minimal code. Try it out in place of creating your own initializer!", "title": "" } [/block] Read more about Heroku configuration management here: - https://devcenter.heroku.com/articles/development-configuration - https://devcenter.heroku.com/articles/config-vars - https://devcenter.heroku.com/articles/deploying-to-a-custom-rails-environment [block:api-header] { "type": "basic", "title": "3. Overwritten initializer code" } [/block] In this problem, the only solution is to search through your app for any Elasticsearch client initializer code that might be connecting to 9200, and remove it. A simple search via your text editor, or a grep search through your project could help identify where you have initializer code and where you might start your debugging process: [block:code] { "codes": [ { "code": "grep -r Elasticsearch::Client dir_name/", "language": "curl" } ] } [/block] Good luck debugging! If you're a Bonsai customer and still enable to debug your app, send us an email at [support@bonsai.io](mailto:support@bonsai.io) with a list of things you've tried, and we'll be happy to dig further with you, or point you in the right direction. Additionally, feel free to suggest edits if you've experienced this problem but from a different cause.