Sha256: 1b67f71138d4e8dfba15546f640aead7e350d673e9ca1c4a33c7195ea6155465

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

require 'main_loop/handler'

module MainLoop

  class ThreadHandler < MainLoop::Handler

    attr_reader :thread

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

      run(&block) if block_given?
    end

    def id
      @thread&.object_id.to_s
    end

    def reap(status)
      logger.info "Thread[#{name}] exited: thread:#{@thread} Status:#{status}"
      @thread = nil
      @finished = true
      @success = false

      return if terminating?

      handle_retry
    end

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

      if terminating?
        @success = false
        logger.info "Thread[#{name}] send force terminate: KILL thread:#{@thread}"
        @thread.kill rescue nil
      else
        @terminating_at ||= Time.now
        logger.info "Thread[#{name}] send terminate: thread:#{@thread}"
      end
    end

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

      @success = false
      logger.info "Thread[#{name}] send kill: thread:#{@thread}"
      @thread.kill rescue nil
    end

    def run(&block)
      return if terminating?

      @block = block
      start_thread(&@block)
    end

    protected

      def start_thread
        @thread = Thread.new do
          yield(self)
        ensure
          publish("reap:#{id}:exited")
        end
        @finished = false
        logger.info "Thread[#{name}] created: thread:#{@thread}"
      end


  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
main_loop-0.1.2.16824 lib/main_loop/thread_handler.rb
main_loop-0.1.1.16822 lib/main_loop/thread_handler.rb
main_loop-0.1.1.16821 lib/main_loop/thread_handler.rb