Clear active ActiveRecord connections between jobs.#116
Clear active ActiveRecord connections between jobs.#116thomasfedb wants to merge 2 commits intoque-rb:masterfrom thomasfedb:0.10.0-hotfix
Conversation
|
After skimming ActiveRecord's source, it looks like The interface between Que and AR's connection pool is pretty simple, we just call |
|
Hey @chanks, Thanks for the reply. I think I should note that in my workers I access multiple databases (the PostgreSQL one used by Que, as well as a SQL Server database), and using I spent a long time debugging this issue with the SQL Server adapter developer: rails-sqlserver/activerecord-sqlserver-adapter#402 The fact that Rails internally uses |
|
You're using ActiveRecord to access two different databases simultaneously? How are you managing that? Does ActiveRecord have some built-in facility for it or are you using another library in conjunction with it? I'm skeptical that |
|
AR supports using a different connection in each model. http://pivotallabs.com/using-activerecord-with-multiple-databases/ |
|
That's interesting. I don't like it as much as Sequel's approach of having a Database class, but good to know. |
|
Hey @chanks, AR natively supports multiple databases, and has for quite a while. Given that you provide an AR integration, it seems reasonable to me that your integration should ensure connections are returned to the pool between jobs. AR transparently handles the multiple connections (eg: during association loading), and so a developer may not even know that an additional connection has been opened to another database. As I mentioned before, It's causes me significant grief, took over a month to debug and identify the root cause, and I would like to help others avoid the pitfall. Cheers, |
|
Alright, you've convinced me. Sorry to be cranky, it just feels like all the time I'm able to devote to this gem goes right into working around problems with Rails/ActiveRecord. Try current master and let me know how it works! |
|
Hey @chanks, Cheers for fixing this, it's very much appreciated. I'm sorry that supporting Rails/AR is burdensome. I'll deploy your new version into our staging environment tomorrow. Que is a fantastic project, and I chose it over all the other Rails worker gems as it is much easier for me to use (don't need a Redis DB, for example), works well, and was highly recommended. Let me know if I can buy you a coffee or if you have a bitcoin address. 👍 |
|
Ha, no, thanks for the offer, though! On Sat, Jul 25, 2015 at 11:58 AM, Thomas Drake-Brockman <
|
|
Hello, this commit a2a46fb breaks rspec tests in our project. Had to rollback to 0.10 |
|
Breaks specs how? Are you calling Que::Job.work in them? |
|
yes. And this somehow clears all (not que_jobs) records created by rspec/code under test |
|
Hm, dropping the connection would roll back the transaction, and I think by Joe On Fri, Sep 25, 2015 at 12:31 PM, Alexey Timanovsky <
|
|
Well, it clears the active connections, which should roll back the current transaction, which wouldn't be helpful if you're wrapping each spec in a transaction. I think you probably shouldn't be using Que::Job.work in your tests, if you want to test that a job works you should be using |
|
|
|
Why are you calling it manually? |
|
Well, we have many queues and each time we decide from which queue to take a job. This is application logic. |
|
So you've implemented your own worker, and you have tests to make sure it's working jobs correctly, but these tests also are writing other records to the DB for some reason? I don't know enough about what you're doing to know what the best solution for you is, but since Que::Job.work is not really intended to have a lot of DB state going on when it's called, I think it's reasonable for the clear_active_connections! call to happen there. |
Not exactly. que Job
This is unfortunate, as it makes pretty serious assumptions about application. Would it be possible to provide an option or a hook to disable this feature? |
|
Que::Job.work isn't usually called by the application, it's called by a worker, which is generally operating in a very simple loop without a lot of state going on, especially not in the DB (it's not intended to be called inside a transaction, for example). If you really wanted to call it from within your app, I suppose you could, but you'd need to be careful. |
|
Yes, I understand all that. It worked perfectly before. Just design when one queue = one CPU process is not flexible enough. I would like to have a generic queue which I can pop from when I need. |
|
May be you can move Best regards, On 25 September 2015 at 23:07, Chris Hanks notifications@github.com wrote:
|
|
That might be reasonable, I'm not sure. The next time I work on Que (maybe next Friday) I'll look into it. I'll also create a new issue so I don't forget. In the meantime if you want to try moving it there and seeing if you run into any issues, I'd be curious to hear how it works out. |
After having significant issues with long running database connections that only ever occurred on my worker processes, and not on my web processes, I investigated and found that Rails clears out active connections between each web request.
This PR implements the same connection clearing for Que. I'm sure the implementation is not ideal, and I'm happy to revise it with some advice. This has been running successfully in a production environment for a number of months, resolving all issues with long running database connections.