Class: Elected::Scheduler::Job

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/elected/scheduler/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &blk) ⇒ Job

Returns a new instance of Job.



9
10
11
12
13
# File 'lib/elected/scheduler/job.rb', line 9

def initialize(name, &blk)
  @name      = name.to_s
  @callback  = blk if block_given?
  @schedules = Set.new
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/elected/scheduler/job.rb', line 7

def callback
  @callback
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/elected/scheduler/job.rb', line 7

def name
  @name
end

#schedulesObject (readonly)

Returns the value of attribute schedules.



7
8
9
# File 'lib/elected/scheduler/job.rb', line 7

def schedules
  @schedules
end

Instance Method Details

#at(options = {}) ⇒ Object



21
22
23
24
# File 'lib/elected/scheduler/job.rb', line 21

def at(options = {})
  schedules.add Schedule.new(options)
  self
end

#executeObject



30
31
32
33
34
35
36
# File 'lib/elected/scheduler/job.rb', line 30

def execute
  callback.call
  true
rescue Exception => e
  error "Exception: #{e.class.name} : #{e.message}\n  #{e.backtrace[0, 10].join("\n  ")}"
  return false
end

#matches?(time) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/elected/scheduler/job.rb', line 26

def matches?(time)
  schedules.any? { |s| s.matches? time }
end

#run(&blk) ⇒ Object



15
16
17
18
19
# File 'lib/elected/scheduler/job.rb', line 15

def run(&blk)
  raise 'must pass a block' unless block_given?
  @callback = blk
  self
end

#to_sObject Also known as: inspect



38
39
40
# File 'lib/elected/scheduler/job.rb', line 38

def to_s
  %{#<#{self.class.name} name="#{name}" schedules=#{schedules.map { |x| x.to_s }.inspect}>}
end