Class: WorkerArmy::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/worker_army/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_name = nil) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/worker_army/worker.rb', line 9

def initialize(worker_name = nil)
  @queue = WorkerArmy::Queue.new
  @worker_name = worker_name
  @host_name = Socket.gethostname
  @processed = 0
  @failed = 0
  begin
    # puts "Using config in your home directory"
    @config = YAML.load(File.read("#{ENV['HOME']}/.worker_army.yml"))
  rescue Errno::ENOENT
    # ignore
  end
  @log = WorkerArmy::Log.new.log
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#failedObject

Returns the value of attribute failed.



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

def failed
  @failed
end

#jobObject

Returns the value of attribute job.



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

def job
  @job
end

#processedObject

Returns the value of attribute processed.



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

def processed
  @processed
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

#worker_nameObject

Returns the value of attribute worker_name.



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

def worker_name
  @worker_name
end

Instance Method Details

#process_queueObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/worker_army/worker.rb', line 24

def process_queue
  raise "No job class set!" unless @job
  @job.log = @log if @job.respond_to?(:log)
  @queue.ping(worker_pid: Process.pid, job_name: @job.class.name, host_name: @host_name,
    timestamp: Time.now.utc.to_i)
  @log.info("Worker ready! Waiting for jobs: #{@job.class.name}")
  @log.info("Processed: #{@processed} - Failed: #{@failed}")
  list, element = @queue.pop(@job.class.name)
  if list and element
    execute_job(list, element, 0)
  end
end