Sha256: fed1e36511bab34b752afa29b411412108d0e6aab3a8ba9f726d9c03a17da83e

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module SrvManager
  class Process

    attr_reader :command
    attr_reader :id

    def initialize(command)
      @command = command
    end
    
    def start
      return if alive?
      command.rvm ? rvm_start : default_start
      @id = wait_for_pid(command.pidfile) if command.pidfile
      LOGGER.info "Started process #{@id}"
    end

    def stop
      send_signal 'TERM'
      LOGGER.info "Stoped process #{@id}"
      @id = nil
    end

    def kill
      send_signal 'KILL'
      LOGGER.info "Killed process #{@id}"
      @id = nil
    end

    def restart
      stop if alive?
      start
    end

    def started?
      !id.nil?
    end

    def alive?
      started? && ::Process.kill(0, id) ? true : false
    rescue Errno::ESRCH
      false
    end

    private

    def detault_start
      @id = ::Process.spawn command.env, 
                            command.text, 
                            chdir: command.dir, 
                            out: '/dev/null', 
                            err: '/dev/null'
      ::Process.detach @id
    end

    def rvm_start
      pid_file = File.expand_path "#{self.object_id}_#{Time.now.to_i}.pid", TMP_PATH
      rvm_pid = ::Process.spawn command.env.merge('SRV_COMMAND' => command.text, 'SRV_PIDFILE' => pid_file), 
                                RVM_RUNNER, 
                                chdir: command.dir, 
                                out: '/dev/null', 
                                err: '/dev/null'
      ::Process.detach rvm_pid
      @id = wait_for_pid pid_file, true
    end

    def send_signal(signal)
      return unless alive?
      begin
        ::Process.kill signal, @id
      rescue Errno::ESRCH
      end
    end

    def wait_for_pid(filename, delete=false)
      Timeout.timeout(60) do
        loop do
          if File.exists? filename
            pid = File.read(filename).to_i
            File.delete filename if delete
            return pid
          end
          sleep 0.1
        end
      end
    rescue Timeout::Error
      nil
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
srv_manager-0.0.7 lib/srv_manager/process.rb
srv_manager-0.0.6 lib/srv_manager/process.rb