Class: Employer::Boss

Inherits:
Object
  • Object
show all
Defined in:
lib/employer/boss.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Boss

Returns a new instance of Boss.



7
8
9
10
11
12
# File 'lib/employer/boss.rb', line 7

def initialize(logger)
  @logger = logger
  @pipeline = Employer::Pipeline.new(logger)
  @employees = []
  @sleep_time_index = 0
end

Instance Attribute Details

#employeesObject (readonly)

Returns the value of attribute employees.



5
6
7
# File 'lib/employer/boss.rb', line 5

def employees
  @employees
end

#keep_goingObject (readonly)

Returns the value of attribute keep_going.



5
6
7
# File 'lib/employer/boss.rb', line 5

def keep_going
  @keep_going
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/employer/boss.rb', line 5

def logger
  @logger
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



5
6
7
# File 'lib/employer/boss.rb', line 5

def pipeline
  @pipeline
end

#sleep_timeObject (readonly)

Returns the value of attribute sleep_time.



5
6
7
# File 'lib/employer/boss.rb', line 5

def sleep_time
  @sleep_time
end

Instance Method Details

#allocate_employee(employee) ⇒ Object



18
19
20
# File 'lib/employer/boss.rb', line 18

def allocate_employee(employee)
  employees << employee
end

#busy_employeesObject



100
101
102
# File 'lib/employer/boss.rb', line 100

def busy_employees
  employees.select { |employee| !employee.free? }
end

#delegate_job(job) ⇒ Object



94
95
96
97
98
# File 'lib/employer/boss.rb', line 94

def delegate_job(job)
  raise Employer::Errors::NoEmployeeFree unless employee = free_employee
  logger.info("Delegating job #{job.id} to employee #{employee.object_id}")
  employee.work(job)
end

#delegate_workObject



37
38
39
40
41
# File 'lib/employer/boss.rb', line 37

def delegate_work
  while free_employee? && job = get_work
    delegate_job(job)
  end
end

#free_employeeObject



104
105
106
# File 'lib/employer/boss.rb', line 104

def free_employee
  employees.find(&:free?)
end

#free_employee?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/employer/boss.rb', line 108

def free_employee?
  free_employee
end

#get_workObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/employer/boss.rb', line 43

def get_work
  sleep_times = [0.1, 0.5, 1, 2.5, 5]
  if job = pipeline.dequeue
    @sleep_time_index = 0
  else
    @sleep_time_index += 1 unless @sleep_time_index == (sleep_times.count - 1)
  end
  @sleep_time = sleep_times[@sleep_time_index]
  sleep(sleep_time)
  job
end

#manageObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/employer/boss.rb', line 26

def manage
  @keep_going = true

  while keep_going
    delegate_work
    progress_update
  end

  wait_on_employees
end

#pipeline_backend=(backend) ⇒ Object



14
15
16
# File 'lib/employer/boss.rb', line 14

def pipeline_backend=(backend)
  pipeline.backend = backend
end

#progress_updateObject



55
56
57
58
59
# File 'lib/employer/boss.rb', line 55

def progress_update
  busy_employees.each do |employee|
    update_job_status(employee)
  end
end

#stop_employeesObject



86
87
88
89
90
91
92
# File 'lib/employer/boss.rb', line 86

def stop_employees
  busy_employees.each do |employee|
    employee.stop_working
    update_job_status(employee)
    employee.free
  end
end

#stop_managingObject



22
23
24
# File 'lib/employer/boss.rb', line 22

def stop_managing
  @keep_going = false
end

#update_job_status(employee) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/employer/boss.rb', line 61

def update_job_status(employee)
  return if employee.work_in_progress?

  job = employee.job

  if employee.work_completed?
    pipeline.complete(job)
  elsif employee.work_failed?
    if job.try_again?
      pipeline.reset(job)
    else
      pipeline.fail(job)
    end
  end

  employee.free
end

#wait_on_employeesObject



79
80
81
82
83
84
# File 'lib/employer/boss.rb', line 79

def wait_on_employees
  busy_employees.each do |employee|
    employee.wait_for_completion
    update_job_status(employee)
  end
end