Class: Employer::Employees::ThreadingEmployee

Inherits:
AbstractEmployee show all
Defined in:
lib/employer/employees/threading_employee.rb

Instance Attribute Summary

Attributes inherited from AbstractEmployee

#job, #logger

Instance Method Summary collapse

Methods inherited from AbstractEmployee

before_fork, before_fork_hooks, #free?, #initialize, #perform_job, #stop_working, #wait_for_completion, #work_completed?, #work_failed?, #work_in_progress?

Constructor Details

This class inherits a constructor from Employer::Employees::AbstractEmployee

Instance Method Details

#force_work_stopObject



31
32
33
34
35
# File 'lib/employer/employees/threading_employee.rb', line 31

def force_work_stop
  return if free?
  @thread.kill
  @work_state = :failed
end

#freeObject



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

def free
  super
  @thread = nil
end

#work(job) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/employer/employees/threading_employee.rb', line 6

def work(job)
  super
  @work_state = :busy

  @thread = Thread.new do
    begin
      perform_job
      @work_state = :complete
    rescue => exception
    ensure
      @work_state = :failed if @work_state == :busy
    end
  end
end

#work_state(wait = false) ⇒ Object



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

def work_state(wait = false)
  @thread.join if wait && @thread
  return @work_state
end