Class: Employer::Boss

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoss

Returns a new instance of Boss.



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

def initialize
  @pipeline = nil
  @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

#pipelineObject

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



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

def allocate_employee(employee)
  employees << employee
end

#busy_employeesObject



98
99
100
# File 'lib/employer/boss.rb', line 98

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

#delegate_job(job) ⇒ Object



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

def delegate_job(job)
  raise Employer::Errors::NoEmployeeFree unless employee = free_employee
  employee.work(job)
end

#delegate_workObject



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

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

#free_employeeObject



102
103
104
# File 'lib/employer/boss.rb', line 102

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

#free_employee?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/employer/boss.rb', line 106

def free_employee?
  free_employee
end

#get_workObject



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

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



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

def manage
  @keep_going = true

  while keep_going
    delegate_work
    progress_update
  end

  wait_on_employees
end

#progress_updateObject



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

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

#stop_employeesObject



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

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

#stop_managingObject



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

def stop_managing
  @keep_going = false
end

#update_job_status(employee) ⇒ Object



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

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



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

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