Module: MiniScheduler

Defined in:
lib/mini_scheduler/web.rb,
lib/mini_scheduler.rb,
lib/mini_scheduler/engine.rb,
lib/mini_scheduler/manager.rb,
lib/mini_scheduler/version.rb,
app/models/mini_scheduler/stat.rb,
lib/mini_scheduler/schedule_info.rb,
lib/mini_scheduler/distributed_mutex.rb,
lib/generators/mini_scheduler/install/install_generator.rb

Overview

Defined Under Namespace

Modules: Generators, Schedule, Web Classes: DistributedMutex, Engine, Manager, ScheduleInfo, Stat

Constant Summary collapse

SidekiqExceptionHandler =
if defined?(Sidekiq.default_configuration) # Sidekiq 7+
  ->(ex, ctx, _config = nil) { Sidekiq.default_configuration.handle_exception(ex, ctx) }
else # Sidekiq 6.5
  ->(ex, ctx, _config = nil) { Sidekiq.handle_exception(ex, ctx) }
end
VERSION =
"0.18.0"

Class Method Summary collapse

Class Method Details

.before_sidekiq_web_request(&blk) ⇒ Object



53
54
55
56
# File 'lib/mini_scheduler.rb', line 53

def self.before_sidekiq_web_request(&blk)
  @before_sidekiq_web_request = blk if blk
  @before_sidekiq_web_request
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (MiniScheduler)

    the object that the method was called on



16
17
18
# File 'lib/mini_scheduler.rb', line 16

def self.configure
  yield self
end

.handle_job_exception(ex, context = {}, _config = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/mini_scheduler.rb', line 32

def self.handle_job_exception(ex, context = {}, _config = nil)
  if job_exception_handler
    job_exception_handler.call(ex, context)
  else
    SidekiqExceptionHandler.call(ex, context)
  end
end

.job_exception_handler(&blk) ⇒ Object



27
28
29
30
# File 'lib/mini_scheduler.rb', line 27

def self.job_exception_handler(&blk)
  @job_exception_handler = blk if blk
  @job_exception_handler
end

.job_ran(&blk) ⇒ Object



48
49
50
51
# File 'lib/mini_scheduler.rb', line 48

def self.job_ran(&blk)
  @job_ran = blk if blk
  @job_ran
end

.redisObject



44
45
46
# File 'lib/mini_scheduler.rb', line 44

def self.redis
  @redis
end

.redis=(r) ⇒ Object



40
41
42
# File 'lib/mini_scheduler.rb', line 40

def self.redis=(r)
  @redis = r
end

.skip_schedule(&blk) ⇒ Object



58
59
60
61
# File 'lib/mini_scheduler.rb', line 58

def self.skip_schedule(&blk)
  @skip_schedule = blk if blk
  @skip_schedule
end

.start(workers: 1) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mini_scheduler.rb', line 63

def self.start(workers: 1)
  schedules = Manager.discover_schedules

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

    schedules.each { |schedule| manager.ensure_schedule!(schedule) if schedule.queue == queue }

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

        sleep 1
      end
    end
  end
end