-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intermittent ActiveRecord::ConnectionNotEstablished error #47
Comments
It's definitely not a good idea for Que to try to start working jobs before ActiveRecord has established the connection. It's strange that I never experienced that in my testing, I wonder if there's something in your setup that's causing this to happen. Regardless, I think the appropriate thing to do is for the Railtie to ensure that the background workers are not started up until the connection is established. Hopefully ActiveRecord provides some kind of hook to do that, but I'm not sure. If you have time to investigate it that'd be great, otherwise I should have time to look into it tonight. Thanks for the report! |
No problem! It is strange... I'm trying to pin down the exact circumstances where I trigger the error, but it seems quite random at the moment. I'll have a go this evening also, although don't wait for me; I need to familiarise myself with the codebase first, so it could take a while. |
So it seems the only way I could replicate this issue was as mentioned above; setting As I understand things, calling Its not nice, but the following works for me. In def checkout_activerecord_adapter(&block)
Timeout.timeout(1) do
begin
::ActiveRecord::Base.connection_pool.with_connection(&block)
rescue ::ActiveRecord::ConnectionNotEstablished => e
puts "couldn't connect to db, retrying..."
sleep 0.1
retry
end
end
end I can't see a nice place to handle this that's specific to the |
Your understanding is right, but I think it's the responsibility of the railtie to be delaying the worker pool appropriately, when necessary. ActiveRecord does have an initializer that connects to the DB: https://github.com/rails/rails/blob/v4.1.2/activerecord/lib/active_record/railtie.rb#L117 So, I'm thinking the clean solution to this is to make sure that the worker pool only starts up after that initializer fires (when ActiveRecord is being used at all, of course). I/We will have to dig more into Rails' initializer system to figure out the best way to do this. I'm debating between:
I'm leaning towards number 1, I think. |
BTW, changing the behavior of worker_count won't be trivial, I think, so please don't feel obligated to submit a patch - I can take care of it. I mention the initializer solution because it might make for a cleaner stopgap solution for you. |
I started digging into this and it turned out to be thornier than I thought. I don't think I'll be able to get it into a releasable state before the weekend. In the meantime, if you need any help with your temporary fix, please let me know! |
Thanks for looking into this issue, its really appreciated! |
I vote for option 1. It was confusing to see that setting worker_count starts the job processing. I'd be happy to see a |
Alright, would you try out master and see if it works for you? I've basically just made it so that mode and worker_count settings don't affect each other. So the worker pool won't hit the DB until the mode is set to :async, but the railtie will still so that for you. You should be able to set worker_count in your app configuration (like you were trying to do before) and everything should just work. If this works I'll release it as 0.8.0, since it's a (minor) API change. |
Thanks for the quick work. I've just tied against master and sadly I'm still seeing the problem. I either get I'll spend some time this evening putting together a minimal Rails app to reproduce this error and point you to that. Hopefully that will rule out anything weird on my end and give us something solid to discuss. |
Alright, thanks! |
Ok, so ignore what I said before about still seeing However... if I set |
Alright, it turned out I hadn't put enough thought into how the worker pool I don't want to make a release until I spend some time figuring out how On Mon, Jul 7, 2014 at 12:55 PM, Jamie English notifications@github.com
|
That one did the trick! The app I was working on that had the original issue is now using rake for the workers, so the current release is fine for me. Cheers. |
I experimented with Unicorn a bit and things look good, so I released 0.8.0. |
I have Que 0.7.3 in a Rails 4.1.1 application on MRI running Thin and using all default settings except for
config.que.worker_count = 1
in myconfig/environments/development.rb
. My ActiveRecord connection pool is set to 5 connections.When I run
rails s
, on my last attempt, the app successfully started up 1 in 10 tries. For the other 9, I received the following output:This seems like a race condition, as if I add
sleep 2
to the top ofQue::Worker#work_loop
all is fine.Happy to provide a fix if pointed in the right direction.
The text was updated successfully, but these errors were encountered: