Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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