Class: Workerholic::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/workerholic/manager.rb

Overview

Handles polling from Redis and hands job to worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Manager

Returns a new instance of Manager.



6
7
8
9
10
11
12
13
14
# File 'lib/workerholic/manager.rb', line 6

def initialize(opts = {})
  @workers = []
  Workerholic.workers_count.times { @workers << Worker.new }

  @scheduler = JobScheduler.new(sorted_set: opts[:sorted_set])
  @worker_balancer = WorkerBalancer.new(workers: workers, auto_balance: opts[:auto_balance])

  @logger = LogManager.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/workerholic/manager.rb', line 4

def logger
  @logger
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



4
5
6
# File 'lib/workerholic/manager.rb', line 4

def scheduler
  @scheduler
end

#worker_balancerObject (readonly)

Returns the value of attribute worker_balancer.



4
5
6
# File 'lib/workerholic/manager.rb', line 4

def worker_balancer
  @worker_balancer
end

#workersObject (readonly)

Returns the value of attribute workers.



4
5
6
# File 'lib/workerholic/manager.rb', line 4

def workers
  @workers
end

Instance Method Details

#shutdownObject



29
30
31
32
33
34
35
36
37
# File 'lib/workerholic/manager.rb', line 29

def shutdown
  workers.each(&:kill)
  worker_balancer.kill
  scheduler.kill
  Starter.kill_memory_tracker_thread

  workers.each(&:join)
  scheduler.join
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/workerholic/manager.rb', line 16

def start
  worker_balancer.start
  workers.each(&:work)
  scheduler.start

  sleep
rescue SystemExit, Interrupt
  logger.info("Workerholic's process #{Process.pid} is gracefully shutting down, letting workers finish their current jobs...")
  shutdown

  exit
end