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
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Scheduler



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

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

#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:



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

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



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

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

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

#error_handler(&block) ⇒ Object



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

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

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



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

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



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

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

#on(event, &block) ⇒ Object



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

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



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

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



63
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
# File 'lib/zhong/scheduler.rb', line 63

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
      sleep_until_next_second
    end

    break if @stop
  end

  @running = false

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

#stopObject



101
102
103
104
# File 'lib/zhong/scheduler.rb', line 101

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