lib/crono_trigger/schedulable.rb in crono_trigger-0.7.1 vs lib/crono_trigger/schedulable.rb in crono_trigger-0.8.0

- old
+ new

@@ -61,21 +61,26 @@ validate :validate_cron_format end module ClassMethods - def executables_with_lock(limit: CronoTrigger.config.executor_thread * 3) - ids = executables(limit: limit).pluck(:id) + def executables_with_lock(limit: CronoTrigger.config.executor_thread * 3, worker_count: 1) + # Fetch more than `limit` records because other workers might have acquired the lock + # and this method might not be able to return enough records for the executor to + # make the best use of the CPU. + ids = executables(limit: limit * worker_count).pluck(:id) maybe_has_next = !ids.empty? records = [] ids.each do |id| transaction do r = all.lock.find(id) unless r.locking? r.crono_trigger_lock! records << r end end + + return [records, maybe_has_next] if records.size == limit end [records, maybe_has_next] end def crono_trigger_column_name(name)