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



225
226
227
# File 'lib/crono_trigger/schedulable.rb', line 225

def abort_execution!
  reset!(false)
end

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/crono_trigger/schedulable.rb', line 149

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)


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

def assume_executing?
  locking?
end

#crono_trigger_column_name(name) ⇒ Object



279
280
281
# File 'lib/crono_trigger/schedulable.rb', line 279

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

#crono_trigger_lock!(**attributes) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/crono_trigger/schedulable.rb', line 229

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



251
252
253
254
255
256
257
258
259
260
# File 'lib/crono_trigger/schedulable.rb', line 251

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

#crono_trigger_unlock!Object



242
243
244
245
246
247
248
249
# File 'lib/crono_trigger/schedulable.rb', line 242

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



118
119
120
121
122
123
124
125
126
# File 'lib/crono_trigger/schedulable.rb', line 118

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



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

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)


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

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)


266
267
268
# File 'lib/crono_trigger/schedulable.rb', line 266

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

#reset!(update_last_executed_at = true) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/crono_trigger/schedulable.rb', line 198

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/crono_trigger/schedulable.rb', line 173

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)


262
263
264
# File 'lib/crono_trigger/schedulable.rb', line 262

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