Class: Zhong::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/zhong/scheduler.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  timeout: 0.5,
  grace: 15.minutes,
  long_running_timeout: 5.minutes,
  tz: nil,
  heartbeat_key: "zhong:heartbeat",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zhong/scheduler.rb', line 13

def initialize(config = {})
  @jobs = {}
  @callbacks = {}
  @config = DEFAULT_CONFIG.merge(config)

  @logger = @config[:logger]
  @redis = @config[:redis]
  @tz = @config[:tz]
  @category = nil
  @error_handler = nil
  @running = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/zhong/scheduler.rb', line 3

def config
  @config
end

#jobsObject (readonly)

Returns the value of attribute jobs.



3
4
5
# File 'lib/zhong/scheduler.rb', line 3

def jobs
  @jobs
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/zhong/scheduler.rb', line 3

def logger
  @logger
end

#redisObject (readonly)

Returns the value of attribute redis.



3
4
5
# File 'lib/zhong/scheduler.rb', line 3

def redis
  @redis
end

Instance Method Details

#category(name) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
42
# File 'lib/zhong/scheduler.rb', line 34

def category(name)
  raise "cannot nest categories: #{name} would be nested in #{@category} (#{caller.first})" if @category

  @category = name.to_s

  yield(self)

  @category = nil
end

#clearObject



26
27
28
29
30
31
32
# File 'lib/zhong/scheduler.rb', line 26

def clear
  raise "unable to clear while running; run Zhong.stop first" if @running

  @jobs = {}
  @callbacks = {}
  @category = nil
end

#error_handler(&block) ⇒ Object



54
55
56
57
# File 'lib/zhong/scheduler.rb', line 54

def error_handler(&block)
  @error_handler = block if block_given?
  @error_handler
end

#every(period, name, opts = {}, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/zhong/scheduler.rb', line 44

def every(period, name, opts = {}, &block)
  raise "must specify a period for #{name} (#{caller.first})" unless period

  job = Job.new(name, opts.merge(@config).merge(every: period, category: @category), &block)

  raise "duplicate job #{job}" if jobs.key?(job.id)

  @jobs[job.id] = job
end

#find_by_name(job_name) ⇒ Object



110
111
112
# File 'lib/zhong/scheduler.rb', line 110

def find_by_name(job_name)
  @jobs[Digest::SHA256.hexdigest(job_name)]
end

#on(event, &block) ⇒ Object



59
60
61
62
# File 'lib/zhong/scheduler.rb', line 59

def on(event, &block)
  raise "unknown callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
  (@callbacks[event.to_sym] ||= []) << block
end

#redis_timeObject



114
115
116
117
118
# File 'lib/zhong/scheduler.rb', line 114

def redis_time
  s, ms = @redis.time # returns [seconds since epoch, microseconds]
  now = Time.at(s + ms / (10**6))
  @tz ? now.in_time_zone(@tz) : now
end

#startObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zhong/scheduler.rb', line 64

def start
  logger.info "starting at #{redis_time}"

  @stop = false

  trap_signals

  raise "already running" if @running

  loop do
    @running = true

    if fire_callbacks(:before_tick)
      now = redis_time

      jobs_to_run(now).each do |_, job|
        break if @stop
        run_job(job, now)
      end

      break if @stop

      fire_callbacks(:after_tick)

      heartbeat(now)

      break if @stop
    else
      logger.info "skipping tick due to a `:before_tick` callback"
    end

    sleep_until_next_second

    break if @stop
  end

  @running = false

  Thread.new { logger.info "stopped" }.join
end

#stopObject



105
106
107
108
# File 'lib/zhong/scheduler.rb', line 105

def stop
  Thread.new { logger.error "stopping" } if @running # thread necessary due to trap context
  @stop = true
end