Class: Shrimple::ProcessMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/shrimple/process_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_processes = 20) ⇒ ProcessMonitor

pass 0 to disable max_processes



13
14
15
16
17
# File 'lib/shrimple/process_monitor.rb', line 13

def initialize(max_processes=20)
  @mutex ||= Mutex.new
  @processes ||= []   # TODO: convert this to a hash by child thread?
  @max_processes = max_processes
end

Instance Attribute Details

#max_processesObject

Returns the value of attribute max_processes.



10
11
12
# File 'lib/shrimple/process_monitor.rb', line 10

def max_processes
  @max_processes
end

Instance Method Details

#_add(process) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/shrimple/process_monitor.rb', line 61

def _add process
  @mutex.synchronize do
    if @max_processes >= 0 && @processes.count >= @max_processes
      raise Shrimple::TooManyProcessesError.new("launched process #{@processes.count+1} of #{@max_processes} maximum")
    end
    @processes.push process
  end
end

#_remove(process) ⇒ Object

removes process from process table. pass a block that cleans up after the process. _remove may be called lots of times but block will only be called once



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/shrimple/process_monitor.rb', line 72

def _remove process
  cleanup = false

  @mutex.synchronize do
    cleanup = process._deactivate
    raise "process not in process table??" if cleanup && !@processes.include?(process)
  end

  # don't want to hold mutex when calling callback because it might block
  if cleanup
    yield
    @mutex.synchronize do
      value = @processes.delete(process)
      raise "someone else deleted process??" unless value
    end
  end
end

#countObject



25
26
27
28
29
# File 'lib/shrimple/process_monitor.rb', line 25

def count
  @mutex.synchronize do
    @processes.count
  end
end

#find(&block) ⇒ Object



31
32
33
34
35
# File 'lib/shrimple/process_monitor.rb', line 31

def find &block
  @mutex.synchronize do
    @processes.find(&block)
  end
end

#firstObject



19
20
21
22
23
# File 'lib/shrimple/process_monitor.rb', line 19

def first
  @mutex.synchronize do
    @processes.first
  end
end

#kill_allObject



37
38
39
40
41
# File 'lib/shrimple/process_monitor.rb', line 37

def kill_all
  while f = first
    f.kill
  end
end

#wait_next(nonblock = nil) ⇒ Object

blocks until any child process returns (unless nonblock is true) raises an exception if no processes are running, or if called nonblocking and no processes have finished (see ThreadsWait#next_wait for details).



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shrimple/process_monitor.rb', line 46

def wait_next nonblock=nil
  # we wait on child threads since calling waitpid would produce a race condition.

  threads = {}
  @processes.each { |p|
    threads[p._child_thread] = p
  }

  thread = ThreadsWait.new(threads.keys).next_wait(nonblock)
  process = threads[thread]
  process.stop # otherwise process will be in an indeterminite state
  process
end