Class: Asger::Task

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

Overview

A Task is a wrapper around an up and a down function. Up functions are called when an auto-scaling group adds an instance, and Asger retrieves the instance data for the task. Down functions are called when an auto-scaling group downs an instance, and Asger passes along only the instance ID (because it's all we've got).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, code, filename = "unknown_file.rb") ⇒ Task

Returns a new instance of Task.



12
13
14
15
16
# File 'lib/asger/task.rb', line 12

def initialize(logger, code, filename = "unknown_file.rb")
  @logger = logger
  @name = File.basename(filename)
  instance_eval(code, filename, 1)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/asger/task.rb', line 10

def logger
  @logger
end

Class Method Details

.from_file(logger, file) ⇒ Object



67
68
69
# File 'lib/asger/task.rb', line 67

def self.from_file(logger, file)
  Task.new(logger, File.read(file), file)
end

Instance Method Details

#down {|instance_id, parameters| ... } ⇒ Object (private)

Defines a 'down' function, addressing EC2_INSTANCE_TERMINATE.

Yields:

  • (instance_id, parameters)

Yield Parameters:

  • instance_id (String)

    the ID of the recently terminated instance

  • asg (nil, Aws::AutoScaling::AutoScalingGroup)

    the ASG resource of the terminated instance

  • parameters (Hash)

    the parameters passed in to Asger



96
97
98
# File 'lib/asger/task.rb', line 96

def down(&block)
  @down_proc = block
end

#down_failed {|asg, parameters| ... } ⇒ Object (private)

Defines an 'up_failed' function, addressing EC2_INSTANCE_TERMINATE_ERROR.

Yields:

  • (asg, parameters)

Yield Parameters:

  • instance_id (String)

    the ID of the instance that failed to terminate

  • asg (nil, Aws::AutoScaling::AutoScalingGroup)

    the ASG resource of the failed instance

  • parameters (Hash)

    the parameters passed in to Asger



113
114
115
# File 'lib/asger/task.rb', line 113

def down_failed(&block)
  @down_failed_proc = block
end

#init {|parameters| ... } ⇒ Object (private)

Defines an init function, which should set member vars. Raise and fail (which will halt Asger before it does anything with the actual queue) if there's a problem with the parameter set.

Yields:

  • (parameters)

Yield Parameters:

  • parameters (Hash)

    the parameters passed in to Asger



78
79
80
# File 'lib/asger/task.rb', line 78

def init(&block)
  @init_proc = block
end

#invoke_down(instance_id, asg, parameters) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/asger/task.rb', line 37

def invoke_down(instance_id, asg, parameters)
  if @down_proc
    logger.debug "Invoking down for '#{@name}'..."
    @down_proc.call(instance_id, asg, parameters)
    logger.debug "Down invoked for '#{@name}'..."
  else
    logger.debug "No down for '#{@name}'."
  end
end

#invoke_down_failed(instance_id, asg, parameters) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/asger/task.rb', line 57

def invoke_down_failed(instance_id, asg, parameters)
  if @down_failed_proc
    logger.debug "Invoking down_failed for '#{@name}'..."
    @down_failed_proc.call(instance_id, asg, parameters)
    logger.debug "down_failed invoked for '#{@name}'..."
  else
    logger.debug "No down_failed for '#{@name}'."
  end
end

#invoke_init(parameters) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/asger/task.rb', line 18

def invoke_init(parameters)
  if @init_proc
    logger.debug "Initializing for '#{@name}'..."
    @init_proc.call(parameters)
  else
    logger.debug "No init for '#{@name}'."
  end
end

#invoke_up(instance, asg, parameters) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/asger/task.rb', line 27

def invoke_up(instance, asg, parameters)
  if @up_proc
    logger.debug "Invoking up for '#{@name}'..."
    @up_proc.call(instance, asg, parameters)
    logger.debug "Up invoked for '#{@name}'..."
  else
    logger.debug "No up for '#{@name}'."
  end
end

#invoke_up_failed(asg, parameters) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/asger/task.rb', line 47

def invoke_up_failed(asg, parameters)
  if @up_failed_proc
    logger.debug "Invoking up_failed for '#{@name}'..."
    @up_failed_proc.call(asg, parameters)
    logger.debug "up_failed invoked for '#{@name}'..."
  else
    logger.debug "No up_failed for '#{@name}'."
  end
end

#up {|instance, parameters| ... } ⇒ Object (private)

Defines an 'up' function, addressing EC2_INSTANCE_LAUNCH.

Yields:

  • (instance, parameters)

Yield Parameters:

  • instance (Aws::EC2::Instance)

    the instance that has been created

  • asg (nil, Aws::AutoScaling::AutoScalingGroup)

    the ASG resource of the launched instance

  • parameters (Hash)

    the parameters passed in to Asger



87
88
89
# File 'lib/asger/task.rb', line 87

def up(&block)
  @up_proc = block
end

#up_failed {|asg, parameters| ... } ⇒ Object (private)

Defines an 'up_failed' function, addressing EC2_INSTANCE_LAUNCH_ERROR.

Yields:

  • (asg, parameters)

Yield Parameters:

  • asg (nil, Aws::AutoScaling::AutoScalingGroup)

    the ASG resource of the failed instance

  • parameters (Hash)

    the parameters passed in to Asger



104
105
106
# File 'lib/asger/task.rb', line 104

def up_failed(&block)
  @up_failed_proc = block
end