Class: Libuv::Spawn

Inherits:
Handle show all
Defined in:
lib/libuv/spawn.rb

Constant Summary

Constants included from Assertions

Assertions::MSG_NO_PROC

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary collapse

Attributes inherited from Handle

#closed, #reactor, #storage

Attributes inherited from Q::Promise

#trace

Instance Method Summary collapse

Methods inherited from Handle

#active?, #close, #closed?, #closing?, #ref, #unref

Methods included from Assertions

#assert_block, #assert_boolean, #assert_type

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#resolved?, #then

Methods inherited from Q::Promise

#catch, #finally, #progress, #ruby_catch, #value

Constructor Details

#initialize(reactor, cmd, working_dir: '.', args: [], env: nil, flags: 0, mode: :capture) ⇒ Spawn

Returns a new instance of Spawn.

Parameters:

  • reactor (::Libuv::Reactor)

    reactor this timer will be associated

  • callback (Proc)

    callback to be called when the timer is triggered



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/libuv/spawn.rb', line 11

def initialize(reactor, cmd, working_dir: '.', args: [], env: nil, flags: 0, mode: :capture)
    @reactor = reactor

    process_ptr = ::Libuv::Ext.allocate_handle_process
    @options = Ext::UvProcessOptions.new

    # Configure IO objects
    @io_obj = Ext::StdioObjs.new
    case mode.to_sym
    when :capture
        @stdin  = @reactor.pipe
        @stdout = @reactor.pipe
        @stderr = @reactor.pipe
        @io_obj[:stdin] = build_stdio(:CREATE_READABLE_PIPE, pipe: @stdin)
        @io_obj[:stdout] = build_stdio(:CREATE_WRITABLE_PIPE, pipe: @stdout)
        @io_obj[:stderr] = build_stdio(:CREATE_WRITABLE_PIPE, pipe: @stderr)
    when :ignore
        @io_obj[:stdin] = build_stdio(:UV_IGNORE)
        @io_obj[:stdout] = build_stdio(:UV_IGNORE)
        @io_obj[:stderr] = build_stdio(:UV_IGNORE)
    when :inherit
        @io_obj[:stdin] = build_stdio(:UV_INHERIT_FD, fd: ::STDIN.fileno)
        @io_obj[:stdout] = build_stdio(:UV_INHERIT_FD, fd: ::STDOUT.fileno)
        @io_obj[:stderr] = build_stdio(:UV_INHERIT_FD, fd: ::STDERR.fileno)
    end

    # Configure arguments
    @cmd = FFI::MemoryPointer.from_string(cmd)
    @args = Array(args).map { |arg| FFI::MemoryPointer.from_string(arg) }
    @args.unshift(@cmd)
    @args_ptr = FFI::MemoryPointer.new(:pointer, @args.length + 1)
    @args_ptr.write_array_of_pointer(@args)

    # Configure environment
    if env
        @env = Array(env).map { |e| FFI::MemoryPointer.from_string(e) }
        @env_ptr = FFI::MemoryPointer.new(:pointer, @env.length + 1)
        @env_ptr.write_array_of_pointer(@env)
    end

    @working_dir = FFI::MemoryPointer.from_string(working_dir)

    # Apply the options
    @options[:exit_cb] = callback(:on_exit, process_ptr.address)
    @options[:file] = @cmd
    @options[:args] = @args_ptr
    @options[:env] = @env_ptr
    @options[:cwd] = @working_dir
    @options[:flags] = 0
    @options[:stdio_count] = 3
    @options[:stdio] = @io_obj

    error = check_result(::Libuv::Ext.spawn(reactor.handle, process_ptr, @options))
    super(process_ptr, error)
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/libuv/spawn.rb', line 7

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



7
8
9
# File 'lib/libuv/spawn.rb', line 7

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



7
8
9
# File 'lib/libuv/spawn.rb', line 7

def stdout
  @stdout
end

Instance Method Details

#kill(signal = 2) ⇒ Object



67
68
69
70
71
# File 'lib/libuv/spawn.rb', line 67

def kill(signal = 2)
    return self if @closed
    ::Libuv::Ext.process_kill(handle, signal)
    self
end