我正在运行大量的后台作业,并且在一个特定的作业上不断遇到竞争状况。
Rails 4、Mongoid、Sidekiq、Redis
Profile has_many ProfilePictures
当此代码被多个线程调用时
def set_as_active_picture
ProfilePicture.where({
profile_id: self.profile.id,
selected: true}
).update_all('$set' => {selected: false})
self.set selected: true
self.profile.set(picture_url: self.image.url)
end我遇到了竞争条件,其中多个ProfilePicture文档都选择了设置为true。
我想要做的是在一组满足profile_id == self.profile_id的ProfilePicture文档上设置一个悲观锁,或者至少在ProfilePicture集合上设置一个悲观锁。
我找不到任何关于Mongoid或Mongo的本机锁定的东西,所以我找了一些宝石。
我添加了afeld/mongoid-locker,但这只适用于单个模型实例。我尝试添加trakio/mongo-lock和servio/ mongo-lock,但不知道如何使用mongo-lock( 'my_key‘指的是什么?)而mongo-locking给了我一个Active_Support 3.0.4的源代码错误
如何在属于特定配置文件文档的ProfilePicture文档集上设置悲观锁定?
发布于 2015-03-27 07:55:07
对于那些关心这个问题的人,我能够解决这个问题,方法是从profile_picture模型中删除选定的属性,而不是向概要文件模型添加selected_pic_id属性。这允许我自动更新概要文件模型,而不是尝试更新profile_picture文档的集合。
https://stackoverflow.com/questions/29267775
复制相似问题