class ChronoTrigger::Schedule

Attributes

events[R]

Public Class Methods

new() click to toggle source
# File lib/chrono_trigger/schedule.rb, line 8
def initialize
  @events = Concurrent::Array.new
  @pool = Concurrent::Actor::Utils::Pool.spawn! "pool", 5 do |index|
    Rails.logger.info "ChronoTrigger: Spawning worker schedule-#{index}"
    ChronoTrigger::Worker.spawn name: "schedule-#{index}", supervise: true, args: []
  end
end

Public Instance Methods

add(event) click to toggle source
# File lib/chrono_trigger/schedule.rb, line 16
def add(event)
  @events << event if event&.is_a?(ChronoTrigger::Event)
  self
end
clear() click to toggle source
# File lib/chrono_trigger/schedule.rb, line 29
def clear
  @events.each { |event| event.purge! }
  self
end
clear_scope(value) click to toggle source
# File lib/chrono_trigger/schedule.rb, line 34
def clear_scope(value)
  return self unless value
  scope = value if value.is_a?(String)
  scope = value.to_gid.to_s if value.is_a?(ActiveRecord::Base)
  @events.each { |event| event.purge! if event.scope == scope }
  self
end
process_events() click to toggle source
# File lib/chrono_trigger/schedule.rb, line 42
def process_events
  now = right_now
  events.each do |event|
    if event.purge || (event.before && now >= event.before) || event.repeats == 0
      @events.delete(event)
      next
    end
    next if event.after && now < event.after
    if event.at.nil? || event.at == now
      @pool << event
      event.at = now + event.every
      event.repeats -= 1 unless event.repeats == :forever
    end
  end
end
refresh() click to toggle source
# File lib/chrono_trigger/schedule.rb, line 58
def refresh
  now = right_now
  events.each do |event|
    next if event.at.nil?
    event.at = nil if event.at < now
  end
end
remove(uuid) click to toggle source
# File lib/chrono_trigger/schedule.rb, line 21
def remove(uuid)
  return self unless uuid
  uuid = uuid.id if uuid.is_a?(ChronoTrigger::Event)
  uuid_regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
  @events.each { |event| event.purge! if event.id == uuid } if uuid && uuid_regex.match?(uuid.to_s.downcase)
  self
end