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

查看:32
本文介绍了在 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

默认执行

# Step 1
do_things_with_activerecord_db_and_get_some_parameters()

# Step 2
step2_perform_an_http_request_with_these_parameters_on_unreliable_server()

结束

在以下情况下,ActiveRecord 连接在步骤 1 中从池中取出,只有在 SideKiq 的 ActiveRecord 中间件完成(或失败)作业后才会由 SideKiq 释放.

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.

由于我们在第2步请求的外部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!.

我们在使用 TorqueBox 服务器上的 JMS 的环境中运行 ActiveRecord,并且我们必须执行类似的操作以确保释放连接.

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

你可以这样使用:

with_connection do 
  do_things_with_activerecord_db_and_get_some_parameters()   
end

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

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