Heroku Rails 4 could not connect to server: connection refused - ruby

Using postgres.
Haven't been able to push.
Tried this without any luck:
config.assets.initialize_on_precompile = false
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

The accepted answer did not fully resolve this. I tried to find a solution for 2-3 hours without success, then this worked:
In your app directory.
heroku labs:enable user-env-compile
it is still failing?
heroku labs:disable user-env-compile
heroku labs:enable user-env-compile
Then it worked for me, just had to remove and do again.
The following config is no longer needed in Rails 4. Compiling assets must work without it.
config.assets.initialize_on_precompile = false

config.assets.initialize_on_precompile = false
Include that in application.rb, ABOVE module APPNAME
I had originally included it inside
class Application < Rails::Application
Edit: actually, the above didn't fix it.
I had to do this
https://devcenter.heroku.com/articles/labs-user-env-compile

Try executing locally
bundle exec rake assets:precompile RAILS_ENV=production
It might be because of devise gem as in my case. You might be missing secret key in devise initializer. try adding
config.secret_key = "PROVIDE-KEY"

The heroku dev center talks about troubleshooting this. Basically your rails4 app should not rely on having config vars present during runtime.
https://devcenter.heroku.com/articles/rails-asset-pipeline#troubleshooting

If you're using Rolify pre version 3.5 then it might be that:
https://github.com/EppO/rolify/issues/221

Related

Why does this Travis deploy to Heroku fail?

I use Travis CI to build a Ruby app. The app is deployed to Heroku after a successful build. After upgrading from Cedar to Cedar-14 and changing Ruby version to 2.1.4 (as this was required for the stack upgrade) the deploy step fails when Heroku precompiles the project. I have tried various deploy strategies but without success. The logs don’t specify the invalid url which makes it hard to track the troubleshoot this issue. From the deploy log:
Running: rake assets:precompile
rake aborted!
ArgumentError: invalid url
/tmp/build_4345603424563456/vendor/bundle/ruby/2.1.0/gems/redis-3.0.7/lib/redis/client.rb:364:in `_parse_options’
When I push the app straight to Github, the deploy is successful and the stack upgrades to Cedar-14.
So, why does the Heroku deploy step suddenly fail when using Travis CI?
This issue was solved by passing the url to the Redis initialization constructor explicitly instead of using the 'old' way of passing the parsed symbols. The old way seems to work best (if not exclusively) with versions before Rails 4. This was confirmed by the Redis To Go documentation on Heroku: Redis To Go
Rails 4
REDIS = Redis.new(:url => redis_url_string)
pre-Rails 4
uri = URI.parse(redis_url)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
For another, similar issue, see this post: Why does Travis fail to connect use Redis cache_store when deploying to Heroku?

Heroku database migrations failing with "unable to connect"

I'm having a heck of a time getting a Postgres database on a heroku instance to allow me to run database migrations.
I'm also incredibly new to Heroku, so apologies in advance if this is a silly question :)
So my app is on Heroku and can successfully connect to its database - it just can't do anything because the tables aren't set up. My connect block in the code looks like this:
if ENV['DATABASE_URL']
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
else
ActiveRecord::Base.establish_connection(dbconfig['development'])
end
The environment variable is set by Heroku at deploy time - this also works. If I jump in after everything is initialized with Pry or something, connection works great. The dbconfig hash is populated from my db/config.yml which works fine locally.
I can rake db:migrate on my local system just fine. However, trying this via heroku run rake db:migrate gives me an error dump beginning with:
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The only other odd thing I can think of here is that I'm using a gem called standalone migrations (declared in Gemfile) which should allow me to do this without pulling in the rest of Rails.
So, my question, why aren't my migrations running?

When or where in a custom Heroku buildpack are config vars available in the environment?

I understand that running a rake task that expects the environment to fully work requires that the DB connection can actually be stablished. Basically, that's the reason why Heroku needs asset pipeline precompilations to run before the environment can be loaded.
That's why this line is needed to deploy rails 3.X apps to the Cedar:
config.assets.initialize_on_precompile = false
Now, I'm trying to build a custom build pack that needs a rake task to do other assets compilations to make the app work as expected. This is because I have internationalized JS asset files and use https://github.com/fnando/i18n-js
I need to run rake i18n:js:export and that requires the environment. *Is there any point in the buildpack where I can add this call and be sure that the connection to the DB of the Rails app will work? * Loading the environment is as a requirement of this task and I don't know how to work it out and it gives the typical error:
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Naturally, I can compile locally and then deploy. But the buildpack seems like the right tool to solve this matter in an automated way. Thanks for the help!
Check out the user env Heroku labs feature: https://devcenter.heroku.com/articles/labs-user-env-compile

Heroku RACK_ENV says "development" on Thin, but "staging" on Unicorn

I came across this behavior and was wondering if anyone else had seen it. I have a workaround so it's not a show-stopper.
I created a new app on Heroku with a Cedar stack. When demonstrating multiple environments I added the following config var:
heroku config:add RACK_ENV=staging --app appname
I visually verified that the environment var was set, then put the following route in my simple Sinatra example:
get '/?' do
ENV['RACK_ENV']
end
When I tested locally on my laptop, I received the expected development.
When I pushed to Heroku and hit the same route on herokuapp.com I got development instead of staging.
I switched the web server from Thin to Unicorn through the Procfile and pushed the changes back up to Heroku.
Now when I hit the route, I get the expected staging.
Has anyone else seen this? My workaround on another project where I was running Thin was to key the environment off of the New Relic app name. (I didn't switch to Unicorn because I need New Relic to work and currently Cedar and New Relic and Unicorn work together).
I had the same problem with sinatra and thin on the cedar stack using heroku's example sinatra app. The RACK_ENV refuses to be set to anything but development. (Heroku seems to think that it's RACK_ENV is set, since running 'heroku config' displays the environment you set, however in the app it's always development).
The same app on the bamboo stack had no problems.
EDIT: I submitted a ticket to heroku about this and got a really quick response which fixed the bug for me:
QUOTE:
It looks like there's a small bug in our default Procfile if you're using thin. Can you create a Procfile with the following in it?
web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
You can also set both your RACK_ENV and RAILS_ENV to staging using the Heroku gem ... then it works as expected. I think it may be a problem with Heroku.

heroku rails "load error expected app/app/model" instead just "app/model"

I have a Rails 2 application running well on my local machine at the moment. But fails on Heroku, here's the log: http://pastie.org/1957572
I can't figure out why the path is "app/app/model/.." instead of just "app/model...", is there anything I need to config my Rails app for Heroku?
Thanks!
You probably figured this out, but it's because /app is the root folder for apps on Heroku
It's like if you had it on your own VPS and it was in /var/www, then the error would read like "/var/www/app..."

Resources