Sha256: a8b03bc0bc1562eb9b27aa4492b1e9cecae6d2c9795b086341f49110f22cd7f9

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

require "timeout"

class ExternalSneaker
  attr_accessor :worker_pid, :start_command, :workers

  def initialize(start_command, *workers)
    fail ArgumentError, "start_command was expected" if start_command.nil?
    self.workers = workers.map(&:to_s).join(",")
    self.start_command = start_command
  end

  def start
    puts "Trying to start #{start_command}..."
    self.worker_pid = fork do
      start_child
    end

    at_exit do
      stop_child
    end
  end

  private

  def start_child
    exec({ "RAILS_ENV" => Rails.env, "WORKERS" => workers }, start_command)
  end

  def stop_child # rubocop:disable AbcSize
    puts "Trying to stop #{start_command}, pid: #{worker_pid}"

    # send TERM and wait for exit
    Process.kill("TERM", worker_pid)

    begin
      Timeout.timeout(10) do
        Process.waitpid(worker_pid)
        puts "Process #{start_command} stopped successfully"
      end
    rescue Timeout::Error
      # Kill process if could not exit in 10 seconds
      puts "Sending KILL signal to #{start_command}, pid: #{worker_pid}"
      Process.kill("KILL", worker_pid)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sapience-1.0.11 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.10 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.9 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.8 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.7 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.6 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.5 test_apps/rails/lib/external_sneaker.rb
sapience-1.0.4 test_apps/rails/lib/external_sneaker.rb