Sha256: e46adf99894646482027678ab2468f949aa7101e87f8e61129930e9bbdc02ea3

Contents?: true

Size: 1.34 KB

Versions: 17

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Nonnative
  class Process < Runner
    def initialize(service)
      super service

      @timeout = Nonnative::Timeout.new(service.timeout)
    end

    def start
      unless process_exists?
        proxy.start
        @pid = process_spawn
        wait_start
      end

      pid
    end

    def stop
      if process_exists?
        process_kill
        proxy.stop
        wait_stop
      end

      pid
    end

    def memory
      return if pid.nil?

      @memory ||= GetProcessMem.new(pid)
    end

    protected

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

    private

    attr_reader :pid, :timeout

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

    def process_spawn
      environment = service.environment || {}
      environment = environment.transform_keys(&:to_s).transform_values(&:to_s)

      environment.each_key do |k|
        environment[k] = ENV.fetch(k, nil) || environment[k]
      end

      spawn(environment, service.command.call, %i[out err] => [service.log, 'a'])
    end

    def process_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

17 entries across 17 versions & 1 rubygems

Version Path
nonnative-1.74.1 lib/nonnative/process.rb
nonnative-1.74.0 lib/nonnative/process.rb
nonnative-1.73.0 lib/nonnative/process.rb
nonnative-1.72.0 lib/nonnative/process.rb
nonnative-1.71.0 lib/nonnative/process.rb
nonnative-1.70.0 lib/nonnative/process.rb
nonnative-1.69.0 lib/nonnative/process.rb
nonnative-1.68.0 lib/nonnative/process.rb
nonnative-1.67.0 lib/nonnative/process.rb
nonnative-1.66.0 lib/nonnative/process.rb
nonnative-1.65.0 lib/nonnative/process.rb
nonnative-1.64.0 lib/nonnative/process.rb
nonnative-1.63.0 lib/nonnative/process.rb
nonnative-1.62.0 lib/nonnative/process.rb
nonnative-1.61.0 lib/nonnative/process.rb
nonnative-1.60.0 lib/nonnative/process.rb
nonnative-1.59.1 lib/nonnative/process.rb