Sha256: d0d5f23f8da123f9688f0e427ba0b578d868e39205d768cf5216c97035a705c4

Contents?: true

Size: 1.58 KB

Versions: 43

Compression:

Stored size: 1.58 KB

Contents

module PgEngine
  class HealthController < ApplicationController
    rescue_from(Exception) do |error|
      pg_err error
      render_down
    end

    def show
      check_redis
      check_postgres
      check_websocket
      render_up
    end

    private

    def check_postgres
      return if User.count.is_a? Integer

      raise PgEngine::Error, 'postgres is down'
    end

    def check_redis
      return if Kredis.counter('healthcheck').increment.is_a? Integer

      raise PgEngine::Error, 'redis is down'
    end

    # rubocop:disable Metrics/MethodLength
    def check_websocket
      result = nil
      begin
        Timeout.timeout(5) do
          EM.run do
            url = Rails.application.config.action_cable.url
            ws = Faye::WebSocket::Client.new(url)

            ws.on :message do |event|
              type = JSON.parse(event.data)['type']
              if type == 'welcome'
                result = :success
                ws.close
                EM.stop
              end
            end
          end
        end
      rescue Timeout::Error
        raise PgEngine::Error, 'websocket server is down'
      end

      return if result == :success

      raise PgEngine::Error, 'websocket server is down'
    end
    # rubocop:enable Metrics/MethodLength

    def render_up
      render html: html_status(color: '#005500')
    end

    def render_down
      render html: html_status(color: '#990000'), status: :internal_server_error
    end

    def html_status(color:)
      %(<!DOCTYPE html><html><body style="background-color: #{color}"></body></html>).html_safe
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
pg_rails-7.6.27 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.26 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.25 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.24 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.24.pre.5 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.24.pre.4 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.24.pre.3 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.23 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.22 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.22.pre.3 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.22.pre.2 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.22.pre.1 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.11 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.10 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.9 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.8 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.7 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.6 pg_engine/app/controllers/pg_engine/health_controller.rb
pg_rails-7.6.21.pre.5 pg_engine/app/controllers/pg_engine/health_controller.rb