# frozen_string_literal: true # # Return the status of the server # class StatusController < ActionController::Base protect_from_forgery with: :exception # # Main (and only) page # def index components = { mongo: mongo_status, redis: redis_status, jobs: delayed_jobs_status, cron_job_servers: cron_job_servers_status, workers: workers_status } respond_to do |format| format.html { render :index, locals: { components: ui_decorators(components) }, layout: false } format.json { render json: components.to_json } format.text { render plain: components.flatten, status: text_status(components) } end end private # # Return the status of the key components, mongo, redis # def text_status(components) components[:mongo][:success] && components[:redis][:success] ? 200 : 500 rescue StandardError 500 end # # Add UI decorators for HTML formatting # def ui_decorators(components) components.each do |key, values| if values[:success] components[key][:icon_name] = 'check_circle' components[key][:icon_color] = 'green-text' else components[key][:icon_name] = 'error' components[key][:icon_color] = 'red-text' end end end # # Report an error for the check # def report_error(error, metrics = {}) metrics.merge(success: false, message: error.message) end # # Report a success for the check # def report_success(message, metrics = {}) metrics.merge(success: true, message: message) end # # Mongo DB status # def mongo_status raise 'Mongo not available' if SystemConfiguration.count.zero? report_success "#{SystemConfiguration.count} Configs", count: SystemConfiguration.count rescue StandardError => error report_error error, count: 0 end # # Redis DB Status # def redis_status value = Time.now.to_f Rails.cache.write 'redis-status-check', value raise 'Redis not available' unless value.eql?(Rails.cache.fetch('redis-status-check')) report_success 'Redis Available', count: 1 rescue StandardError => error report_error(error, count: 0) end # # Job Count # def delayed_jobs_status count = Delayed::Backend::Mongoid::Job.count report_success "#{count} Jobs", count: count rescue StandardError => error report_error(error) end # # Cron job primary status # def cron_job_servers_status server = cron_job_server raise 'No primary server' if server.blank? server_info = "#{server.host_name}(#{server.pid}), last check in #{server.last_check_in_at}" raise "Primary Server is DEAD: #{server_info}" if server.dead? report_success "Primary Server is alive: #{server_info}", primary_count: 1, total_count: Cron::Server.count rescue StandardError => error report_error(error, primary_count: 0, total_count: Cron::Server.count) end # # Desired and current worker status for servers # def workers_status server = cron_job_server raise 'No primary server' if server.blank? || server.dead? report_success "Workers #{server.desired_server_count}/ #{server.current_server_count}", desired_count: server.desired_server_count, current_count: server.current_server_count rescue StandardError => error report_error(error, desired_count: 0, current_count: 0) end def cron_job_server @cron_job_server ||= Cron::Server.primary_server end end