Class: Workhorse::Poller

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

Constant Summary collapse

MIN_LOCK_TIMEOUT =

In seconds

0.1
MAX_LOCK_TIMEOUT =

In seconds

1.0
ORACLE_LOCK_MODE =

X_MODE (exclusive)

6
ORACLE_LOCK_HANDLE =

Randomly chosen number

478_564_848

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker) ⇒ Poller

Returns a new instance of Poller.



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

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.



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

def table
  @table
end

#workerObject (readonly)

Returns the value of attribute worker.



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

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.



59
60
61
62
# File 'lib/workhorse/poller.rb', line 59

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

#running?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/workhorse/poller.rb', line 20

def running?
  @running
end

#shutdownObject



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

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

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/workhorse/poller.rb', line 24

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

  @thread = Thread.new do
    loop do
      break unless running?

      begin
        poll
        sleep
      rescue Exception => e
        worker.log %(Poll encountered exception:\n#{e.message}\n#{e.backtrace.join("\n")})
        worker.log 'Worker shutting down...'
        Workhorse.on_exception.call(e) unless Workhorse.silence_poller_exceptions
        @running = false
        worker.instance_variable_get(:@pool).shutdown
        break
      end
    end
  end
end

#waitObject



53
54
55
# File 'lib/workhorse/poller.rb', line 53

def wait
  @thread.join
end