Class: QueueingRabbit::Worker
- Inherits:
-
Object
- Object
- QueueingRabbit::Worker
- Includes:
- Logging
- Defined in:
- lib/queueing_rabbit/worker.rb
Defined Under Namespace
Classes: WorkerError
Instance Attribute Summary collapse
-
#jobs ⇒ Object
Returns the value of attribute jobs.
Instance Method Summary collapse
-
#initialize(*jobs) ⇒ Worker
constructor
A new instance of Worker.
- #pid ⇒ Object
- #pidfile_exists? ⇒ Boolean
- #read_pidfile ⇒ Object
- #remove_pidfile ⇒ Object
- #stop(connection = QueueingRabbit.connection) ⇒ Object
- #to_s ⇒ Object
- #use_pidfile(filename) ⇒ Object
- #work ⇒ Object
- #work! ⇒ Object
Constructor Details
#initialize(*jobs) ⇒ Worker
Returns a new instance of Worker.
10 11 12 13 14 15 16 |
# File 'lib/queueing_rabbit/worker.rb', line 10 def initialize(*jobs) self.jobs = jobs.map { |job| job.to_s.strip } sync_stdio validate_jobs constantize_jobs end |
Instance Attribute Details
#jobs ⇒ Object
Returns the value of attribute jobs.
8 9 10 |
# File 'lib/queueing_rabbit/worker.rb', line 8 def jobs @jobs end |
Instance Method Details
#pid ⇒ Object
52 53 54 |
# File 'lib/queueing_rabbit/worker.rb', line 52 def pid Process.pid end |
#pidfile_exists? ⇒ Boolean
48 49 50 |
# File 'lib/queueing_rabbit/worker.rb', line 48 def pidfile_exists? @pidfile && File.exists?(@pidfile) end |
#read_pidfile ⇒ Object
44 45 46 |
# File 'lib/queueing_rabbit/worker.rb', line 44 def read_pidfile File.read(@pidfile).to_i if pidfile_exists? end |
#remove_pidfile ⇒ Object
40 41 42 |
# File 'lib/queueing_rabbit/worker.rb', line 40 def remove_pidfile File.delete(@pidfile) if pidfile_exists? end |
#stop(connection = QueueingRabbit.connection) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/queueing_rabbit/worker.rb', line 60 def stop(connection = QueueingRabbit.connection) connection.next_tick do connection.close do info "gracefully shutting down the worker #{self}" remove_pidfile QueueingRabbit.trigger_event(:consuming_done) end end end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/queueing_rabbit/worker.rb', line 56 def to_s "PID=#{pid}, JOBS=#{jobs.join(',')}" end |
#use_pidfile(filename) ⇒ Object
34 35 36 37 38 |
# File 'lib/queueing_rabbit/worker.rb', line 34 def use_pidfile(filename) @pidfile = filename cleanup_pidfile File.open(@pidfile, 'w') { |f| f << pid } end |
#work ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/queueing_rabbit/worker.rb', line 18 def work trap_signals QueueingRabbit.trigger_event(:worker_ready) jobs.each { |job| run_job(QueueingRabbit.connection, job) } QueueingRabbit.trigger_event(:consuming_started) end |
#work! ⇒ Object
28 29 30 31 32 |
# File 'lib/queueing_rabbit/worker.rb', line 28 def work! info "starting a new queueing_rabbit worker #{self}" QueueingRabbit.begin_worker_loop { work } end |