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.



6
7
8
9
10
11
12
# File 'lib/workhorse/poller.rb', line 6

def initialize(worker)
  @worker = worker
  @running = false
  @table = Workhorse::DbJob.arel_table
  @is_oracle = ActiveRecord::Base.connection.adapter_name == 'OracleEnhanced'
  @instant_repoll = Concurrent::AtomicBoolean.new(false)
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#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

#instant_repoll!Object

Call this to interrupt current sleep and perform the next poll as soon as possible, then resume in the normal polling interval.



47
48
49
50
# File 'lib/workhorse/poller.rb', line 47

def instant_repoll!
  worker.log 'Aborting next sleep to perform instant repoll', :debug
  @instant_repoll.make_true
end

#running?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/workhorse/poller.rb', line 14

def running?
  @running
end

#shutdownObject



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

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

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/workhorse/poller.rb', line 18

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

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

#waitObject



41
42
43
# File 'lib/workhorse/poller.rb', line 41

def wait
  @thread.join
end