Class: XPool::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/xpool/process.rb

Instance Method Summary collapse

Constructor Details

#initializeXPool::Process

Returns an instance of XPool::Process



6
7
8
9
# File 'lib/xpool/process.rb', line 6

def initialize
  reset
  @id = spawn
end

Instance Method Details

#alive?Boolean

Returns true when the process is still running.

Returns:

  • Returns true when the process is still running.



94
95
96
# File 'lib/xpool/process.rb', line 94

def alive?
  !dead?
end

#backtraceArray<String>

If a process has failed (see #failed?) this method returns the backtrace of the exception that caused the process to fail.

Returns:

  • Returns the backtrace.



114
115
116
117
# File 'lib/xpool/process.rb', line 114

def backtrace
  synchronize!
  @states[:backtrace]
end

#busy?Boolean

Returns true when the process is executing a unit of work.

Returns:

  • Returns true when the process is executing a unit of work.



68
69
70
71
# File 'lib/xpool/process.rb', line 68

def busy?
  synchronize!
  @states[:busy]
end

#dead?Boolean

Returns true when the process has shutdown.

Returns:

  • Returns true when the process has shutdown.



102
103
104
105
# File 'lib/xpool/process.rb', line 102

def dead?
  synchronize!
  @states[:dead]
end

#failed?Boolean

Returns true when the process has failed due to an unhandled exception.

Returns:

  • Returns true when the process has failed due to an unhandled exception.



85
86
87
88
# File 'lib/xpool/process.rb', line 85

def failed?
  synchronize!
  @states[:failed]
end

#frequencyFixnum

Returns The number of times the process has been asked to schedule work.

Returns:

  • The number of times the process has been asked to schedule work.



37
38
39
# File 'lib/xpool/process.rb', line 37

def frequency
  @frequency
end

#idle?Boolean

Returns true when the process is not executing a unit of work.

Returns:

  • Returns true when the process is not executing a unit of work.



77
78
79
# File 'lib/xpool/process.rb', line 77

def idle?
  !busy?
end

#restartFixnum

Restart the process. The current process shuts down(gracefully) and a new process replaces it. If the current process has failed the new process will inherit its message queue.

Returns:

  • Returns the process ID of the new process.



127
128
129
130
131
# File 'lib/xpool/process.rb', line 127

def restart
  _shutdown 'SIGUSR1', false
  reset(false)
  @id = spawn
end

#schedule(unit, *args) ⇒ XPool::Process

Returns self

Parameters:

  • The unit of work

  • A variable number of arguments to be passed to #run

Returns:

  • Returns self

Raises:

  • When the process is dead.



54
55
56
57
58
59
60
61
62
# File 'lib/xpool/process.rb', line 54

def schedule(unit,*args)
  if dead?
    raise RuntimeError,
      "cannot schedule work on a dead process (with ID: #{@id})"
  end
  @frequency += 1
  @channel.put unit: unit, args: args
  self
end

#shutdownvoid

This method returns an undefined value.

A graceful shutdown of the process.

The signal 'SIGUSR1' is caught in the subprocess and exit is performed through Kernel#exit after the process has finished executing its work.



20
21
22
# File 'lib/xpool/process.rb', line 20

def shutdown
  _shutdown 'SIGUSR1' unless @shutdown
end

#shutdown!void

This method returns an undefined value.

A non-graceful shutdown through SIGKILL.



29
30
31
# File 'lib/xpool/process.rb', line 29

def shutdown!
  _shutdown 'SIGKILL' unless @shutdown
end