Class: Updater::ThreadWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/updater/thread_worker.rb

Overview

This class repeatedly searches the database for active jobs and runs them

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ThreadWorker

Returns a new instance of ThreadWorker.



14
15
16
17
18
# File 'lib/updater/thread_worker.rb', line 14

def initialize(options={})
  @quiet = options[:quiet]
  @name = options[:name] || "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
  @pid = Process.pid
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/updater/thread_worker.rb', line 12

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/updater/thread_worker.rb', line 11

def name
  @name
end

#pidObject

Returns the value of attribute pid.



10
11
12
# File 'lib/updater/thread_worker.rb', line 10

def pid
  @pid
end

Instance Method Details

#run_loopObject



48
49
50
51
52
53
54
55
# File 'lib/updater/thread_worker.rb', line 48

def run_loop
  if @t.alive?
    @t.wakeup #calling run here is a Bad Idea
  else
    say " ~~ Restarting Job Loop"
    @t = run_job_loop
  end
end

#say(text) ⇒ Object



38
39
40
41
# File 'lib/updater/thread_worker.rb', line 38

def say(text)
  puts text unless @quiet
  logger.info text if logger      
end

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/updater/thread_worker.rb', line 20

def start
  say "*** Starting job worker #{@name}"
  @t = run_job_loop
  
  trap('TERM') { terminate_with @t }
  trap('INT')  { terminate_with @t }
  
  trap('USR1') do
    old_proc = trap('USR1','IGNORE')
    run_loop
    trap('USR1',old_proc)
  end
  
  Thread.pass
 
  sleep unless $exit
end

#stopObject

Raises:

  • (RuntimeError)


43
44
45
46
# File 'lib/updater/thread_worker.rb', line 43

def stop
  raise RuntimeError unless @t
  terminate_with @t
end