Class: Wrapbox::Runner::Ecs
- Inherits:
-
Object
- Object
- Wrapbox::Runner::Ecs
- Defined in:
- lib/wrapbox/runner/ecs.rb,
lib/wrapbox/runner/ecs/task_waiter.rb,
lib/wrapbox/runner/ecs/instance_manager.rb
Defined Under Namespace
Classes: Cli, ContainerAbnormalEnd, ExecutionFailure, ExecutionTimeout, InstanceManager, LackResource, LaunchFailure, Parameter, TaskWaiter
Constant Summary collapse
- EXECUTION_RETRY_INTERVAL =
3
- WAIT_DELAY =
5
- TERM_TIMEOUT =
120
- HOST_TERMINATED_REASON_REGEXP =
/Host EC2.*terminated/
Instance Attribute Summary collapse
-
#container_definitions ⇒ Object
readonly
Returns the value of attribute container_definitions.
-
#cpu ⇒ Object
readonly
Returns the value of attribute cpu.
-
#enable_ecs_managed_tags ⇒ Object
readonly
Returns the value of attribute enable_ecs_managed_tags.
-
#enable_execute_command ⇒ Object
readonly
Returns the value of attribute enable_execute_command.
-
#main_container_name ⇒ Object
readonly
Returns the value of attribute main_container_name.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#network_configuration ⇒ Object
readonly
Returns the value of attribute network_configuration.
-
#network_mode ⇒ Object
readonly
Returns the value of attribute network_mode.
-
#placement_constraints ⇒ Object
readonly
Returns the value of attribute placement_constraints.
-
#placement_strategy ⇒ Object
readonly
Returns the value of attribute placement_strategy.
-
#propagate_tags ⇒ Object
readonly
Returns the value of attribute propagate_tags.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#requires_compatibilities ⇒ Object
readonly
Returns the value of attribute requires_compatibilities.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#task_definition_name ⇒ Object
readonly
Returns the value of attribute task_definition_name.
-
#volumes ⇒ Object
readonly
Returns the value of attribute volumes.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Ecs
constructor
A new instance of Ecs.
- #run(class_name, method_name, args, container_definition_overrides: {}, **parameters) ⇒ Object
- #run_cmd(cmds, container_definition_overrides: {}, ignore_signal: false, **parameters) ⇒ Object
Constructor Details
#initialize(options) ⇒ Ecs
Returns a new instance of Ecs.
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 |
# File 'lib/wrapbox/runner/ecs.rb', line 63 def initialize() @name = [:name] @task_definition_name = [:task_definition_name] @revision = [:revision] @cluster = [:cluster] @region = [:region] @volumes = [:volumes] @placement_constraints = [:placement_constraints] || [] @placement_strategy = [:placement_strategy] @capacity_provider_strategy = [:capacity_provider_strategy] || [] @launch_type = [:launch_type] @requires_compatibilities = [:requires_compatibilities] @network_mode = [:network_mode] @network_configuration = [:network_configuration] @cpu = [:cpu] @memory = [:memory] @enable_ecs_managed_tags = [:enable_ecs_managed_tags] @tags = [:tags] @propagate_tags = [:propagate_tags] @enable_execute_command = [:enable_execute_command] if [:launch_instances] @instance_manager = Wrapbox::Runner::Ecs::InstanceManager.new(@cluster, @region, **[:launch_instances]) end @task_waiter = Wrapbox::Runner::Ecs::TaskWaiter.new(cluster: @cluster, region: @region, delay: WAIT_DELAY) @container_definitions = [:container_definition] ? [[:container_definition]] : [:container_definitions] || [] @container_definitions.concat([:additional_container_definitions]) if [:additional_container_definitions] # deprecated if !@container_definitions.empty? && [:task_definition] raise "Please set only one of `container_definition` and `task_definition`" end if [:additional_container_definitions] && ![:additional_container_definitions].empty? warn "`additional_container_definitions` is deprecated parameter, Use `container_definitions` instead of it" end @task_definition_info = [:task_definition] if !@container_definitions.empty? @task_definition_name ||= "wrapbox_#{@name}" @main_container_name = @container_definitions[0][:name] || @task_definition_name elsif @task_definition_info @task_definition_name = @task_definition_info[:task_definition_name] @main_container_name = @task_definition_info[:main_container_name] unless @main_container_name raise "Please set `task_definition[:main_container_name]`" end end @container_definitions.each do |d| d[:docker_labels]&.stringify_keys! d.dig(:log_configuration, :options)&.stringify_keys! end @task_role_arn = [:task_role_arn] @execution_role_arn = [:execution_role_arn] @logger = Wrapbox.logger if [:log_fetcher] type = [:log_fetcher][:type] @log_fetcher = LogFetcher.new(type, **[:log_fetcher]) end end |
Instance Attribute Details
#container_definitions ⇒ Object (readonly)
Returns the value of attribute container_definitions.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def container_definitions @container_definitions end |
#cpu ⇒ Object (readonly)
Returns the value of attribute cpu.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def cpu @cpu end |
#enable_ecs_managed_tags ⇒ Object (readonly)
Returns the value of attribute enable_ecs_managed_tags.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def @enable_ecs_managed_tags end |
#enable_execute_command ⇒ Object (readonly)
Returns the value of attribute enable_execute_command.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def enable_execute_command @enable_execute_command end |
#main_container_name ⇒ Object (readonly)
Returns the value of attribute main_container_name.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def main_container_name @main_container_name end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def memory @memory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def name @name end |
#network_configuration ⇒ Object (readonly)
Returns the value of attribute network_configuration.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def network_configuration @network_configuration end |
#network_mode ⇒ Object (readonly)
Returns the value of attribute network_mode.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def network_mode @network_mode end |
#placement_constraints ⇒ Object (readonly)
Returns the value of attribute placement_constraints.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def placement_constraints @placement_constraints end |
#placement_strategy ⇒ Object (readonly)
Returns the value of attribute placement_strategy.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def placement_strategy @placement_strategy end |
#propagate_tags ⇒ Object (readonly)
Returns the value of attribute propagate_tags.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def @propagate_tags end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def region @region end |
#requires_compatibilities ⇒ Object (readonly)
Returns the value of attribute requires_compatibilities.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def requires_compatibilities @requires_compatibilities end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def revision @revision end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def @tags end |
#task_definition_name ⇒ Object (readonly)
Returns the value of attribute task_definition_name.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def task_definition_name @task_definition_name end |
#volumes ⇒ Object (readonly)
Returns the value of attribute volumes.
32 33 34 |
# File 'lib/wrapbox/runner/ecs.rb', line 32 def volumes @volumes end |
Class Method Details
.split_overridable_options_and_parameters(options) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wrapbox/runner/ecs.rb', line 52 def self.() opts = .dup = {} %i[cluster launch_type task_role_arn execution_role_arn tags propagate_tags].each do |key| value = opts.delete(key) [key] = value if value end [, opts] end |
Instance Method Details
#run(class_name, method_name, args, container_definition_overrides: {}, **parameters) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/wrapbox/runner/ecs.rb', line 145 def run(class_name, method_name, args, container_definition_overrides: {}, **parameters) task_definition = prepare_task_definition(container_definition_overrides) parameter = Parameter.new(**parameters) envs = parameters[:environments] || [] envs += [ { name: CLASS_NAME_ENV, value: class_name.to_s, }, { name: METHOD_NAME_ENV, value: method_name.to_s, }, { name: METHOD_ARGS_ENV, value: MultiJson.dump(args), }, ] if @instance_manager Thread.new { @instance_manager.start_preparing_instances(1) } end run_task(task_definition.task_definition_arn, ["bundle", "exec", "rake", "wrapbox:run"], envs, parameter) ensure @instance_manager&.terminate_all_instances end |
#run_cmd(cmds, container_definition_overrides: {}, ignore_signal: false, **parameters) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/wrapbox/runner/ecs.rb', line 174 def run_cmd(cmds, container_definition_overrides: {}, ignore_signal: false, **parameters) ths = [] task_definition = prepare_task_definition(container_definition_overrides) parameter = Parameter.new(**parameters) cmds << nil if cmds.empty? if @instance_manager Thread.new { @instance_manager.start_preparing_instances(cmds.size) } end cmds.each_with_index do |cmd, idx| ths << Thread.new(cmd, idx) do |c, i| Thread.current[:cmd_index] = i envs = (parameters[:environments] || []) + [{name: "WRAPBOX_CMD_INDEX", value: i.to_s}] run_task(task_definition.task_definition_arn, c&.shellsplit, envs, parameter) end end ThreadsWait.all_waits(ths) # Raise an error if some threads have an error ths.each(&:join) true rescue SignalException => e sig = "SIG#{Signal.signame(e.signo)}" if ignore_signal @logger.info("Receive #{sig} signal. But ECS Tasks continue running") else @logger.info("Receive #{sig} signal. Stop All tasks") ths.each do |th| th.report_on_exception = false th.raise(e) end wait_until = Time.now + TERM_TIMEOUT + 15 # thread_timeout_buffer ths.each do |th| wait = wait_until - Time.now th.join(wait) if wait.positive? end end nil ensure @instance_manager&.terminate_all_instances end |