Sha256: bab30ea98960b03089ad0c830193de0260b51a650000f242605a1c618fcf488b

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

require 'timeout'

module Rollbar
  module Delay
    class Thread
      EXIT_SIGNAL  = :exit
      EXIT_TIMEOUT = 6

      Error        = Class.new(StandardError)
      TimeoutError = Class.new(Error)

      class << self
        attr_reader :reaper

        def call(payload)
          spawn_threads_reaper

          thread = new.call(payload)
          threads << thread
          thread
        end

        private

        def threads
          @threads ||= Queue.new
        end

        def spawn_threads_reaper
          return if @spawned

          @spawned = true

          @reaper ||= build_reaper_thread
          configure_exit_handler
        end

        def build_reaper_thread
          ::Thread.start do
            loop do
              thread = threads.pop

              break if thread == EXIT_SIGNAL

              thread.join
            end
          end
        end

        def configure_exit_handler
          at_exit do
            begin
              Timeout.timeout(EXIT_TIMEOUT) do
                threads << EXIT_SIGNAL
                reaper.join
              end
            rescue Timeout::Error
              raise TimeoutError, "unable to reap all threads within #{EXIT_TIMEOUT} seconds"
            end
          end
        end
      end # class << self

      def call(payload)
        ::Thread.new do
          begin
            Rollbar.process_from_async_handler(payload)
          rescue StandardError
            # Here we swallow the exception:
            # 1. The original report wasn't sent.
            # 2. An internal error was sent and logged
            #
            # If users want to handle this in some way they
            # can provide a more custom Thread based implementation
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rollbar-2.22.1 lib/rollbar/delay/thread.rb
rollbar-2.22.0 lib/rollbar/delay/thread.rb
rollbar-2.21.0 lib/rollbar/delay/thread.rb
rollbar-2.20.2 lib/rollbar/delay/thread.rb
rollbar-2.20.1 lib/rollbar/delay/thread.rb
rollbar-2.20.0 lib/rollbar/delay/thread.rb
rollbar-2.19.4 lib/rollbar/delay/thread.rb
rollbar-2.19.3 lib/rollbar/delay/thread.rb