Module: Zerg::Support::Process::Helpers
- Defined in:
- lib/zerg_support/spawn.rb
Overview
Helpers for spawning processes.
Class Method Summary collapse
-
.close_fds(options) ⇒ Object
Closes all open file descriptors except for stdin/stdout/stderr.
-
.do_redirects(options) ⇒ Object
Performs IO redirections according to the given options.
-
.set_environment(options) ⇒ Object
Sets the process’ environment according to the given options.
-
.set_process_group(options) ⇒ Object
Sets the process’ group according to the given options.
-
.set_rlimits(options) ⇒ Object
Sets process limits (rlimits) according to the given options.
Class Method Details
.close_fds(options) ⇒ Object
Closes all open file descriptors except for stdin/stdout/stderr
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/zerg_support/spawn.rb', line 111 def self.close_fds() return if [:close_others] == false ObjectSpace.each_object(IO) do |io| next if [STDIN, STDOUT, STDERR].include? io begin io.close unless io.closed? rescue Exception end end end |
.do_redirects(options) ⇒ Object
Performs IO redirections according to the given options. The options follow the convention of Kernel.spawn in ruby1.9
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/zerg_support/spawn.rb', line 164 def self.do_redirects() redirected_files = {} file_redirects = [] fd_redirects = [] .each do |key, value| next unless key.kind_of? IO if value.kind_of? String if redirected_files[value] fd_redirects << [key, redirected_files[value]] else file_redirects << [key, value] redirected_files[value] = key end else fd_redirects << [key, value] end end (file_redirects + fd_redirects).each do |r| r.first.reopen r.last end end |
.set_environment(options) ⇒ Object
Sets the process’ environment according to the given options. The options follow the convention of Kernel.spawn in ruby1.9
153 154 155 156 157 158 159 160 |
# File 'lib/zerg_support/spawn.rb', line 153 def self.set_environment() return unless [:env] ENV.clear if [:unsetenv_others] [:env].each do |key, value| ENV[key] = value end end |
.set_process_group(options) ⇒ Object
Sets the process’ group according to the given options. The options follow the convention of Kernel.spawn in ruby1.9
141 142 143 144 145 146 147 148 149 |
# File 'lib/zerg_support/spawn.rb', line 141 def self.set_process_group() return unless [:pgroup] if [:pgroup].kind_of? Numeric and [:pgroup] > 0 Process.setpgid 0, [:pgroup] else Process.setsid end end |
.set_rlimits(options) ⇒ Object
Sets process limits (rlimits) according to the given options. The options follow the convention of Kernel.spawn in ruby1.9
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/zerg_support/spawn.rb', line 126 def self.set_rlimits() # rlimit options .each do |k, v| next unless k.kind_of? Symbol and k.to_s[0, 7] == 'rlimit_' rconst = Process.const_get k.to_s.upcase.to_sym if v.kind_of? Enumerable Process.setrlimit rconst, v.first, v.last else Process.setrlimit rconst, v end end end |