Class: Workhorse::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/workhorse/poller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker) ⇒ Poller

Returns a new instance of Poller.



5
6
7
8
# File 'lib/workhorse/poller.rb', line 5

def initialize(worker)
  @worker = worker
  @running = false
end

Instance Attribute Details

#workerObject (readonly)

Returns the value of attribute worker.



3
4
5
# File 'lib/workhorse/poller.rb', line 3

def worker
  @worker
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/workhorse/poller.rb', line 10

def running?
  @running
end

#shutdownObject



31
32
33
34
35
# File 'lib/workhorse/poller.rb', line 31

def shutdown
  fail 'Poller is not running.' unless running?
  @running = false
  wait
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/workhorse/poller.rb', line 14

def start
  fail 'Poller is already running.' if running?
  @running = true

  @thread = Thread.new do
    begin
      loop do
        poll
        break unless running?
        sleep
      end
    rescue => e
      worker.log %(Poller stopped with exception:\n#{e.message}\n#{e.backtrace.join("\n")})
    end
  end
end

#waitObject



37
38
39
# File 'lib/workhorse/poller.rb', line 37

def wait
  @thread.join
end