child-process-manager 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/child-process-manager.rb +94 -0
- metadata +55 -0
@@ -0,0 +1,94 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class ChildProcess
|
4
|
+
|
5
|
+
attr_reader :cmd, :port, :on_connect, :on_stdout, :on_stderr, :connected
|
6
|
+
|
7
|
+
def initialize(opts = {})
|
8
|
+
@cmd = opts[:cmd]
|
9
|
+
@ip = opts[:ip] || '127.0.0.1'
|
10
|
+
@port = opts[:port]
|
11
|
+
@on_connect = opts[:on_connect]
|
12
|
+
@io_stdout = opts[:io_stdout]
|
13
|
+
@io_stderr = opts[:io_stderr]
|
14
|
+
@pid = nil
|
15
|
+
@connected = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def start
|
19
|
+
o = {:out => '/dev/null', :err => '/dev/null'}
|
20
|
+
o[:out] = @io_stdout if @io_stdout
|
21
|
+
o[:err] = @io_stderr if @io_stderr
|
22
|
+
@pid = Process.spawn(@cmd, o)
|
23
|
+
loop do
|
24
|
+
begin
|
25
|
+
s = TCPSocket.open(@ip, @port)
|
26
|
+
s.close
|
27
|
+
@on_connect.call if @on_connect
|
28
|
+
return
|
29
|
+
rescue Errno::ECONNREFUSED
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def stop
|
35
|
+
begin
|
36
|
+
Process.detach(@pid)
|
37
|
+
Process.kill('TERM', @pid)
|
38
|
+
Process.waitpid(@pid)
|
39
|
+
rescue
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class ChildProcessManager
|
46
|
+
|
47
|
+
def self.init
|
48
|
+
@@managed_processes ||= {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.spawn(processes)
|
52
|
+
self.init
|
53
|
+
|
54
|
+
if processes.is_a?(Hash)
|
55
|
+
processes = [processes]
|
56
|
+
end
|
57
|
+
|
58
|
+
processes.each do |process_options|
|
59
|
+
process_options[:ip] ||= '127.0.0.1'
|
60
|
+
mpskey = "#{process_options[:ip]}:#{process_options[:port]}"
|
61
|
+
|
62
|
+
if !@@managed_processes[mpskey]
|
63
|
+
cp = ChildProcess.new(process_options)
|
64
|
+
cp.start
|
65
|
+
@@managed_processes[mpskey] = cp
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.reap_all
|
71
|
+
self.init
|
72
|
+
|
73
|
+
@@managed_processes.each_value do |child_process|
|
74
|
+
child_process.stop
|
75
|
+
child_process = nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.reap_one(*args)
|
80
|
+
self.init
|
81
|
+
port = 0; ip = '127.0.0.1'
|
82
|
+
|
83
|
+
if args.size == 1
|
84
|
+
port = args[0]
|
85
|
+
elsif args.size == 2
|
86
|
+
ip = args[0]
|
87
|
+
port = args[1]
|
88
|
+
end
|
89
|
+
if @@managed_processes["#{ip}:#{port}"]
|
90
|
+
@@managed_processes["#{ip}:#{port}"].stop
|
91
|
+
@@managed_processes["#{ip}:#{port}"] = nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: child-process-manager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bcg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2011-03-29 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: child-process-manager
|
17
|
+
email: brenden.grace@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/child-process-manager.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: ""
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --charset=UTF-8
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project: child-process-manager
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: child-process-manager
|
54
|
+
test_files: []
|
55
|
+
|