【发布时间】:2014-06-08 10:17:46
【问题描述】:
在我使用 mongodb 和 rails 构建的应用程序中,我想使用 sidekiq 构建一定数量的处理器来处理和转换数据并将其保存到 mongodb。为了与 mongodb 交互,我使用了 mongoid。对于多租户环境,为了不将每个服务的“路由”信息(租户)传递到每个工作人员上的持久单元,mongoid 建议使用 Thread.current 使用 store_in 类宏提取信息,即:
class Band
include Mongoid::Document
store_in database: ->{ Thread.current[:database] }
end
我也在使用分片,所以我的集合名称也取决于动态参数,所以解决方案类似于:
store_in collection: ->{ Thread.current[:collection] }
我的问题是:在sidekiq worker下使用mongoid的Thread.current和store_in宏如何节省?,使用时需要注意什么?,有没有其他解决方案?
【问题讨论】:
-
您可以考虑使用 Celluloid 在这种特定情况下可能比 sidekiq 更好...导致赛璐珞内部使用线程正常工作
标签: ruby-on-rails mongodb thread-safety mongoid sidekiq