Class: ElasticBeans::Command::Restart

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/command/restart.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: NoEnvironmentsError

Constant Summary collapse

USAGE =
"restart [ENVIRONMENT]"
DESC =
"Restart one or all environments in the Elastic Beanstalk application"
LONG_DESC =
<<-LONG_DESC
Given no arguments, restarts all servers for the application.
Given an environment name, restarts only the servers for that environment, e.g. `restart webserver`.
Use `-q` for worker environments to clarify which worker environment to restart, e.g. `restart worker -q default`.

Requires AWS credentials to be set in the environment, i.e. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
LONG_DESC

Instance Method Summary collapse

Constructor Details

#initialize(application:, queue:, elastic_beanstalk:, ui:) ⇒ Restart

Returns a new instance of Restart.

[View source]

19
20
21
22
23
24
# File 'lib/elastic_beans/command/restart.rb', line 19

def initialize(application:, queue:, elastic_beanstalk:, ui:)
  @application = application
  @queue = queue
  @elastic_beanstalk = elastic_beanstalk
  @ui = ui
end

Instance Method Details

#run(environment_type = nil) ⇒ Object

[View source]

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
76
77
78
79
80
81
82
# File 'lib/elastic_beans/command/restart.rb', line 26

def run(environment_type = nil)
  environments = []
  if environment_type
    environment = ElasticBeans::Environment.new_by_type(
      environment_type,
      queue: queue,
      application: application,
      elastic_beanstalk: elastic_beanstalk,
    )
    environments << environment
    if environment.status != "Ready"
      raise EnvironmentsNotReady.new(environments: environments)
    end
  else
    environments = application.environments
    if environments.empty?
      raise NoEnvironmentsError
    end
    unready_environments = environments.select { |environment| environment.status != "Ready" }
    if unready_environments.any?
      raise EnvironmentsNotReady.new(environments: unready_environments)
    end
  end

  progressbar = ProgressBar.create(title: "Restarting", total: nil, output: ui.stdout)

  Signal.trap("INT") do
    puts "\nInterrupting beans"
    puts "Restart still in progress, follow it in the AWS console"
    raise Interrupt
  end
  Signal.trap("TERM") do
    puts "Terminating beans"
    puts "Restart still in progress, follow it in the AWS console"
    raise SignalException.new("TERM")
  end

  threads = environments.map { |environment|
    progressbar.log("Restarting `#{environment.name}'...")
    thread = Thread.new do
      environment.restart
    end
    progressbar.increment
    thread
  }

  loop do
    sleep 0.5
    progressbar.increment
    if threads.none?(&:alive?)
      progressbar.total = progressbar.progress
      break
    end
  end

  threads.each(&:join)
end