Class: ChildProcess::Server
- Inherits:
-
Object
- Object
- ChildProcess::Server
- Defined in:
- lib/childprocess/server.rb
Class Method Summary collapse
-
.connect(uri) ⇒ DrbObject<Server>
Connect to existing DRb service.
Instance Method Summary collapse
-
#alive?(pid) ⇒ Bool
Check whether a process managed by this server is alive.
-
#clean_up ⇒ Object
Clean up exited processes.
-
#initialize ⇒ Server
constructor
A new instance of Server.
-
#launch(*commands) ⇒ Integer
Launch a process in background.
-
#list_pids ⇒ Array<Integer>
List process ids managed by this server.
-
#read_output(pid) ⇒ String
Read output, will not block.
-
#start_service(uri, wait = true) ⇒ DRb::DRbServer
Start DRb service.
-
#stop(pid) ⇒ Object
Stop a process managed by this server.
-
#write_input(pid, content) ⇒ Object
Write to input.
Constructor Details
#initialize ⇒ Server
28 29 30 31 |
# File 'lib/childprocess/server.rb', line 28 def initialize @processes = {} @mutex = Mutex.new end |
Class Method Details
.connect(uri) ⇒ DrbObject<Server>
Connect to existing DRb service.
12 13 14 15 |
# File 'lib/childprocess/server.rb', line 12 def self.connect(uri) DRb.start_service DRbObject.new_with_uri(uri) end |
Instance Method Details
#alive?(pid) ⇒ Bool
Check whether a process managed by this server is alive.
86 87 88 89 90 |
# File 'lib/childprocess/server.rb', line 86 def alive?(pid) access_processes do |processes| processes[pid] && processes[pid].alive? end end |
#clean_up ⇒ Object
Clean up exited processes.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/childprocess/server.rb', line 102 def clean_up access_processes do |processes| processes.values.select(&:exited?).each do |process| process.io.stdout.path.unlink rescue nil end processes.delete_if { |_, process| process.exited? } # Do not leak @processes outside # We are using dRuby, keep input/output objects simple nil end end |
#launch(*commands) ⇒ Integer
Launch a process in background.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/childprocess/server.rb', line 37 def launch(*commands) output = Tempfile.new('cps-out') output.sync = true process = ChildProcess.build(*commands) process.io.stdout = process.io.stderr = output process.duplex = true process.start pid = process.pid access_processes do |processes| processes[pid] = process end pid end |
#list_pids ⇒ Array<Integer>
List process ids managed by this server.
75 76 77 78 79 |
# File 'lib/childprocess/server.rb', line 75 def list_pids access_processes do |processes| processes.keys end end |
#read_output(pid) ⇒ String
Read output, will not block.
57 58 59 60 61 |
# File 'lib/childprocess/server.rb', line 57 def read_output(pid) access_processes do |processes| File.read(processes[pid].io.stdout.path) rescue nil end end |
#start_service(uri, wait = true) ⇒ DRb::DRbServer
Start DRb service.
22 23 24 25 26 |
# File 'lib/childprocess/server.rb', line 22 def start_service(uri, wait = true) server = DRb.start_service(uri, self) DRb.thread.join if wait server end |
#stop(pid) ⇒ Object
Stop a process managed by this server.
95 96 97 98 99 |
# File 'lib/childprocess/server.rb', line 95 def stop(pid) access_processes do |processes| processes[pid] && processes[pid].stop end end |
#write_input(pid, content) ⇒ Object
Write to input.
66 67 68 69 70 |
# File 'lib/childprocess/server.rb', line 66 def write_input(pid, content) access_processes do |processes| processes[pid] && processes[pid].io.stdin.write(content) rescue nil end end |