Class: Asger::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/asger/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger, sqs_client, ec2_client, queue_url, parameters, task_files) ⇒ Runner

Returns a new instance of Runner.

Parameters:

  • logger (Logger)

    the logger for Asger to use

  • sqs_client (Aws::SQS::Client)

    the SQS client to use for polling

  • ec2_client (Aws::EC2::Client)

    the EC2 client to use to get instance information

  • queue_url (String)

    the queue URL to poll

  • parameters (Hash)

    a hash of parameters to pass to Tasks

  • task_files (Array<String>)

    list of file paths to load as Tasks



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/asger/runner.rb', line 15

def initialize(logger, sqs_client, ec2_client, queue_url, parameters, task_files)
  @logger = logger
  @sqs_client = sqs_client
  @ec2_client = ec2_client
  @ec2_resource_client = Aws::EC2::Resource.new(client: @ec2_client)
  @queue_url = queue_url
  @parameters = Hashie::Mash.new(parameters)
  @tasks = task_files.map { |tf| Task.from_file(@logger, tf) }

  @logger.info "#{@tasks.length} task(s) set up."

  @tasks.each { |t| t.invoke_sanity_check(@parameters) }
end

Instance Method Details

#delete_message(msg) ⇒ Object (private)



71
72
73
# File 'lib/asger/runner.rb', line 71

def delete_message(msg)
  @sqs_client.delete_message(queue_url: @queue_url, receipt_handle: msg[:receipt_handle])
end

#stepObject



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
# File 'lib/asger/runner.rb', line 30

def step()
  messages = @sqs_client.receive_message(queue_url: @queue_url)[:messages]
  messages.each do |msg|
    notification = JSON.parse(JSON.parse(msg[:body])["Message"])
    if notification["Event"] != nil
      case notification["Event"].gsub("autoscaling:", "")
        when "EC2_INSTANCE_LAUNCH"
          instance_id = notification["EC2InstanceId"]
          @logger.info "Instance launched: #{instance_id}"

          instance = @ec2_resource_client.instance(instance_id)
          @tasks.each do |task|
            task.invoke_up(instance, @parameters)
          end

          delete_message(msg)
        when "EC2_INSTANCE_LAUNCH_ERROR"
          @logger.warn "Instance launch error received."
          delete_message(msg)
        when "EC2_INSTANCE_TERMINATE"
          instance_id = notification["EC2InstanceId"]
          @logger.info "Instance terminated: #{instance_id}"

          @tasks.reverse_each do |task|
            task.invoke_down(instance_id, @parameters)
          end
          delete_message(msg)
        when "EC2_INSTANCE_TERMINATE_ERROR"
          @logger.warn "Instance terminate error received."
          delete_message(msg)
        when "TEST_NOTIFICATION"
          @logger.debug "Found test notification in queue."
          delete_message(msg)
        else
          @logger.debug "Unrecognized notification '#{notification["Event"]}', ignoring."
      end
    end
  end
end