Class: Employer::Boss
- Inherits:
-
Object
- Object
- Employer::Boss
- Defined in:
- lib/employer/boss.rb
Instance Attribute Summary collapse
-
#employees ⇒ Object
readonly
Returns the value of attribute employees.
-
#keep_going ⇒ Object
readonly
Returns the value of attribute keep_going.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#pipeline ⇒ Object
readonly
Returns the value of attribute pipeline.
-
#sleep_time ⇒ Object
readonly
Returns the value of attribute sleep_time.
Instance Method Summary collapse
- #allocate_employee(employee) ⇒ Object
- #busy_employees ⇒ Object
- #delegate_job(job) ⇒ Object
- #delegate_work ⇒ Object
- #free_employee ⇒ Object
- #free_employee? ⇒ Boolean
- #get_work ⇒ Object
-
#initialize(logger) ⇒ Boss
constructor
A new instance of Boss.
- #manage ⇒ Object
- #pipeline_backend=(backend) ⇒ Object
- #progress_update ⇒ Object
- #stop_employees ⇒ Object
- #stop_managing ⇒ Object
- #update_job_status(employee) ⇒ Object
- #wait_on_employees ⇒ Object
Constructor Details
Instance Attribute Details
#employees ⇒ Object (readonly)
Returns the value of attribute employees.
5 6 7 |
# File 'lib/employer/boss.rb', line 5 def employees @employees end |
#keep_going ⇒ Object (readonly)
Returns the value of attribute keep_going.
5 6 7 |
# File 'lib/employer/boss.rb', line 5 def keep_going @keep_going end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/employer/boss.rb', line 5 def logger @logger end |
#pipeline ⇒ Object (readonly)
Returns the value of attribute pipeline.
5 6 7 |
# File 'lib/employer/boss.rb', line 5 def pipeline @pipeline end |
#sleep_time ⇒ Object (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_employees ⇒ Object
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_work ⇒ Object
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_employee ⇒ Object
104 105 106 |
# File 'lib/employer/boss.rb', line 104 def free_employee employees.find(&:free?) end |
#free_employee? ⇒ Boolean
108 109 110 |
# File 'lib/employer/boss.rb', line 108 def free_employee? free_employee end |
#get_work ⇒ Object
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 |
#manage ⇒ Object
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_update ⇒ Object
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_employees ⇒ Object
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_managing ⇒ Object
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_employees ⇒ Object
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 |