lib/hako/schedulers/ecs.rb in hako-0.8.4 vs lib/hako/schedulers/ecs.rb in hako-0.8.5
- old
+ new
@@ -20,10 +20,12 @@
@ecs = Aws::ECS::Client.new(region: region)
@elb = EcsElb.new(app_id, Aws::ElasticLoadBalancing::Client.new(region: region), options.fetch('elb', nil))
@ec2 = Aws::EC2::Client.new(region: region)
@force = force
@dry_run = dry_run
+ @started_at = nil
+ @container_instance_arn = nil
end
def deploy(containers)
app = containers.fetch('app')
front = containers.fetch('front')
@@ -65,26 +67,21 @@
Hako.logger.info "Task definition isn't changed"
task_definition = @ecs.describe_task_definition(task_definition: "#{@app_id}-oneshot").task_definition
else
Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}"
end
- task = run_task(task_definition, commands, env)
- Hako.logger.info "Started task: #{task.task_arn}"
- containers = wait_for_task(task)
- Hako.logger.info 'Oneshot task finished'
- exit_code = 127
- containers.each do |name, container|
- if container.exit_code.nil?
- Hako.logger.info "#{name} has stopped without exit_code: reason=#{container.reason}"
- else
- Hako.logger.info "#{name} has stopped with exit_code=#{container.exit_code}"
- if name == 'app'
- exit_code = container.exit_code
- end
- end
+ @task = run_task(task_definition, commands, env)
+ Hako.logger.info "Started task: #{@task.task_arn}"
+ wait_for_oneshot_finish
+ end
+
+ def stop_oneshot
+ if @task
+ Hako.logger.warn "Stopping #{@task.task_arn}"
+ @ecs.stop_task(cluster: @cluster, task: @task.task_arn, reason: 'Stopped by hako stop_oneshot')
+ wait_for_oneshot_finish
end
- exit_code
end
def status
service = describe_service
unless service
@@ -311,30 +308,46 @@
count: 1,
started_by: "hako oneshot #{@app_id}",
).tasks[0]
end
+ def wait_for_oneshot_finish
+ containers = wait_for_task(@task)
+ @task = nil
+ Hako.logger.info 'Oneshot task finished'
+ exit_code = 127
+ containers.each do |name, container|
+ if container.exit_code.nil?
+ Hako.logger.info "#{name} has stopped without exit_code: reason=#{container.reason}"
+ else
+ Hako.logger.info "#{name} has stopped with exit_code=#{container.exit_code}"
+ if name == 'app'
+ exit_code = container.exit_code
+ end
+ end
+ end
+ exit_code
+ end
+
def wait_for_task(task)
task_arn = task.task_arn
- container_instance_arn = nil
- started_at = nil
loop do
task = @ecs.describe_tasks(cluster: @cluster, tasks: [task_arn]).tasks[0]
- if container_instance_arn != task.container_instance_arn
- container_instance_arn = task.container_instance_arn
- report_container_instance(container_instance_arn)
+ if @container_instance_arn != task.container_instance_arn
+ @container_instance_arn = task.container_instance_arn
+ report_container_instance(@container_instance_arn)
end
- unless started_at
- started_at = task.started_at
- if started_at
- Hako.logger.info "Started at #{started_at}"
+ unless @started_at
+ @started_at = task.started_at
+ if @started_at
+ Hako.logger.info "Started at #{@started_at}"
end
end
Hako.logger.debug " status #{task.last_status}"
if task.last_status == 'STOPPED'
- Hako.logger.info "Stopped at #{task.stopped_at}"
+ Hako.logger.info "Stopped at #{task.stopped_at} (reason: #{task.stopped_reason})"
containers = {}
task.containers.each do |c|
containers[c.name] = c
end
return containers