`

rails3 update_all 使用:order, :limit条件时,忽略条件问题

阅读更多

这个bug是在使用delay_job(2.1.2)查出来的,后来google了一下,发现已经有人提交了这个问题

 

delay_job中如果有失败任务的话,后台会一直不停的运行失败任务

源代码修改,加注释的为原来的代码,注释后的为自己添加的修改代码

        # Reserve a single job in a single update query.  This causes workers to serialize on the
        # database and avoids contention.
        def self.reserve(worker, max_run_time = Worker.max_run_time)
#          affected_rows = 0
#          ::ActiveRecord::Base.silence do
#            affected_rows = jobs_available_to_worker(worker.name, max_run_time).limit(1).update_all(["locked_at = ?, locked_by = ?", db_time_now, worker.name])
#          end
#          if affected_rows == 1
#            locked_by_worker(worker.name, max_run_time).first
#          else
#            nil
#          end

#修改代码start
          success = false
          ::ActiveRecord::Base.silence do
            available_job = jobs_available_to_worker(worker.name, max_run_time).first
            success = available_job.update_attributes(:locked_at => db_time_now, :locked_by => worker.name) if available_job
          end          
          if success
            locked_by_worker(worker.name, max_run_time).first
          else
            nil
          end
#修改代码end
        end

 

引起的原因就是,那个update_all了。因为update_all前有limit处理,这样limit前的所有条件就会失效了,这样返回的结果就会始终有一个正在处理的任务。

 

[1]https://rails.lighthouseapp.com/projects/8994/tickets/6058-update_all-ignores-conditions-when-orders-and-limit-options-are-supplied

 

 

分享到:
评论

相关推荐

    rails for zombies

    tweets = Tweet.all.order(:zombie) ``` - **查询前 10 条记录**: ```ruby tweets = Tweet.all.limit(10) ``` #### 六、总结 Rails for Zombies 通过一系列生动有趣的示例,向学习者介绍了 Ruby on Rails 中...

    ruby on rails 2.1 what is new (CN)

    named_scope :recent, lambda { {:order => 'created_at DESC', :limit => 5} } end Post.published.recent.all ``` ##### UTC为基础的迁移 为了更好地处理国际化问题,Rails 2.1的迁移命令现在默认使用UTC时间...

    W5D5:W5D5 Classwork-Rails ActiveRecord查询

    - 使用`where`方法时,可以使用哈希、字符串、范围等多种方式指定条件。 - `exists?`: 检查是否存在满足条件的记录。 四、动态Finder ActiveRecord还提供了一些预定义的动态查找方法,如`find_by`和`find_by!`,...

    record_store_SQL:第3周使用SQL的ruby记录存储

    1. **SELECT语句**:用于从数据库中检索数据,可以使用WHERE子句进行条件过滤,GROUP BY进行分组,HAVING进行分组后的过滤,ORDER BY进行排序,LIMIT和OFFSET用于分页。 2. **INSERT INTO语句**:向表中插入新的记录...

    ActiveRecord简单实例_activerecord.zip

    users = User.order("created_at DESC").limit(10) ``` 更新记录: ```ruby user.update(name: "Jane Doe") ``` 删除记录: ```ruby user.destroy ``` ActiveRecord还支持关联,如一对一(has_one),一对多...

    Thinking-Sphinx:ActiveRecordRails的Sphinx插件

    `Thinking-Sphinx` 支持多种高级搜索选项,如使用 `:with` 和 `:without` 过滤特定字段,`:order` 和 `:limit` 进行排序和分页,以及使用 `:conditions` 进行 SQL 风格的查询。 6. **实时索引** `Thinking-Sphinx...

Global site tag (gtag.js) - Google Analytics