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



47
48
49
# File 'lib/asger/task.rb', line 47

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.

Yields:

  • (instance_id, parameters)

Yield Parameters:

  • instance_id (String)

    the ID of the recently terminated instance

  • parameters (Hash)

    the parameters passed in to Asger



74
75
76
# File 'lib/asger/task.rb', line 74

def down(&block)
  @down_proc = block
end

#invoke_down(instance_id, parameters) ⇒ Object



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

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

#invoke_sanity_check(parameters) ⇒ Object



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

def invoke_sanity_check(parameters)
  if @sanity_check_proc
    logger.debug "Sanity checking for '#{@name}'..."
    @sanity_check_proc.call(parameters)
  else
    logger.debug "No sanity check for '#{@name}'."
  end
end

#invoke_up(instance, parameters) ⇒ Object



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

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

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

Defines a sanity check function, which should 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



58
59
60
# File 'lib/asger/task.rb', line 58

def sanity_check(&block)
  @sanity_check_proc = block
end

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

Defines an 'up' function.

Yields:

  • (instance, parameters)

Yield Parameters:

  • instance (Aws::EC2::Instance)

    the instance that has been created

  • parameters (Hash)

    the parameters passed in to Asger



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

def up(&block)
  @up_proc = block
end