Class: Employer::Employees::ForkingEmployee

Inherits:
AbstractEmployee show all
Defined in:
lib/employer/employees/forking_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



45
46
47
48
49
# File 'lib/employer/employees/forking_employee.rb', line 45

def force_work_stop
  return if free?
  Process.kill("KILL", @job_pid)
  work_state(true)
end

#freeObject



22
23
24
25
# File 'lib/employer/employees/forking_employee.rb', line 22

def free
  super
  @job_pid = nil
end

#work(job) ⇒ Object



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

def work(job)
  super

  @job_pid = fork do
    state = nil

    begin
      perform_job
      state = 0
    ensure
      state = 1 if state.nil?
      exit(state)
    end
  end
end

#work_state(wait = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/employer/employees/forking_employee.rb', line 27

def work_state(wait = false)
  return @work_state if [:complete, :failed].include?(@work_state)

  @work_state = :busy

  flags = wait == false ? Process::WNOHANG : 0
  pid, status = Process.waitpid2(@job_pid, flags)
  if pid
    if status.exitstatus == 0
      @work_state = :complete
    else
      @work_state = :failed
    end
  end

  @work_state
end