【问题标题】:Sidekiq worker changing schema_search_pathSidekiq 工作人员更改 schema_search_path
【发布时间】: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


【解决方案1】:

使用 after_commit 确保作业执行时数据库记录存在。

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 2018-11-05
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2013-09-16
    • 2015-10-16
    • 2012-09-30
    相关资源
    最近更新 更多