Skip to content

Instantly share code, notes, and snippets.

@bpo
Created December 2, 2014 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpo/4cf95a1ff10f0c504c03 to your computer and use it in GitHub Desktop.
Save bpo/4cf95a1ff10f0c504c03 to your computer and use it in GitHub Desktop.
Heroku dyno networking workaround
#!/usr/bin/env ruby
# 1. Add your environment variable keys below to check them
# for connectivity.
CHECK = [
'REDISGREEN_URL',
# Any other services external to Heroku should have their URLs here
]
# 2. Save this file to bin/check-connection, then:
# chmod +x bin/check-connection
#
# 3. To test without deploying, run:
# env -i $(heroku config -s -a your-app) bin/check-connection
#
# 4. Lastly, add it to your Procfile:
# web: bin/check-connection && bin/start
#
#----------------------------------------------
#----------------------------------------------
require 'uri'
require 'socket'
require 'timeout'
def check(host)
if host.include?(',')
return host.split(',').map { |h| check(h) }.inject { |a, b| a && b }
end
uri = URI(host)
Timeout.timeout(1) do
TCPSocket.new(uri.host, uri.port).close
end
true
# ECONNREFUSED, DNS failure, etc
rescue SocketError
false
# connection timeout
rescue Timeout::Error
false
# what was provided could not be parsed as uri
rescue URI::InvalidURIError
true
end
should_exit = false
CHECK.each do |var|
result = check(ENV[var])
unless result
$stderr.puts "!! #{var} failed connection check"
should_exit = true
end
end
exit 1 if should_exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment