Module: Motor::Alerts::Scheduler

Defined in:
lib/motor/alerts/scheduler.rb

Constant Summary collapse

SCHEDULER_INTERVAL =
1.minute
CHECK_BEHIND_DURATION =
15.minutes
SCHEDULER_TASK =
Concurrent::TimerTask.new(
  execution_interval: SCHEDULER_INTERVAL
) { Motor::Alerts::Scheduler.call }
ALREADY_PROCESSED_CACHE =
ActiveSupport::Cache::MemoryStore.new(size: 5.megabytes)

Class Method Summary collapse

Class Method Details

.callObject


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/motor/alerts/scheduler.rb', line 17

def call
  ScheduledAlertsCache.all.each do |alert|
    next unless (CHECK_BEHIND_DURATION.ago..Time.current).cover?(alert.cron.previous_time.to_local_time)

    ALREADY_PROCESSED_CACHE.fetch("#{alert.id}-#{alert.cron.previous_time.to_i}") do
      Motor::AlertSendingJob.perform_later(alert).job_id
    end
  rescue StandardError => e
    Rails.logger.error(e)
  end
end