Skip to content
Snippets Groups Projects
Unverified Commit 620f6d5e authored by Samuel Couillard's avatar Samuel Couillard Committed by GitHub
Browse files

Add HealthChecksController (#5199)

* Add HealthChecks

* rubo

* Improve specs

* rspec
parent 43dd29b2
No related branches found
No related tags found
No related merge requests found
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with Greenlight; if not, see <http://www.gnu.org/licenses/>.
# frozen_string_literal: true
class HealthChecksController < ApplicationController
skip_before_action :verify_authenticity_token
def check
response = 'success'
begin
check_database unless ENV.fetch('DATABASE_HEALTH_CHECK_DISABLED', false) == 'true'
check_redis unless ENV.fetch('REDIS_HEALTH_CHECK_DISABLED', false) == 'true'
check_smtp unless ENV.fetch('SMTP_HEALTH_CHECK_DISABLED', false) == 'true'
check_big_blue_button unless ENV.fetch('BBB_HEALTH_CHECK_DISABLED', false) == 'true'
end
render plain: response, status: :ok
rescue StandardError => e
render plain: e, status: :internal_server_error
end
private
def check_database
ActiveRecord::Base.establish_connection # Establishes connection
ActiveRecord::Base.connection # Calls connection object
raise 'Unable to connect to Database' unless ActiveRecord::Base.connected?
raise 'Unable to connect to Database - pending migrations' unless ActiveRecord::Migration.check_pending!.nil?
rescue StandardError => e
raise "Unable to connect to Database - #{e}"
end
def check_redis
Redis.new.ping
rescue StandardError => e
raise "Unable to connect to Redis - #{e}"
end
def check_smtp
settings = ActionMailer::Base.smtp_settings
smtp = Net::SMTP.new(settings[:address], settings[:port])
smtp.enable_starttls_auto if settings[:openssl_verify_mode] != OpenSSL::SSL::VERIFY_NONE && smtp.respond_to?(:enable_starttls_auto)
if settings[:authentication].present? && settings[:authentication] != 'none'
smtp.start(settings[:domain]) do |s|
s.authenticate(settings[:user_name], settings[:password], settings[:authentication])
end
else
smtp.start(settings[:domain])
end
smtp.finish
rescue StandardError => e
raise "Unable to connect to SMTP Server - #{e}"
end
def check_big_blue_button
checksum = Digest::SHA1.hexdigest("getMeetings#{Rails.configuration.bigbluebutton_secret}")
uri = URI("#{Rails.configuration.bigbluebutton_endpoint}getMeetings?checksum=#{checksum}")
res = Net::HTTP.get(uri)
doc = Nokogiri::XML(res)
raise "Unable to connect to BigBlueButton - #{res}" unless doc.css('returncode').text == 'SUCCESS'
rescue StandardError => e
raise "Unable to connect to BigBlueButton - #{e}"
end
end
......@@ -25,6 +25,9 @@ Rails.application.routes.draw do
get '/meeting_ended', to: 'external#meeting_ended'
post '/recording_ready', to: 'external#recording_ready'
# Health checks
get '/health_checks', to: 'health_checks#check'
# All the Api endpoints must be under /api/v1 and must have an extension .json.
namespace :api do
namespace :v1 do
......
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with Greenlight; if not, see <http://www.gnu.org/licenses/>.
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe HealthChecksController, type: :controller do
describe '#check' do
# Disable all checks initially
before do
allow(ENV).to receive(:fetch).with('DATABASE_HEALTH_CHECK_DISABLED', false).and_return('true')
allow(ENV).to receive(:fetch).with('REDIS_HEALTH_CHECK_DISABLED', false).and_return('true')
allow(ENV).to receive(:fetch).with('SMTP_HEALTH_CHECK_DISABLED', false).and_return('true')
allow(ENV).to receive(:fetch).with('BBB_HEALTH_CHECK_DISABLED', false).and_return('true')
end
context 'when all services are disabled' do
it 'returns success' do
get :check
expect(response.body).to eq('success')
expect(response).to have_http_status(:ok)
end
end
context 'when database check is enabled' do
before do
allow(ENV).to receive(:fetch).with('DATABASE_HEALTH_CHECK_DISABLED', false).and_return(false)
end
it 'returns success' do
allow(ActiveRecord::Base).to receive(:connected?).and_return(true)
get :check
expect(response.body).to eq('success')
expect(response).to have_http_status(:ok)
end
it 'returns failure message' do
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
get :check
expect(response.body).to include('Unable to connect to Database')
expect(response).to have_http_status(:internal_server_error)
end
end
context 'when redis check is enabled' do
before do
allow(ENV).to receive(:fetch).with('REDIS_HEALTH_CHECK_DISABLED', false).and_return(false)
end
it 'returns success' do
allow_any_instance_of(Redis).to receive(:ping)
get :check
expect(response.body).to eq('success')
expect(response).to have_http_status(:ok)
end
it 'returns failure message' do
allow(Redis).to receive(:new).and_raise(StandardError.new('Redis connection error'))
get :check
expect(response.body).to include('Unable to connect to Redis')
expect(response).to have_http_status(:internal_server_error)
end
end
context 'when smtp check is enabled' do
before do
allow(ENV).to receive(:fetch).with('SMTP_SENDER_EMAIL', nil).and_return('test@test.test')
allow(ENV).to receive(:fetch).with('SMTP_HEALTH_CHECK_DISABLED', false).and_return(false)
end
it 'returns failure message' do
allow(Net::SMTP).to receive(:new).and_raise(StandardError.new('SMTP error'))
get :check
expect(response.body).to include('Unable to connect to SMTP Server')
expect(response).to have_http_status(:internal_server_error)
end
end
context 'when big_blue_button check is enabled' do
before do
allow(ENV).to receive(:fetch).with('BBB_HEALTH_CHECK_DISABLED', false).and_return(false)
end
it 'returns failure message' do
allow(Net::HTTP).to receive(:get).and_raise(StandardError.new('BBB error'))
get :check
expect(response.body).to include('Unable to connect to BigBlueButton')
expect(response).to have_http_status(:internal_server_error)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment