Sha256: a21e6bae299d1715501d23b49c55d13e7eddc25e227116f3a36ab87d7f078272

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

require 'main_loop/handler'

module MainLoop
  class ProcessHandler < MainLoop::Handler

    attr_reader :pid

    def initialize(dispatcher, name, **kwargs, &block)
      super
      @handler_type = 'Process'
      @pid = nil
      dispatcher.add_handler(self)

      run(&block) if block_given?
    end

    def id
      @pid
    end

    def reap(status)
      logger.info "Process[#{name}] exited: Pid:#{@pid} Status: #{status.exitstatus.inspect} Termsig: #{status.termsig.inspect} Success: #{status.success?}"
      @pid = nil
      @finished = true
      @success = !!status.success?

      return if terminating?

      handle_retry
    end

    def term
      unless @pid
        @terminating_at ||= Time.now
        logger.debug "Process[#{name}] alredy terminated. Skipped."
        return
      end

      if terminating?
        @success = false
        logger.info "Process[#{name}] send force terminate: KILL Pid:#{@pid}"
        ::Process.kill('KILL', @pid) rescue nil
      else
        @terminating_at ||= Time.now
        logger.info "Process[#{name}] send terminate: Pid:#{@pid}"
        ::Process.kill('TERM', @pid) rescue nil
      end
    end

    def kill
      unless @pid
        logger.debug "Process[#{name}] alredy Killed. Skipped."
        return
      end

      @success = false
      logger.info "Process[#{name}] send kill: Pid:#{@pid}"
      ::Process.kill('KILL', @pid) rescue nil
    end

    def run(&block)
      return if terminating?

      @block = block
      start_fork(&@block)
    end

    protected

      def start_fork
        @pid = Kernel.fork do
          yield
        end
        @finished = false
        logger.info "Process[#{name}] created: Pid:#{@pid}"
      end


  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
main_loop-0.1.2.16854 lib/main_loop/process_handler.rb
main_loop-0.1.2.16825 lib/main_loop/process_handler.rb
main_loop-0.1.2.16824 lib/main_loop/process_handler.rb
main_loop-0.1.1.16822 lib/main_loop/process_handler.rb
main_loop-0.1.1.16821 lib/main_loop/process_handler.rb