Class: Babushka::Task

Inherits:
Object show all
Includes:
LogHelpers, PathHelpers, ShellHelpers, Singleton
Defined in:
lib/babushka/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Methods included from PathHelpers

cd, in_build_dir, in_download_dir

Constructor Details

#initializeTask

Returns a new instance of Task.



11
12
13
14
15
# File 'lib/babushka/task.rb', line 11

def initialize
  @opts = Base.cmdline.opts.dup
  @running = false
  @caching = false
end

Instance Attribute Details

#cachesObject (readonly)

Returns the value of attribute caches.



8
9
10
# File 'lib/babushka/task.rb', line 8

def caches
  @caches
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/babushka/task.rb', line 8

def opts
  @opts
end

#persistent_logObject (readonly)

Returns the value of attribute persistent_log.



8
9
10
# File 'lib/babushka/task.rb', line 8

def persistent_log
  @persistent_log
end

#reportableObject

Returns the value of attribute reportable.



9
10
11
# File 'lib/babushka/task.rb', line 9

def reportable
  @reportable
end

#varsObject (readonly)

Returns the value of attribute vars.



8
9
10
# File 'lib/babushka/task.rb', line 8

def vars
  @vars
end

Instance Method Details

#cache(&block) ⇒ Object



44
45
46
47
48
49
# File 'lib/babushka/task.rb', line 44

def cache &block
  was_caching, @caching, @caches = @caching, true, {}
  block.call
ensure
  @caching = was_caching
end

#cached(key, opts = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/babushka/task.rb', line 51

def cached key, opts = {}, &block
  if !@caching
    block.call
  elsif @caches.has_key?(key)
    @caches[key].tap {|value|
      opts[:hit].call(value) if opts.has_key?(:hit)
    }
  else
    @caches[key] = block.call
  end
end

#callstackObject



82
83
84
# File 'lib/babushka/task.rb', line 82

def callstack
  @callstack ||= []
end

#log_path_for(dep) ⇒ Object



86
87
88
# File 'lib/babushka/task.rb', line 86

def log_path_for dep
  log_prefix / dep.contextual_name
end

#opt(name) ⇒ Object



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

def opt name
  opts[name]
end

#process(dep_names, with_vars) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/babushka/task.rb', line 17

def process dep_names, with_vars
  raise "A task is already running." if running?
  @vars = Vars.new
  @running = true
  Base.in_thread { RunReporter.post_reports }
  dep_names.all? {|dep_name| process_dep dep_name, with_vars }
rescue SourceLoadError => e
  Babushka::Logging.log_exception(e)
ensure
  @running = false
end

#process_dep(dep_name, with_vars) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/babushka/task.rb', line 29

def process_dep dep_name, with_vars
  Dep.find_or_suggest dep_name do |dep|
    log_dep(dep) {
      load_run_info_for dep, with_vars
      dep.with(task_args_for(dep, with_vars)).process.tap {|result|
        save_run_info_for dep, result
      }
    }.tap {|result|
      log_stderr "You can view #{opt(:debug) ? 'the' : 'a more detailed'} log at '#{log_path_for(dep)}'." unless result
      RunReporter.queue dep, result, reportable
      BugReporter.report dep if reportable
    }
  end
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  @running
end

#task_info(dep, result) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/babushka/task.rb', line 63

def task_info dep, result
  {
    :version => Base.ref,
    :run_at => Time.now,
    :system_info => Babushka.host.description,
    :dep_name => dep.name,
    :source_uri => dep.dep_source.uri,
    :result => result
  }
end

#var_path_for(dep) ⇒ Object



90
91
92
# File 'lib/babushka/task.rb', line 90

def var_path_for dep
  VarsPrefix.p / dep.contextual_name
end