Class: OpenStax::Utilities::StatusController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/openstax/utilities/status_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/openstax/utilities/status_controller.rb', line 8

def index
  @application_name = OSU::SITE_NAME.humanize
  @environment_name = OpenStax::Utilities.configuration.environment_name

  @statuses = if @environment_name == 'development'
    [ [ 'local', 1, 1, :ok ] ]
  else
    queues = Module.const_defined?(:Delayed) && Delayed.const_defined?(:Worker) ?
      Delayed::Worker.queue_attributes.keys.map(&:to_s) - [ 'migration' ] : []
    asgs = [ 'migration', 'web', 'cron', 'background' ] + queues.map do |queue|
      "background-#{queue}"
    end

    Aws::AutoScaling::Client.new.describe_auto_scaling_groups(
      auto_scaling_group_names: asgs.map do |asg|
        "#{@environment_name}-#{@application_name.downcase}-#{asg}-asg"
      end
    ).auto_scaling_groups.map do |asg|
      name = asg.auto_scaling_group_name.sub(
        "#{@environment_name}-#{@application_name.downcase}-", ''
      ).chomp('-asg')

      status = if asg.desired_capacity == 0
        if [ 'web', 'background', 'cron' ].include?(name)
          :configuration_error
        elsif asg.instances.size > 0
          :shutting_down
        else
          :sleeping
        end
      elsif asg.instances.all? do |instance|
              instance.health_status != 'Healthy' || instance.lifecycle_state != 'InService'
            end
        case name
        when 'web'
          :down
        when 'background', 'cron'
          :impacted
        else
          :offline
        end
      elsif name == 'migration'
        :migrating
      elsif asg.max_size > 1 && asg.desired_capacity == asg.max_size
        :at_capacity
      elsif asg.instances.size < asg.desired_capacity
        :scaling_up
      elsif asg.instances.size > asg.desired_capacity
        :scaling_down
      else
        :ok
      end

      [ name, asg.instances.size, asg.max_size, status ]
    end.sort_by do |name, _, _, _|
      case name
      when 'migration'
        '0'
      when 'web'
        '1'
      when 'cron'
        '2'
      else
        "3#{name}"
      end
    end
  end
end