Class: Zhong::Scheduler

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  timeout: 0.5,
  grace: 15.minutes,
  long_running_timeout: 5.minutes
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



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

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

  @category = nil
  @error_handler = nil
  @running = false
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



6
7
8
# File 'lib/zhong/scheduler.rb', line 6

def jobs
  @jobs
end

Instance Method Details

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

Yields:

  • (_self)

Yield Parameters:



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

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



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

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

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

#error_handler(&block) ⇒ Object



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

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

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



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

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), @callbacks, &block)

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

  @jobs[job.id] = job
end

#find_by_name(job_name) ⇒ Object



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

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

#on(event, &block) ⇒ Object



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

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

#redis_timeObject



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

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



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

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



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

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