Sha256: c3c2dfac0f7c885d5dc1c9554853379397e05c06290e295d8387022289dab6f7

Contents?: true

Size: 1.55 KB

Versions: 89

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'thread'

require_relative 'logger'

module Listen
  module Thread
    class << self
      # Creates a new thread with the given name.
      # Any exceptions raised by the thread will be logged with the thread name and complete backtrace.
      # rubocop:disable Style/MultilineBlockChain
      def new(name, &block)
        thread_name = "listen-#{name}"
        caller_stack = caller

        ::Thread.new do
          rescue_and_log(thread_name, caller_stack: caller_stack, &block)
        end.tap do |thread|
          thread.name = thread_name
        end
      end
      # rubocop:enable Style/MultilineBlockChain

      def rescue_and_log(method_name, *args, caller_stack: nil)
        yield(*args)
      rescue => exception
        _log_exception(exception, method_name, caller_stack: caller_stack)
      end

      private

      def _log_exception(exception, thread_name, caller_stack: nil)
        complete_backtrace = if caller_stack
          [*exception.backtrace, "--- Thread.new ---", *caller_stack]
        else
          exception.backtrace
        end
        message = "Exception rescued in #{thread_name}:\n#{_exception_with_causes(exception)}\n#{complete_backtrace * "\n"}"
        Listen.logger.error(message)
      end

      def _exception_with_causes(exception)
        result = +"#{exception.class}: #{exception}"
        if exception.cause
          result << "\n"
          result << "--- Caused by: ---\n"
          result << _exception_with_causes(exception.cause)
        end
        result
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 6 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
listen-3.9.0 lib/listen/thread.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/listen-3.8.0/lib/listen/thread.rb