Sha256: b3a668c6a4b68b43970071d68871b952f09e40268e3d66aad34344bdf90b97d3

Contents?: true

Size: 946 Bytes

Versions: 3

Compression:

Stored size: 946 Bytes

Contents

# frozen_string_literal: true

module Nonnative
  class Command < Nonnative::Service
    def initialize(service)
      @service = service
      @timeout = Nonnative::Timeout.new(service.timeout)
    end

    def name
      service.command
    end

    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 :service, :timeout, :pid

    def command_kill
      Process.kill('SIGINT', pid)
    end

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

    def command_exists?
      return false if pid.nil?

      Process.kill(0, pid)
      true
    rescue Errno::ESRCH
      false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nonnative-1.14.0 lib/nonnative/command.rb
nonnative-1.13.0 lib/nonnative/command.rb
nonnative-1.12.0 lib/nonnative/command.rb