检测 DB 事务内的非原子交互作用
检测 DB 事务内的非原子交互作用。 示例:
# HTTP calls within transaction
User.transaction do
user = User.new(user_params)
user.save!
# HTTP API call
PaymentsService.charge!(user)
end
#=> raises Isolator::HTTPError
# background job
User.transaction do
user.update!(confirmed_at: Time.now)
UserMailer.successful_confirmation(user).deliver_later
end
#=> raises Isolator::BackgroundJobError当然, Isolator 也可以检测 implicit 事务。考虑一下这个非常常见的不良做法 - 从 after_create 回调中排队执行后台任务:
class Comment raises Isolator::BackgroundJobError隔离器通常用于测试和部署阶段。 Isolator 由 Evil Martians 打造,这是一家为开发者工具、AI 和网络安全初创公司提供设计和工程咨询的美国公司。
将以下一行添加到您应用程序的 Gemfile 中:
# We suppose that Isolator is used in development and test
# environments.
group :development, :test do
gem "isolator"
end
# Or you can add it to Gemfile with `require: false`
# and require it manually in your code.
#
# This approach is useful when you want to use it in staging env too.
gem "isolator", require: false如果您正在对 net http 进行工具化(例如在测试或开发环境中使用 APM),那么可能需要强制 sniffer 进入前置模式:请参阅 https://github.com/palkan/isolator/issues/44#issuecomment-5475678308
Isolator 是一个即插即用的工具,因此它在需要时立即开始工作。但是,存在一些潜在的注意事项:
require "active_job/base"暂无开放 Issues,或尚未同步最近议题。