Module: CronoTrigger::Schedulable::ClassMethods

Defined in:
lib/crono_trigger/schedulable.rb

Instance Method Summary collapse

Instance Method Details

#crono_trigger_column_name(name) ⇒ Object



93
94
95
# File 'lib/crono_trigger/schedulable.rb', line 93

def crono_trigger_column_name(name)
  crono_trigger_options["#{name}_column_name".to_sym].try(:to_s) || name.to_s
end

#crono_trigger_unlock_all!Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/crono_trigger/schedulable.rb', line 101

def crono_trigger_unlock_all!
  wheres = all.where_values_hash
  if wheres.empty?
    raise NoRestrictedUnlockError, "Need `where` filter at least one, because this method is danger"
  else
    update_all(
      crono_trigger_column_name(:execute_lock) => 0,
      crono_trigger_column_name(:locked_by) => nil,
    )
  end
end

#executables_with_lock(limit: CronoTrigger.config.executor_thread * 3, worker_count: 1) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/crono_trigger/schedulable.rb', line 66

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]
rescue => e
  raise if records.empty?

  logger&.warn("Failed to fetching some records but continue processing records: #{e} (#{e.class})")
  [records, maybe_has_next]
end

#execute_lock_timeoutObject



97
98
99
# File 'lib/crono_trigger/schedulable.rb', line 97

def execute_lock_timeout
  (crono_trigger_options[:execute_lock_timeout] || DEFAULT_EXECUTE_LOCK_TIMEOUT)
end