Class: QueueingRabbit::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/queueing_rabbit/worker.rb

Defined Under Namespace

Classes: WorkerError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#jobsObject

Returns the value of attribute jobs.



8
9
10
# File 'lib/queueing_rabbit/worker.rb', line 8

def jobs
  @jobs
end

Instance Method Details

#pidObject



52
53
54
# File 'lib/queueing_rabbit/worker.rb', line 52

def pid
  Process.pid
end

#pidfile_exists?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/queueing_rabbit/worker.rb', line 48

def pidfile_exists?
  @pidfile && File.exists?(@pidfile)
end

#read_pidfileObject



44
45
46
# File 'lib/queueing_rabbit/worker.rb', line 44

def read_pidfile
  File.read(@pidfile).to_i if pidfile_exists?
end

#remove_pidfileObject



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_sObject



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

#workObject



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