Class: Employer::Employees::AbstractEmployee

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

Direct Known Subclasses

ForkingEmployee, ThreadingEmployee

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ AbstractEmployee

Returns a new instance of AbstractEmployee.



16
17
18
# File 'lib/employer/employees/abstract_employee.rb', line 16

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



6
7
8
# File 'lib/employer/employees/abstract_employee.rb', line 6

def job
  @job
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/employer/employees/abstract_employee.rb', line 6

def logger
  @logger
end

Class Method Details

.before_fork(&block) ⇒ Object



12
13
14
# File 'lib/employer/employees/abstract_employee.rb', line 12

def self.before_fork(&block)
  before_fork_hooks << block
end

.before_fork_hooksObject



8
9
10
# File 'lib/employer/employees/abstract_employee.rb', line 8

def self.before_fork_hooks
  @before_fork_hooks ||= []
end

Instance Method Details

#force_work_stopObject



66
67
# File 'lib/employer/employees/abstract_employee.rb', line 66

def force_work_stop
end

#freeObject



57
58
59
60
61
# File 'lib/employer/employees/abstract_employee.rb', line 57

def free
  return unless work_completed? || work_failed?
  @work_state = nil
  @job = nil
end

#free?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/employer/employees/abstract_employee.rb', line 41

def free?
  job.nil?
end

#perform_jobObject



26
27
28
29
30
# File 'lib/employer/employees/abstract_employee.rb', line 26

def perform_job
  logger.debug("Employee #{self.object_id} is now performing job #{job.id}")
  job.perform
  logger.debug("Employee #{self.object_id} has now completed #{job.id}")
end

#stop_workingObject



36
37
38
39
# File 'lib/employer/employees/abstract_employee.rb', line 36

def stop_working
  return if work_completed? || work_failed?
  force_work_stop
end

#wait_for_completionObject



32
33
34
# File 'lib/employer/employees/abstract_employee.rb', line 32

def wait_for_completion
  work_state(true)
end

#work(job) ⇒ Object



20
21
22
23
24
# File 'lib/employer/employees/abstract_employee.rb', line 20

def work(job)
  raise Employer::Errors::EmployeeBusy unless free?
  @job = job
  self.class.before_fork_hooks.each(&:call)
end

#work_completed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/employer/employees/abstract_employee.rb', line 49

def work_completed?
  true if work_state == :complete
end

#work_failed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/employer/employees/abstract_employee.rb', line 53

def work_failed?
  true if work_state == :failed
end

#work_in_progress?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/employer/employees/abstract_employee.rb', line 45

def work_in_progress?
  true if work_state == :busy
end

#work_state(wait = false) ⇒ Object



63
64
# File 'lib/employer/employees/abstract_employee.rb', line 63

def work_state(wait = false)
end