Class: ActionSubscriber::Middleware::ErrorHandler

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/action_subscriber/middleware/error_handler.rb

Instance Method Summary collapse

Methods included from Logging

initialize_logger, logger, #logger, logger=

Constructor Details

#initialize(app) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



6
7
8
# File 'lib/action_subscriber/middleware/error_handler.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_subscriber/middleware/error_handler.rb', line 10

def call(env)
  job_mutex = ::Mutex.new
  job_complete = ::ConditionVariable.new

  job_mutex.synchronize do
    ::Thread.new do
      job_mutex.synchronize do
        begin
          @app.call(env)
        rescue => error
          logger.error "FAILED #{env.message_id}"
          ::ActionSubscriber.configuration.error_handler.call(error, env.to_h)
        ensure
          job_complete.signal
        end
      end
    end

    # TODO we might want to pass a timeout to this wait so we can handle jobs that get frozen
    job_complete.wait(job_mutex)
  end
end