I have been testing with Heroku for internal apps for a while and am
now moving into hosting commercial apps on Heroku. Unfortunately I am
running into some timeout problems with accessing the database. The
below is an example error message I have gotten (e-mail address
removed):
A ActiveRecord::StatementInvalid occurred in user_sessions#create:
PGError: ERROR: canceling statement due to statement timeout
: SELECT * FROM "users" WHERE (LOWER("users".email) =
E'[email protected]') LIMIT 1
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/
active_record/connection_adapters/abstract_adapter.rb:219:in
`log_without_newrelic_instrumentation'
As you can see my query is quite simple. So I don't think the problem
is it being a complicated query. Also there are only about 20 users so
it is not a case of trying to look through too much data. I think
Heroku sets the database timeout to 6 seconds which seems to be plenty
of time to run such a simple query. I contacted Heroku support about
these problems and they told me that it was probably due to another
user on the shared database hogging the resources and suggested I move
to a dedicated database. But since a dedicated database STARTS at $200/
month that is not realistic for this app (and a lot of apps I
imagine).
Where this problem has REALLY bit me in ass is when the error occurs
as the app starts up. I use the Enforce Schema Rules[1] plugin to
automatically setup validations based on the database to make my app a
bit more dry. This plugin simply scans the columns and calls the
appropriate validates_X methods to mirror what is in the database.
Since it does this scanning as the object loads this means my app is
contacting the database as the app loads. If I get this timeout then
the app fails to load. What is worse is that if the user tries to hit
the app again heroku doesn't try again. I have to manually restart the
app in order for it to try again. This means my app is down until I
manually restart.
So my questions are:
1. Is there anything I can do to prevent these timeouts. Would
increasing the timeout beyond 6 seconds help? I imagine to do that I
just need to run a query at the start of a request.
2. If I get an error on startup (database or otherwise) is there a way
I can have heroku try again on the next request instead of doing a
manual restart.
3. Would it help to disable eager class loading by adding the
following to my production environment:
config.eager_load_paths = []
Disabling eager loading would cause the app to not contact the
database on startup. Would adding that cause any other problems? I
found one article that suggested disabling eager loading can help
situations like this[2] and another that says it causes random
problems in the app[3]. Or maybe I should just drop the plugin and
manually type in all the validation macros myself?
Any advice would be greatly appreciated.
1. http://agilewebdevelopment.com/plugins/enforce_schema_rules
2.
http://pivotallabs.com/users/scott/blog/articles/693-standup-2-9-09-chaining-associatons-use-rails-error-handling-and-eager-load-paths
3.
http://www.whatcodecraves.com/articles/2009/03/17/rails_2.2.2_chicken_and_egg_migrations_headache/
--
You received this message because you are subscribed to the Google Groups
"Heroku" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/heroku?hl=en.