Sha256: d4f1143de9b0ec4b90d8e2030603ff690da6c2edf312f7652df1c153043a33b9

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 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.
      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

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

      private

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

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

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
qiita_org-0.1.35 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.34 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.33 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.32 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.31 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.30 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.29 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.28 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/thread.rb
listen-3.3.1 lib/listen/thread.rb
listen-3.3.0 lib/listen/thread.rb