module MiniScheduler

Based off sidetiq github.com/tobiassvn/sidetiq/blob/master/lib/sidetiq/web.rb

Constants

VERSION

Public Class Methods

before_sidekiq_web_request(&blk) click to toggle source
# File lib/mini_scheduler.rb, line 45
def self.before_sidekiq_web_request(&blk)
  @before_sidekiq_web_request = blk if blk
  @before_sidekiq_web_request
end
configure() { |self| ... } click to toggle source
# File lib/mini_scheduler.rb, line 11
def self.configure
  yield self
end
handle_job_exception(ex, context = {}) click to toggle source
# File lib/mini_scheduler.rb, line 24
def self.handle_job_exception(ex, context = {})
  if job_exception_handler
    job_exception_handler.call(ex, context)
  else
    SidekiqExceptionHandler.handle_exception(ex, context)
  end
end
job_exception_handler(&blk) click to toggle source
# File lib/mini_scheduler.rb, line 19
def self.job_exception_handler(&blk)
  @job_exception_handler = blk if blk
  @job_exception_handler
end
job_ran(&blk) click to toggle source
# File lib/mini_scheduler.rb, line 40
def self.job_ran(&blk)
  @job_ran = blk if blk
  @job_ran
end
redis() click to toggle source
# File lib/mini_scheduler.rb, line 36
def self.redis
  @redis
end
redis=(r) click to toggle source
# File lib/mini_scheduler.rb, line 32
def self.redis=(r)
  @redis = r
end
skip_schedule(&blk) click to toggle source
# File lib/mini_scheduler.rb, line 50
def self.skip_schedule(&blk)
  @skip_schedule = blk if blk
  @skip_schedule
end
start(workers: 1) click to toggle source
# File lib/mini_scheduler.rb, line 55
def self.start(workers: 1)
  schedules = Manager.discover_schedules

  Manager.discover_queues.each do |queue|
    manager = Manager.new(queue: queue, workers: workers)

    schedules.each do |schedule|
      if schedule.queue == queue
        manager.ensure_schedule!(schedule)
      end
    end

    Thread.new do
      while true
        begin
          if !self.skip_schedule || !self.skip_schedule.call
            manager.tick
          end
        rescue => e
          # the show must go on
          handle_job_exception(e, message: "While ticking scheduling manager")
        end

        sleep 1
      end
    end
  end
end