Module: Zerg::Support::Process::Helpers

Defined in:
lib/zerg_support/spawn.rb

Overview

Helpers for spawning processes.

Class Method Summary collapse

Class Method Details

.close_fds(options) ⇒ Object

Closes all open file descriptors except for stdin/stdout/stderr



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zerg_support/spawn.rb', line 116

def self.close_fds(options)
  return if options[: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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/zerg_support/spawn.rb', line 169

def self.do_redirects(options)
  redirected_files = {}
  file_redirects = []
  fd_redirects = []
  options.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



158
159
160
161
162
163
164
165
# File 'lib/zerg_support/spawn.rb', line 158

def self.set_environment(options)
  return unless options[:env]

  ENV.clear if options[:unsetenv_others]
  options[: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



146
147
148
149
150
151
152
153
154
# File 'lib/zerg_support/spawn.rb', line 146

def self.set_process_group(options)
  return unless options[:pgroup]

  if options[:pgroup].kind_of? Numeric and options[:pgroup] > 0
    Process.setpgid 0, options[: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



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zerg_support/spawn.rb', line 131

def self.set_rlimits(options)
  # rlimit options
  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