Class: Cuboid::Processes::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cuboid/processes/manager.rb

Overview

Helper for managing processes.

Author:

  • Tasos “Zapotek” Laskos <tasos.laskos@gmail.com>

Constant Summary collapse

RUNNER =
"#{File.dirname( __FILE__ )}/executables/base.rb"
SPAWN_RETRIES =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



19
20
21
# File 'lib/cuboid/processes/manager.rb', line 19

def initialize
    reset
end

Instance Attribute Details

#pidsArray<Integer> (readonly)

Returns PIDs of all running processes.

Returns:

  • (Array<Integer>)

    PIDs of all running processes.



17
18
19
# File 'lib/cuboid/processes/manager.rb', line 17

def pids
  @pids
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/cuboid/processes/manager.rb', line 286

def self.method_missing( sym, *args, &block )
    if instance.respond_to?( sym )
        instance.send( sym, *args, &block )
    else
        super( sym, *args, &block )
    end
end

.respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/cuboid/processes/manager.rb', line 294

def self.respond_to?( m )
    super( m ) || instance.respond_to?( m )
end

Instance Method Details

#<<(pid) ⇒ Integer

Returns ‘pid`.

Parameters:

  • pid (Integer)

    Adds a PID to the #pids and detaches the process.

Returns:

  • (Integer)

    ‘pid`



32
33
34
35
36
# File 'lib/cuboid/processes/manager.rb', line 32

def <<( pid )
    @pids << pid
    Process.detach pid
    pid
end

#alive?(pid) ⇒ Boolean

Returns ‘true` if the process is alive, `false` otherwise.

Parameters:

  • pid (Integer)

Returns:

  • (Boolean)

    ‘true` if the process is alive, `false` otherwise.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cuboid/processes/manager.rb', line 138

def alive?( pid )
    # Windows is not big on POSIX so try it its own way if possible.
    if Cuboid.windows?
        begin
            alive = false
            processes = wmi.ExecQuery( "select ProcessId from win32_process where ProcessID='#{pid}'" )
            processes.each do |proc|
                proc.ole_free
                alive = true
            end
            processes.ole_free

            return alive
        rescue WIN32OLERuntimeError
        end
    end

    !!(Process.kill( 0, pid ) rescue false)
end

#discard_outputObject



186
187
188
# File 'lib/cuboid/processes/manager.rb', line 186

def discard_output
    @discard_output = true
end

#discard_output?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/cuboid/processes/manager.rb', line 190

def discard_output?
    @discard_output
end

#find(bin) ⇒ Object



73
74
75
76
# File 'lib/cuboid/processes/manager.rb', line 73

def find( bin )
    find_in_path( bin ) || find_in_applications( bin ) ||
        find_in_program_files( bin )
end

#find_in_applications(bin) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cuboid/processes/manager.rb', line 94

def find_in_applications( bin )
    return if !Cuboid.mac?

    @find_in_applications ||= {}
    return @find_in_applications[bin] if @find_in_applications.include?( bin )

    paths = ENV['PATH'].split( File::PATH_SEPARATOR ) | [
      '/Applications/'
    ]

    paths.each do |root|
        glob = File.join( "#{root}/*/Contents/MacOS", '**', bin )

        exe = Dir.glob( glob ).find { |f| File.executable?( f ) }
        return @find_in_applications[bin] = exe if exe
    end

    @find_in_applications[bin] = nil
end

#find_in_path(bin) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cuboid/processes/manager.rb', line 78

def find_in_path( bin )
    @find_in_path ||= {}
    return @find_in_path[bin] if @find_in_path.include?( bin )

    if Cuboid.windows?
        bin = "#{bin}.exe"
    end

    ENV['PATH'].split( File::PATH_SEPARATOR ).each do |path|
        f = File.join( path, bin )
        return @find_in_path[bin] = f if File.exist?( f )
    end

    @find_in_path[bin] = nil
end

#find_in_program_files(bin) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cuboid/processes/manager.rb', line 114

def find_in_program_files( bin )
    return if !Cuboid.windows?

    @find_in_program_files ||= {}
    return @find_in_program_files[bin] if @find_in_program_files.include?( bin )

    [
        ENV['PROGRAMFILES']      || '\\Program Files',
        ENV['ProgramFiles(x86)'] || '\\Program Files (x86)',
        ENV['ProgramW6432']      || '\\Program Files'
    ].each do |root|
        glob = File.join( root, '**', "#{bin}.exe" )
        glob.tr!( '\\', '/' )

        exe = Dir.glob( glob ).find { |f| File.executable?( f ) }
        return @find_in_program_files[bin] = exe if exe
    end

    @find_in_program_files[bin] = nil
end

#kill(pid) ⇒ Object

Parameters:

  • pid (Integer)

    PID of the process to kill.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cuboid/processes/manager.rb', line 40

def kill( pid )
    fail 'Cannot kill self.' if pid == Process.pid

    Timeout.timeout 10 do
        while sleep 0.1 do
            begin
                Process.kill( Cuboid.windows? ? 'KILL' : 'TERM', pid )

            # Either kill was successful or we don't have enough perms or
            # we hit a reused PID for someone else's process, either way,
            # consider the process gone.
            rescue Errno::ESRCH, Errno::EPERM,
                # Don't kill ourselves.
                SignalException

                @pids.delete pid
                return
            end
        end
    end
rescue Timeout::Error
    # SIGTERM didn't take in 10s — escalate. Ruby's default TERM
    # handler only raises SignalException on the main thread; if
    # the main thread already exited but a non-daemon Application
    # thread is keeping the runtime up (audit workers, browser
    # cluster manager, etc.) then no amount of TERMs will land.
    # Without this escalation the spec suite leaks engine
    # subprocesses every time a kill_instance / killall fails to
    # land cleanly.
    Process.kill( 'KILL', pid ) rescue nil
    @pids.delete pid
end

#kill_many(pids) ⇒ Object

Parameters:

  • pids (Array<Integer>)

    PIDs of the process to #kill.



160
161
162
# File 'lib/cuboid/processes/manager.rb', line 160

def kill_many( pids )
    pids.each { |pid| kill pid }
end

#kill_reactorObject

Stops the Reactor.



171
172
173
174
175
# File 'lib/cuboid/processes/manager.rb', line 171

def kill_reactor
    Raktr.stop
rescue
    nil
end

#killallObject

Kills all processes.



165
166
167
168
# File 'lib/cuboid/processes/manager.rb', line 165

def killall
    kill_many @pids.dup
    @pids.clear
end

#preserve_outputObject

Overrides the default setting of discarding process outputs.



178
179
180
# File 'lib/cuboid/processes/manager.rb', line 178

def preserve_output
    @discard_output = false
end

#preserve_output?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/cuboid/processes/manager.rb', line 182

def preserve_output?
    !discard_output?
end

#resetObject



23
24
25
26
# File 'lib/cuboid/processes/manager.rb', line 23

def reset
    @pids           = []
    @discard_output = true
end

#spawn(executable, options = {}) ⇒ Integer

Returns PID of the process.

Parameters:

  • executable (String)

    Name of the executable Ruby script found in OptionGroups::Paths#executables without the ‘.rb’ extension.

  • options (Hash) (defaults to: {})

    Options to pass to the script – can be retrieved from ‘$options`.

Returns:

  • (Integer)

    PID of the process.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/cuboid/processes/manager.rb', line 202

def spawn( executable, options = {} )
    fail ArgumentError, 'Fork not supported.' if options.delete(:fork)

    stdin      = options.delete(:stdin)
    stdout     = options.delete(:stdout)
    stderr     = options.delete(:stderr)
    new_pgroup = options.delete(:new_pgroup)
    daemonize  = options.delete(:daemonize)

    spawn_options = {}

    if new_pgroup
        if Cuboid.windows?
            spawn_options[:new_pgroup] = new_pgroup
        else
            spawn_options[:pgroup] = new_pgroup
        end
    end

    spawn_options[:in]  = stdin  if stdin
    spawn_options[:out] = stdout if stdout
    spawn_options[:err] = stderr if stderr

    # `:detached` decouples "child outlives spawner" from
    # `:daemonize`, which only controls whether THIS thread
    # `waitpid`s. MCP and the spec helpers spawn with
    # `daemonize: true` but want the child tethered (die with
    # parent); only Agent-managed instances genuinely want to
    # outlive their spawner. Default = tethered; Agent's
    # `spawn_instance` opts out by passing `detached: true`.
    # base.rb's parent-death watchdog reads `$options[:ppid]`;
    # a missing ppid means "no tether" and the watchdog
    # short-circuits.
    detached = options.delete( :detached )
    options[:ppid]   = Process.pid if !detached
    options[:tmpdir] = Options.paths.tmpdir

    cuboid_options = Options.dup.update( options.delete(:options) || {} ).to_h
    encoded_cuboid_options = Base64.strict_encode64( Marshal.dump( cuboid_options ) )

    if executable.is_a? Symbol
        executable = "#{Options.paths.executables}/#{executable}.rb"
    elsif !File.exist?( executable )
        raise ArgumentError, "Executable does not exist: #{executable}"
    end

    encoded_options = Base64.strict_encode64( Marshal.dump( options ) )
    argv            = [executable, encoded_options]


    pid = nil
    SPAWN_RETRIES.times do |i|
        begin
            # It's very, **VERY** important that we use this argument format as
            # it bypasses the OS shell and we can thus count on a 1-to-1 process
            # creation and that the PID we get will be for the actual process.
            pid = Process.spawn(
                {
                    'CUBOID_SPAWN_OPTIONS' => encoded_cuboid_options
                },
                RbConfig.ruby,
                RUNNER,
                *(argv + [spawn_options])
            )
            break if alive? pid
            end
    end

    self << pid

    if !daemonize
        begin
            Process.waitpid( pid )
        rescue Errno::ECHILD
            @pids.delete pid
            return
        rescue Interrupt
            exit 0
        end
    end

    pid
end