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

Returns a new instance of Scheduler.



12
13
14
15
16
17
18
19
20
21
22
# 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
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:



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

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

  @category = name.to_s

  yield(self)

  @category = nil
end

#error_handler(&block) ⇒ Object



47
48
49
50
# File 'lib/zhong/scheduler.rb', line 47

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

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



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

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

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

  if jobs.key?(job.id)
    @logger.error "duplicate job #{job}, skipping"
    return
  end

  @jobs[job.id] = job
end

#on(event, &block) ⇒ Object



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

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

#startObject



57
58
59
60
61
62
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
# File 'lib/zhong/scheduler.rb', line 57

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

  @stop = false

  trap_signals

  loop do
    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

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

#stopObject



89
90
91
92
# File 'lib/zhong/scheduler.rb', line 89

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