Class: Zhong::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_name, config = {}, &block) ⇒ Job

Returns a new instance of Job.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zhong/job.rb', line 5

def initialize(job_name, config = {}, &block)
  @name = job_name
  @category = config[:category]
  @logger = config[:logger]
  @config = config

  @at = config[:at] ? At.parse(config[:at], grace: config.fetch(:grace, 15.minutes)) : nil
  @every = config[:every] ? Every.parse(config[:every]) : nil

  fail "must specific either `at` or `every` for job: #{self}" unless @at || @every

  @block = block

  @redis = config[:redis]
  @tz = config[:tz]
  @if = config[:if]
  @long_running_timeout = config[:long_running_timeout]
  @running = false
  @first_run = true
  @id = Digest::SHA256.hexdigest(@name)
end

Instance Attribute Details

#atObject (readonly)

Returns the value of attribute at.



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

def at
  @at
end

#categoryObject (readonly)

Returns the value of attribute category.



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

def category
  @category
end

#everyObject (readonly)

Returns the value of attribute every.



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

def every
  @every
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_ranObject (readonly)

Returns the value of attribute last_ran.



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

def last_ran
  @last_ran
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#clearObject



113
114
115
# File 'lib/zhong/job.rb', line 113

def clear
  @redis.del(last_ran_key)
end

#desired_at_keyObject



121
122
123
# File 'lib/zhong/job.rb', line 121

def desired_at_key
  "zhong:at:#{self}"
end

#disableObject



91
92
93
# File 'lib/zhong/job.rb', line 91

def disable
  @redis.set(disabled_key, "true")
end

#disabled?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/zhong/job.rb', line 99

def disabled?
  !!@redis.get(disabled_key)
end

#disabled_keyObject



125
126
127
# File 'lib/zhong/job.rb', line 125

def disabled_key
  "zhong:disabled:#{self}"
end

#enableObject



95
96
97
# File 'lib/zhong/job.rb', line 95

def enable
  @redis.del(disabled_key)
end

#last_ran_keyObject



117
118
119
# File 'lib/zhong/job.rb', line 117

def last_ran_key
  "zhong:last_ran:#{self}"
end

#lock_keyObject



129
130
131
# File 'lib/zhong/job.rb', line 129

def lock_key
  "zhong:lock:#{self}"
end

#next_atObject



107
108
109
110
111
# File 'lib/zhong/job.rb', line 107

def next_at
  every_time = @every.next_at(@last_ran) if @last_ran && @every
  at_time = @at.next_at(Time.now) if @at
  [every_time, at_time, Time.now].compact.max || "now"
end

#refresh_last_ranObject



86
87
88
89
# File 'lib/zhong/job.rb', line 86

def refresh_last_ran
  last_ran_val = @redis.get(last_ran_key)
  @last_ran = last_ran_val ? Time.at(last_ran_val.to_i) : nil
end

#run(time = Time.now, error_handler = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zhong/job.rb', line 37

def run(time = Time.now, error_handler = nil)
  return unless run?(time)

  locked = false
  errored = false

  begin
    redis_lock.lock do
      locked = true
      @running = true

      refresh_last_ran

      # we need to check again, as another process might have acquired
      # the lock right before us and obviated our need to do anything
      break unless run?(time)

      if disabled?
        logger.info "not running, disabled: #{self}"
        break
      end

      logger.info "running: #{self}"

      if @block
        begin
          @block.call
        rescue => boom
          logger.error "#{self} failed: #{boom}"
          error_handler.call(boom, self) if error_handler
        end
      end

      ran!(time)
    end
  rescue Suo::LockClientError => boom
    logger.error "unable to run due to client error: #{boom}"
    errored = true
  end

  @running = false

  logger.info "unable to acquire exclusive run lock: #{self}" if !locked && !errored
end

#run?(time = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/zhong/job.rb', line 27

def run?(time = Time.now)
  if @first_run
    clear_last_ran_if_at_changed if @at
    refresh_last_ran
    @first_run = false
  end

  run_every?(time) && run_at?(time) && run_if?(time)
end

#running?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/zhong/job.rb', line 82

def running?
  @running
end

#to_sObject



103
104
105
# File 'lib/zhong/job.rb', line 103

def to_s
  @to_s ||= [@category, @name].compact.join(".").freeze
end