Module: CronoTrigger::Schedulable

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Defined in:
lib/crono_trigger/schedulable.rb

Defined Under Namespace

Modules: ClassMethods Classes: NoRestrictedUnlockError

Constant Summary collapse

DEFAULT_RETRY_LIMIT =
10
DEFAULT_RETRY_INTERVAL =
4
DEFAULT_EXECUTE_LOCK_TIMEOUT =
600

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included_byObject



19
20
21
# File 'lib/crono_trigger/schedulable.rb', line 19

def self.included_by
  @included_by
end

Instance Method Details

#abort_execution!Object



231
232
233
# File 'lib/crono_trigger/schedulable.rb', line 231

def abort_execution!
  reset!(false)
end

#activate_schedule!(at: Time.current) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/crono_trigger/schedulable.rb', line 155

def activate_schedule!(at: Time.current)
  time = calculate_next_execute_at || at

  attributes = {}
  unless self[crono_trigger_column_name(:next_execute_at)]
    attributes[crono_trigger_column_name(:next_execute_at)] = time
  end

  if self.class.column_names.include?(crono_trigger_column_name(:started_at))
    unless self[crono_trigger_column_name(:started_at)]
      attributes[crono_trigger_column_name(:started_at)] = time
    end
  end

  if new_record?
    self.attributes = attributes
  else
    merge_updated_at_for_crono_trigger!(attributes)
    update_columns(attributes)
  end

  self
end

#assume_executing?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/crono_trigger/schedulable.rb', line 281

def assume_executing?
  locking?
end

#crono_trigger_column_name(name) ⇒ Object



285
286
287
# File 'lib/crono_trigger/schedulable.rb', line 285

def crono_trigger_column_name(name)
  self.class.crono_trigger_column_name(name)
end

#crono_trigger_lock!(**attributes) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/crono_trigger/schedulable.rb', line 235

def crono_trigger_lock!(**attributes)
  attributes = {
    crono_trigger_column_name(:execute_lock) => Time.current.to_i,
    crono_trigger_column_name(:locked_by) => CronoTrigger.config.worker_id
  }.merge(attributes)
  merge_updated_at_for_crono_trigger!(attributes)
  if new_record?
    self.attributes = attributes
  else
    update_columns(attributes)
  end
end

#crono_trigger_statusObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/crono_trigger/schedulable.rb', line 257

def crono_trigger_status
  case
  when locking?
    :locked
  when waiting?
    :waiting
  when not_scheduled?
    :not_scheduled
  end
end

#crono_trigger_unlock!Object



248
249
250
251
252
253
254
255
# File 'lib/crono_trigger/schedulable.rb', line 248

def crono_trigger_unlock!
  attributes = {
    crono_trigger_column_name(:execute_lock) => 0,
    crono_trigger_column_name(:locked_by) => nil,
  }
  merge_updated_at_for_crono_trigger!(attributes)
  update_columns(attributes)
end

#do_executeObject



124
125
126
127
128
129
130
131
132
# File 'lib/crono_trigger/schedulable.rb', line 124

def do_execute
  ExecutionTracker.track(self) do
    do_execute_with_catch
  end
rescue Exception => ex
  logger.error(ex) if logger
  save_last_error_info(ex)
  retry_or_reset!(ex)
end

#execute_nowObject



289
290
291
292
293
# File 'lib/crono_trigger/schedulable.rb', line 289

def execute_now
  crono_trigger_lock!(next_execute_at: Time.now)
  save! if new_record?
  do_execute
end

#locking?(at: Time.now) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
279
# File 'lib/crono_trigger/schedulable.rb', line 276

def locking?(at: Time.now)
  self[crono_trigger_column_name(:execute_lock)] > 0 &&
    self[crono_trigger_column_name(:execute_lock)] >= at.to_f - self.class.execute_lock_timeout
end

#not_scheduled?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/crono_trigger/schedulable.rb', line 272

def not_scheduled?
  self[crono_trigger_column_name(:next_execute_at)].nil? && last_executed_at.nil?
end

#reset!(update_last_executed_at = true) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/crono_trigger/schedulable.rb', line 204

def reset!(update_last_executed_at = true)
  logger.info "Reset execution schedule #{self.class}-#{id}" if logger

  attributes = {
    crono_trigger_column_name(:next_execute_at) => calculate_next_execute_at,
    crono_trigger_column_name(:execute_lock) => 0,
    crono_trigger_column_name(:locked_by) => nil,
  }

  now = Time.current

  if update_last_executed_at && self.class.column_names.include?(crono_trigger_column_name(:last_executed_at))
    attributes.merge!(crono_trigger_column_name(:last_executed_at) => now)
  end

  if self.class.column_names.include?("retry_count")
    attributes.merge!(retry_count: 0)
  end

  if self.class.column_names.include?(crono_trigger_column_name(:current_cycle_id))
    attributes.merge!(crono_trigger_column_name(:current_cycle_id) => SecureRandom.uuid)
  end

  merge_updated_at_for_crono_trigger!(attributes, now)
  update_columns(attributes)
end

#retry!(immediately: false) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/crono_trigger/schedulable.rb', line 179

def retry!(immediately: false)
  run_callbacks :retry do
    logger.info "Retry #{self.class}-#{id}" if logger

    now = Time.current
    if immediately
      wait = 0
    else
      wait = crono_trigger_options[:exponential_backoff] ? retry_interval * [2 * (retry_count - 1), 1].max : retry_interval
    end
    attributes = {
      crono_trigger_column_name(:next_execute_at) => now + wait,
      crono_trigger_column_name(:execute_lock) => 0,
      crono_trigger_column_name(:locked_by) => nil,
    }

    if self.class.column_names.include?("retry_count")
      attributes.merge!(retry_count: retry_count.to_i + 1)
    end

    merge_updated_at_for_crono_trigger!(attributes, now)
    update_columns(attributes)
  end
end

#waiting?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/crono_trigger/schedulable.rb', line 268

def waiting?
  !!self[crono_trigger_column_name(:next_execute_at)]
end