Class: Avocado::TaskManager
- Inherits:
-
Object
- Object
- Avocado::TaskManager
- Defined in:
- lib/avocado/task/task_manager.rb
Instance Attribute Summary collapse
-
#chains ⇒ Object
readonly
Returns the value of attribute chains.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
Instance Method Summary collapse
-
#add_task(name, options, &block) ⇒ Object
Adds a task to the task manager.
-
#execute_for_each_target(task, env) ⇒ Object
Executes a task for all defined targets.
-
#find_chain_index_containing(name) ⇒ Object
Finds the chain containing a specifc task.
-
#initialize ⇒ TaskManager
constructor
Initializes the task manager.
-
#invoke_task(task) ⇒ Object
Invokes a task.
-
#invoke_task_chain_containing(task_name) ⇒ Object
Invokes the task chain, that contains the requested task.
-
#invoke_task_oneshot(task_name) ⇒ Object
Invokes a task without dependencies.
-
#task_by_name(name) ⇒ Task
Finds a task by its name.
Constructor Details
#initialize ⇒ TaskManager
Initializes the task manager
26 27 28 29 30 |
# File 'lib/avocado/task/task_manager.rb', line 26 def initialize @chains = [] @remote_env = nil @local_env = nil end |
Instance Attribute Details
#chains ⇒ Object (readonly)
Returns the value of attribute chains.
23 24 25 |
# File 'lib/avocado/task/task_manager.rb', line 23 def chains @chains end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
22 23 24 |
# File 'lib/avocado/task/task_manager.rb', line 22 def dependencies @dependencies end |
Instance Method Details
#add_task(name, options, &block) ⇒ Object
Adds a task to the task manager
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 |
# File 'lib/avocado/task/task_manager.rb', line 37 def add_task(name, , &block) position = :after standalone = true if .has_key?(:before) position = :before end key = name if .has_key?(:before) key = [:before] standalone = false elsif .has_key?(:after) key = [:after] standalone = false end if standalone == false idx = find_chain_index_containing(key) @chains[idx].delete(name) @chains[idx].insert_at(position, key, [ name, Avocado::Task.from_task_block(name, , &block) ]) else chain = {} chain[name] = Avocado::Task.from_task_block(name, , &block) @chains << chain end end |
#execute_for_each_target(task, env) ⇒ Object
Executes a task for all defined targets
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/avocado/task/task_manager.rb', line 129 def execute_for_each_target(task, env) raise ArgumentError, 'task must be a task' unless task.kind_of?(Avocado::Task) raise ArgumentError, 'env must be a RemoteTaskExecutionEnvironment' unless env.kind_of?(Avocado::RemoteTaskExecutionEnvironment) avo = Avocado::Deployment.instance avo.config.targets.each_pair do |key, target| avo.log.info "invoking task #{task.name} for target #{target.name}..." env.config.merge!(target.config) env.establish_connection task.invoke(env) end end |
#find_chain_index_containing(name) ⇒ Object
Finds the chain containing a specifc task
83 84 85 86 87 88 89 90 91 |
# File 'lib/avocado/task/task_manager.rb', line 83 def find_chain_index_containing(name) @chains.each_with_index do |chain, idx| if chain.has_key?(name) return idx end end raise RuntimeError, "could not find a chain containing task #{name}" end |
#invoke_task(task) ⇒ Object
Invokes a task
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/avocado/task/task_manager.rb', line 149 def invoke_task(task) raise ArgumentError, 'task must be a task' unless task.kind_of?(Avocado::Task) avo = Avocado::Deployment.instance env = nil if task.scope == :remote if @remote_env.nil? @remote_env = Avocado::RemoteTaskExecutionEnvironment.new(avo.config.config) end env = @remote_env elsif task.scope == :local if @local_env.nil? @local_env = Avocado::LocalTaskExecutionEnvironment.new(avo.config.config) end env = @local_env else raise RuntimeError, 'scope must either be remote or local' end scm_provider = ScmProvider.new(env, avo.config.get(:scm)) env.scm_provider = scm_provider # if remote task -> execute for each target if task.scope == :remote execute_for_each_target(task, env) else task.invoke(env) end end |
#invoke_task_chain_containing(task_name) ⇒ Object
Invokes the task chain, that contains the requested task
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/avocado/task/task_manager.rb', line 111 def invoke_task_chain_containing(task_name) task_name = task_name.to_sym if task_name.is_a?(String) cidx = find_chain_index_containing(task_name) begin @chains[cidx].each_pair do |name, task| invoke_task(task) end rescue Exception => e Avocado::Deployment.instance.handle_abort(e) end end |
#invoke_task_oneshot(task_name) ⇒ Object
Invokes a task without dependencies
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/avocado/task/task_manager.rb', line 96 def invoke_task_oneshot(task_name) task_name = task_name.to_sym if task_name.is_a?(String) cidx = find_chain_index_containing(task_name) begin invoke_task(@chains[cidx][task_name]) rescue Exception => e Avocado::Deployment.instance.handle_abort(e) end end |
#task_by_name(name) ⇒ Task
Finds a task by its name
72 73 74 75 76 77 |
# File 'lib/avocado/task/task_manager.rb', line 72 def task_by_name(name) name = name.to_sym if name.is_a?(String) cidx = find_chain_index_containing(name) @chains[cidx][name] end |