HealthCheckController.test_smtp()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 14
rs 9.7
c 1
b 0
f 0
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
class HealthCheckController < ApplicationController
20
  skip_before_action :redirect_to_https, :set_user_domain, :set_user_settings, :maintenance_mode?, :migration_error?,
21
  :user_locale, :check_admin_password, :check_user_role
22
23
  # GET /health_check
24
  def all
25
    response = "success"
26
    @cache_expire = 10.seconds
27
28
    begin
29
      cache_check
30
      database_check
31
      email_check
32
    rescue => e
33
      response = "Health Check Failure: #{e}"
34
    end
35
36
    render plain: response
37
  end
38
39
  private
40
41
  def cache_check
42
    if Rails.cache.write("__health_check_cache_write__", "true", expires_in: @cache_expire)
43
      raise "Unable to read from cache" unless Rails.cache.read("__health_check_cache_write__") == "true"
44
    else
45
      raise "Unable to write to cache"
46
    end
47
  end
48
49
  def database_check
50
    if defined?(ActiveRecord)
51
      raise "Database not responding" unless ActiveRecord::Migrator.current_version
52
    end
53
    raise "Pending migrations" unless ActiveRecord::Migration.check_pending!.nil?
54
  end
55
56
  def email_check
57
    test_smtp if Rails.configuration.action_mailer.delivery_method == :smtp
58
  end
59
60
  def test_smtp
61
    settings = ActionMailer::Base.smtp_settings
62
63
    smtp = Net::SMTP.new(settings[:address], settings[:port])
64
    if settings[:enable_starttls_auto] == "true"
65
      smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
66
    end
67
68
    smtp.start(settings[:domain]) do |s|
69
      s.authenticate(settings[:user_name], settings[:password], settings[:authentication])
70
    end
71
  rescue => e
72
    raise "Unable to connect to SMTP Server - #{e}"
73
  end
74
end
75