GithubHelp home page GithubHelp logo

production_rails's Introduction

Production Rails

Best practices for running Rails in production

Error Reporting

Use an error reporting service like Rollbar.

gem 'rollbar'

Run:

rails generate rollbar

And add the following to your environment.

ROLLBAR_ACCESS_TOKEN=test123

Timeouts

Use Rack Timeout.

gem 'rack-timeout', '>= 0.1.0beta3'

Add the following to config/initializers/timeout.rb.

Rack::Timeout.timeout = ENV["RACK_TIMEOUT"] || 5
Rack::Timeout.unregister_state_change_observer(:logger)

# hack to bubble timeout errors
module ActionView
  class Template

    def handle_render_error_with_timeout(view, e)
      raise e if e.is_a?(Rack::Timeout::Error)
      handle_render_error_without_timeout(view, e)
    end
    alias_method_chain :handle_render_error, :timeout

  end
end

Show a custom error page for timeouts. Add the following to app/controllers/application_controller.rb.

# from https://github.com/heroku/rack-timeout/issues/40#issuecomment-51865104
rescue_from Rack::Timeout::Error, with: :handle_request_timeout

private

def handle_request_timeout(e)
  Rollbar.report_exception(e)
  respond_to do |format|
    format.html { render file: Rails.root.join("public/503.html"), status: 503, layout: nil }
    format.all { head 503 }
  end
end

And create a public/503.html.

Logging

Use Lograge.

gem 'lograge'

Add the following to config/environments/production.rb.

config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
  event.payload.slice(:user_id, :visit_id)
end

Add the following to app/controllers/application_controller.rb.

def append_info_to_payload(payload)
  super
  payload[:user_id] = current_user.id if current_user
  payload[:visit_id] = ahoy.visit_id # if you use Ahoy
end

Uptime Monitoring

Use an uptime monitoring service like Pingdom or Uptime Robot.

Monitor web servers, background jobs, and scheduled tasks.

Performance Monitoring

Use a performance monitoring service like New Relic or AppSignal.

Web Server

Use a high performance web server like Unicorn.

gem 'unicorn'

Security

Use SSL to protect your users. Add the following to config/environments/production.rb.

config.force_ssl = true

production_rails's People

Contributors

ankane avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.