Class: Elected::Scheduler::Poller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/elected/scheduler/poller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, timeout = nil) ⇒ Poller

Returns a new instance of Poller.



13
14
15
16
17
18
# File 'lib/elected/scheduler/poller.rb', line 13

def initialize(key, timeout = nil)
  @senado ||= Senado.new key, timeout
  @key    = key
  @jobs   = Concurrent::Hash.new
  @stats  = Stats.new :processed_job, :no_match, :sleep_slave
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



9
10
11
# File 'lib/elected/scheduler/poller.rb', line 9

def jobs
  @jobs
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/elected/scheduler/poller.rb', line 8

def key
  @key
end

#statsObject (readonly)

Returns the value of attribute stats.



9
10
11
# File 'lib/elected/scheduler/poller.rb', line 9

def stats
  @stats
end

Instance Method Details

#add(job) ⇒ Object Also known as: <<



20
21
22
23
# File 'lib/elected/scheduler/poller.rb', line 20

def add(job)
  @jobs[job.name] = job
  self
end

#leader?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/elected/scheduler/poller.rb', line 63

def leader?
  senado.leader?
end

#running?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/elected/scheduler/poller.rb', line 31

def running?
  status == :running
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elected/scheduler/poller.rb', line 39

def start
  return false unless stopped?
  raise 'No jobs to run!' if jobs.empty?

  debug "#{label} starting ..."
  @status = :starting
  start_polling_loop
  @status = :running
  debug "#{label} running poller!"
  @status
end

#statusObject



27
28
29
# File 'lib/elected/scheduler/poller.rb', line 27

def status
  @status ||= :stopped
end

#stopObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/elected/scheduler/poller.rb', line 51

def stop
  return false unless running?

  debug "#{label} stopping poller ..."
  @status = :stopping
  stop_polling_loop
  senado.release
  @status = :stopped
  debug "#{label} stopped poller!"
  @status
end

#stopped?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/elected/scheduler/poller.rb', line 35

def stopped?
  status == :stopped
end

#to_sObject Also known as: inspect



67
68
69
# File 'lib/elected/scheduler/poller.rb', line 67

def to_s
  %{#<#{self.class.name} key="#{key}" timeout="#{timeout}" jobs=#{jobs.size}>}
end