Module: Genesis::Framework::Tasks

Defined in:
lib/genesisframework/tasks.rb

Class Method Summary collapse

Class Method Details

.call_block(blocks, sym, msg = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/genesisframework/tasks.rb', line 23

def self.call_block blocks, sym, msg = nil
  if self.has_block? blocks, sym
    puts msg if msg
    blocks[sym].call
  else
    true
  end
end

.execute(task_name) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/genesisframework/tasks.rb', line 52

def self.execute task_name
  puts "\n#{task_name}\n================================================="

  prompt_timeout = ENV['GENESIS_PROMPT_TIMEOUT'] \
    || Genesis::Framework::Utils.config_cache['task_prompt_timeout'] \
    || 10
  if prompt_timeout.to_i > 0
    # only prompt if there is a resonable timeout
    return true unless Genesis::PromptCLI.ask("Would you like to run this task?", prompt_timeout, true)
  end

  task = Genesis::Framework::Tasks.const_get(task_name)

  if task.blocks.nil?
    puts "task is empty with nothing to do, skipping..."
    return true
  end

  begin
    puts "task is now testing if it needs to be initialized..."
    if task.blocks.has_key?(:precondition)
      task.blocks[:precondition].each do |description, block|
        puts "Testing: %s" % description
        unless self.call_block(task.blocks[:precondition], description)
          puts "task is being skipped..."
          return true
        end
      end
    end
  rescue => e
    puts "%s task had error on testing if it needs initialization: %s" % [task_name, e.message]
    return false
  end

  begin
    puts "task is now initializing..."
    self.call_block(task.blocks, :init);
    puts "task is now initialized..."
  rescue => e
    puts "%s task threw error on initialization: %s" % [task_name, e.message]
    return false
  end

  begin
    puts "task is now testing if it can run..."
    if task.blocks.has_key?(:condition)
      task.blocks[:condition].each do |description, block|
        puts "Checking: %s" % description
        unless self.call_block(task.blocks[:condition], description)
          puts "Conditional failed. Task is being skipped."
          return true
        end
      end
    end
  rescue => e
    puts "%s task had error on testing if it needs running: %s" % [task_name, e.message]
    return false
  end

  success = nil
  task.options[:retries].each_with_index do |sleep_interval, index|
    attempt = index + 1
    begin
      puts "task is attempting run #%d..." % [attempt]
      Timeout::timeout(task.options[:timeout]) do
        success = self.call_block(task.blocks, :run)
      end
      # a run block should raise an error or be false for a failure
      success = true if success.nil?
    rescue => e
       puts "%s task [run #%d] caused error: %s" % [task_name, attempt, e.message]
       success = nil      # cause a retry
    end
    break unless success.nil? # if we got an answer, we're done
    puts "task is sleeping for %d seconds..." % [sleep_interval]
    Kernel.sleep(sleep_interval)
  end
  success = false if success.nil? # must have used all the retries, fail

  if success
    success = self.call_block(task.blocks, :success)
    puts "task is successful!"
  else
    puts 'task failed!!!'
    if self.has_block? task.blocks, :rollback
      success = self.call_block(task.blocks, :rollback, "rolling back!")
    end
  end

  puts "\n\n"
  return success
end

.has_block?(blocks, sym) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/genesisframework/tasks.rb', line 19

def self.has_block? blocks, sym
  blocks.has_key?(sym) && blocks[sym].respond_to?(:call)
end

.load_config(file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/genesisframework/tasks.rb', line 8

def self.load_config file
  begin
    data = File.read(file)

    ## TODO: consider tokenizing the keys of the hash? needed???
    Genesis::Framework::Utils.config_cache = YAML::load(data)
  rescue => e
    raise "Unable to parse config %s: %s" % [file, e.message]
  end
end

.load_tasks(dir) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/genesisframework/tasks.rb', line 32

def self.load_tasks dir
  # expand the LOAD_PATH to include modules, so facts are available
  $:.unshift File.join(File.expand_path(dir),'modules')
  puts "\nParsing tasks from directory: %s" % [dir]

  Dir.glob(File.join(dir,'*.rb')) do |f|
    begin
      Genesis::Framework::Tasks.class_eval File.read(f)
    rescue => e
      raise "Error parsing task %s: %s" % [f, e.message]
    end
  end

  @tasks = Genesis::Framework::Tasks.constants.select do |c|
    Genesis::Framework::Tasks.const_get(c).include?( Genesis::Framework::Task )
  end

  @tasks.sort!
end