Class: Warren::FrameworkAdaptor::RailsAdaptor

Inherits:
Object
  • Object
show all
Defined in:
lib/warren/framework_adaptor/rails_adaptor.rb

Overview

The RailsAdaptor provides error handling and application loading for Rails applications

Defined Under Namespace

Classes: ConnectionMissing

Instance Method Summary collapse

Instance Method Details

#envActiveSupport::StringInquirer

Returns the rails environment

Returns:

  • (ActiveSupport::StringInquirer)

    The rails environment



109
110
111
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 109

def env
  Rails.env
end

#handleVoid

Checks ensures a database connection has been checked out before yielding to allow message processing. Rescues loss of the database connection and raises Exceptions::TemporaryIssue to send the consumers to sleep until it recovers.

Returns:

  • (Void)


86
87
88
89
90
91
92
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 86

def handle
  with_connection do
    yield
  rescue ConnectionMissing => e
    raise Warren::Exceptions::TemporaryIssue, e.message
  end
end

#load_applicationVoid

Triggers full loading of the rails application and dependencies

Returns:

  • (Void)


123
124
125
126
127
128
129
130
131
132
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 123

def load_application
  $stdout.puts 'Loading application...'
  require './config/environment'
  Warren.load_configuration
  $stdout.puts 'Loaded!'
rescue LoadError
  # Need to work out an elegant way to handle non-rails
  # apps
  $stdout.puts 'Could not auto-load application'
end

#loggerLogger, ...

Returns the configured logger

Returns:

  • (Logger, ActiveSupport::Logger, ...)

    The application logger



116
117
118
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 116

def logger
  Rails.logger
end

#recovered?Bool

Checks that the database has recovered to allow message processing

Returns:

  • (Bool)

    Returns true if the application has recovered



71
72
73
74
75
76
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 71

def recovered?
  ActiveRecord::Base.connection.reconnect!
  true
rescue StandardError
  false
end

#with_connectionObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/warren/framework_adaptor/rails_adaptor.rb', line 94

def with_connection
  begin
    ActiveRecord::Base.connection
  rescue StandardError => e
    raise Warren::Exceptions::TemporaryIssue, e.message
  end

  yield
ensure
  ActiveRecord::Base.clear_active_connections!
end