一个Sidekiq作业结束前释放的ActiveRecord连接 [英] Releasing ActiveRecord connection before the end of a Sidekiq job

查看:308
本文介绍了一个Sidekiq作业结束前释放的ActiveRecord连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及的性能和优化一个可以实现Sidekiq作业方式的问题。

This question deals with matters of performance and optimization with the way one can implement Sidekiq jobs.

假设我们有以下的工作流程

Suppose we have the following job workflow

DEF执行

# Step 1
do_things_with_activerecord_db_and_get_some_parameters()

# Step 2
step2_perform_an_http_request_with_these_parameters_on_unreliable_server()

     

结束

end

在下面的情况下,一个ActiveRecord连接取自池在步骤1中,并且将仅由SideKiq被释放后作业已完成(或失败)通过SideKiq的ActiveRecord的中间件。

In the following case, an ActiveRecord connection is taken from the pool at Step 1, and will only be released by SideKiq after job has completed (or failed) by the ActiveRecord middleware of SideKiq.

由于外部HTTP服务器在第二步上,我们做的请求是不可靠的,HTTP请求可能需要很长的时间,甚至超时,因此,ActiveRecord的连接已被锁定,没有为所有的时间,对吧?

Since the external http server at step2 on which we do the request is unreliable, the http request can take a long time or even timeout, and thus the ActiveRecord connection is locked for nothing for all that time, right?

所以我的问题是:是否有针对性,实用性和安全调用:

So my question is: Is it pertinent, useful and safe to call:

的ActiveRecord :: Base.clear_active_connections!

ActiveRecord::Base.clear_active_connections!

步骤1和步骤2之间,以使工作释放资源本身并使其可用于其它类似工作?还是我错过了一些关于连接池?这种方法可应用于Redis的连接呢?

between Step 1 and Step 2, so that the job frees the resource by itself and make it available for other similar jobs? Or have I missed something about connection pools? Can this method be applied to the redis connection too?

由于受前进!

推荐答案

您一定要打电话clear_active_connections!

You definitely want to call clear_active_connections!.

我们在环境中运行的ActiveRecord,我们使用JMS的TorqueBox服务器上,而我们所要做的,以确保连接中解脱出来,类似的事情。

We run ActiveRecord in an environment where we make use of JMS on a TorqueBox server, and we had to do something similar in order to ensure connections were freed.

作为一个警告,你也需要做,如果你产生一个线程,这使得ActiveRecord使用,因为(在ActiveRecord的3.2肯定)线程ID作为连接结帐过程的一部分。

As a caveat, you will also need to do this if you spawn a Thread which makes use of ActiveRecord, since (in ActiveRecord 3.2 certainly) the thread id is used as part of the connection checkout process.

我们多次使用的模式如下:

A pattern we use repeatedly is the following:

def with_connection(&block)
  ActiveRecord::Base.connection_pool.with_connection do
    yield block
  end
ensure
  ActiveRecord::Base.clear_active_connections!
  ActiveRecord::Base.connection.close
end

,你可以使用这样的:

which you could use like this:

with_connection do 
  do_things_with_activerecord_db_and_get_some_parameters()   
end

这篇关于一个Sidekiq作业结束前释放的ActiveRecord连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆