Sha256: f0c3f299dab4893db6584a9761c3a539430bde69c42f9b42c160570376a28644

Contents?: true

Size: 875 Bytes

Versions: 10

Compression:

Stored size: 875 Bytes

Contents

# frozen_string_literal: true

module Nonnative
  class Command < Nonnative::Service
    def start
      unless command_exists?
        @pid = command_spawn
        wait_start
      end

      pid
    end

    def stop
      if command_exists?
        command_kill
        wait_stop
      end

      pid
    end

    protected

    def wait_stop
      timeout.perform do
        Process.waitpid2(pid)
      end
    end

    private

    attr_reader :pid

    def command_kill
      signal = Signal.list[service.signal || 'INT'] || Signal.list['INT']
      Process.kill(signal, pid)
    end

    def command_spawn
      spawn(service.command, %i[out err] => [service.file, 'a'])
    end

    def command_exists?
      return false if pid.nil?

      signal = Signal.list['EXIT']
      Process.kill(signal, pid)
      true
    rescue Errno::ESRCH
      false
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nonnative-1.24.0 lib/nonnative/command.rb
nonnative-1.23.0 lib/nonnative/command.rb
nonnative-1.22.0 lib/nonnative/command.rb
nonnative-1.21.0 lib/nonnative/command.rb
nonnative-1.20.0 lib/nonnative/command.rb
nonnative-1.19.0 lib/nonnative/command.rb
nonnative-1.18.0 lib/nonnative/command.rb
nonnative-1.17.0 lib/nonnative/command.rb
nonnative-1.16.0 lib/nonnative/command.rb
nonnative-1.15.0 lib/nonnative/command.rb