Sha256: 74ed73b0a2a0d0a6e8e425db25afd861e36f598dfe4bd0e67125ea88651916cc

Contents?: true

Size: 1.98 KB

Versions: 12

Compression:

Stored size: 1.98 KB

Contents

require "thread"

module Bugsnag
  module Delivery
    class ThreadQueue < Synchronous
      MAX_OUTSTANDING_REQUESTS = 100
      STOP = Object.new
      MUTEX = Mutex.new

      class << self
        ##
        # Queues a given payload to be delivered asynchronously
        #
        # @param url [String]
        # @param get_payload [Proc] A Proc that will return the payload.
        # @param configuration [Bugsnag::Configuration]
        # @param options [Hash]
        # @return [void]
        def serialize_and_deliver(url, get_payload, configuration, options={})
          @configuration = configuration

          start_once!

          if @queue.length > MAX_OUTSTANDING_REQUESTS
            @configuration.warn("Dropping notification, #{@queue.length} outstanding requests")
            return
          end

          # Add delivery to the worker thread
          @queue.push(proc do
            begin
              payload = get_payload.call
            rescue StandardError => e
              configuration.error("Unable to send information to Bugsnag (#{url}), #{e.inspect}")
              configuration.error(e.backtrace)
            end

            Synchronous.deliver(url, payload, configuration, options) unless payload.nil?
          end)
        end

        private

        def start_once!
          MUTEX.synchronize do
            @started = nil unless defined?(@started)
            return if @started == Process.pid
            @started = Process.pid

            @queue = Queue.new

            worker_thread = Thread.new do
              while x = @queue.pop
                break if x == STOP
                x.call
              end
            end

            at_exit do
              @configuration.warn("Waiting for #{@queue.length} outstanding request(s)") unless @queue.empty?
              @queue.push STOP
              worker_thread.join
            end
          end
        end
      end
    end
  end
end

Bugsnag::Delivery.register(:thread_queue, Bugsnag::Delivery::ThreadQueue)

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bugsnag-6.26.4 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.26.3 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.26.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.26.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.26.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.25.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.25.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.25.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.24.2 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.24.1 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.24.0 lib/bugsnag/delivery/thread_queue.rb
bugsnag-6.23.0 lib/bugsnag/delivery/thread_queue.rb