【发布时间】:2014-08-15 11:14:34
【问题描述】:
我有以下工人:
class ImageWorker
include Sidekiq::Worker
def perform(tenant_id, id, key)
tenant = Tenant.find(tenant_id)
tenant.scope_schema do
image = Image.find(id)
unless image.image_processed?
image.key = key
image.remote_image_url = image.image.direct_fog_url(with_path: true)
image.save!
image.update_column(:image_processed, true)
end
end
end
end
Tenant#scope_schema 方法如下所示:
def scope_schema(*paths)
original_search_path = ActiveRecord::Base.connection.schema_search_path
paths << "extensions"
ActiveRecord::Base.connection.schema_search_path = ["tenant#{id}", *paths].join(",")
yield
ensure
ActiveRecord::Base.connection.schema_search_path = original_search_path
end
当 ImageWorker 作业运行时,它告诉我找不到 id=7 的图像,因此虽然我可以在 Sidekiq 工作人员类之外获取相同的代码并运行,但 scope_schema 似乎不起作用很好。
【问题讨论】:
-
您是否使用 after_commit 来确保在作业尝试执行之前已在数据库中创建记录? github.com/mperham/sidekiq/wiki/…
-
是的,我在发布后不久就意识到这是一个竞争条件。如果您想要信用,请将其作为答案发布。
标签: ruby-on-rails ruby postgresql sidekiq