hako 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04af6ef3ea0e6a6e2c80930324cd68f3b9a5fc2b
4
- data.tar.gz: ea768f333dae15fa6375c4e3e73d33d4141dff00
3
+ metadata.gz: 872f90813b982a8a19c5b4cf901aa06eb401216b
4
+ data.tar.gz: 02e799d33ceaa061a85578b48929b2eaa4c0922c
5
5
  SHA512:
6
- metadata.gz: aa664e517703a21b214062359a345292dfd0a2b92e56cfee8fd6ac59a58986e068ae46fee8b06002fbd9495b86dd3c4f53de5252f3b68acad1617cafe1d3256a
7
- data.tar.gz: bf721b1a28403e98c471d53f68924fe460516bfe29657fa237d747f53274ad973ea7037e414ac5e4ecf43679bd1793ad77b71a3d275939a66b6207fe1afe8d0d
6
+ metadata.gz: f6089d93829d5c5866857bb10104a5a145801efd7ca3672aec0e6e58704d735416404a435b780d0f5d897a0c838de545c3ea051e060584f5a4db8a28d0aeb824
7
+ data.tar.gz: 50227fd8d3abfff03896b85fbb96a9deb9c53246e26c05724d9e0bc01d30cb53c70b1d3667ef782751b80da7003381ce738fa3f63cc5b8dab6c45e8acf3937a6
@@ -27,7 +27,9 @@ module Hako
27
27
  def oneshot(commands, tag:, containers:, env: {})
28
28
  containers = load_containers(tag, dry_run: false, with: containers)
29
29
  scheduler = load_scheduler(@app.yaml['scheduler'])
30
- exit scheduler.oneshot(containers, commands, env)
30
+ with_oneshot_signal_handlers(scheduler) do
31
+ exit scheduler.oneshot(containers, commands, env)
32
+ end
31
33
  end
32
34
 
33
35
  def status
@@ -42,6 +44,32 @@ module Hako
42
44
 
43
45
  private
44
46
 
47
+ TRAP_SIGNALS = %i[INT TERM].freeze
48
+ class SignalTrapped < StandardError; end
49
+
50
+ def with_oneshot_signal_handlers(scheduler, &block)
51
+ old_handlers = {}
52
+ trapped = false
53
+
54
+ begin
55
+ TRAP_SIGNALS.each do |sig|
56
+ old_handlers[sig] = Signal.trap(sig) { raise SignalTrapped }
57
+ end
58
+ block.call
59
+ rescue SignalTrapped
60
+ trapped = true
61
+ ensure
62
+ old_handlers.each do |sig, command|
63
+ Signal.trap(sig, command)
64
+ end
65
+ end
66
+
67
+ if trapped
68
+ scheduler.stop_oneshot
69
+ end
70
+ nil
71
+ end
72
+
45
73
  def load_containers(tag, dry_run:, with: nil)
46
74
  app = AppContainer.new(@app, @app.yaml['app'].merge('tag' => tag), dry_run: dry_run)
47
75
  front = load_front(@app.yaml['front'], dry_run: dry_run)
@@ -22,6 +22,8 @@ module Hako
22
22
  @ec2 = Aws::EC2::Client.new(region: region)
23
23
  @force = force
24
24
  @dry_run = dry_run
25
+ @started_at = nil
26
+ @container_instance_arn = nil
25
27
  end
26
28
 
27
29
  def deploy(containers)
@@ -67,22 +69,17 @@ module Hako
67
69
  else
68
70
  Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}"
69
71
  end
70
- task = run_task(task_definition, commands, env)
71
- Hako.logger.info "Started task: #{task.task_arn}"
72
- containers = wait_for_task(task)
73
- Hako.logger.info 'Oneshot task finished'
74
- exit_code = 127
75
- containers.each do |name, container|
76
- if container.exit_code.nil?
77
- Hako.logger.info "#{name} has stopped without exit_code: reason=#{container.reason}"
78
- else
79
- Hako.logger.info "#{name} has stopped with exit_code=#{container.exit_code}"
80
- if name == 'app'
81
- exit_code = container.exit_code
82
- end
83
- end
72
+ @task = run_task(task_definition, commands, env)
73
+ Hako.logger.info "Started task: #{@task.task_arn}"
74
+ wait_for_oneshot_finish
75
+ end
76
+
77
+ def stop_oneshot
78
+ if @task
79
+ Hako.logger.warn "Stopping #{@task.task_arn}"
80
+ @ecs.stop_task(cluster: @cluster, task: @task.task_arn, reason: 'Stopped by hako stop_oneshot')
81
+ wait_for_oneshot_finish
84
82
  end
85
- exit_code
86
83
  end
87
84
 
88
85
  def status
@@ -313,27 +310,43 @@ module Hako
313
310
  ).tasks[0]
314
311
  end
315
312
 
313
+ def wait_for_oneshot_finish
314
+ containers = wait_for_task(@task)
315
+ @task = nil
316
+ Hako.logger.info 'Oneshot task finished'
317
+ exit_code = 127
318
+ containers.each do |name, container|
319
+ if container.exit_code.nil?
320
+ Hako.logger.info "#{name} has stopped without exit_code: reason=#{container.reason}"
321
+ else
322
+ Hako.logger.info "#{name} has stopped with exit_code=#{container.exit_code}"
323
+ if name == 'app'
324
+ exit_code = container.exit_code
325
+ end
326
+ end
327
+ end
328
+ exit_code
329
+ end
330
+
316
331
  def wait_for_task(task)
317
332
  task_arn = task.task_arn
318
- container_instance_arn = nil
319
- started_at = nil
320
333
  loop do
321
334
  task = @ecs.describe_tasks(cluster: @cluster, tasks: [task_arn]).tasks[0]
322
- if container_instance_arn != task.container_instance_arn
323
- container_instance_arn = task.container_instance_arn
324
- report_container_instance(container_instance_arn)
335
+ if @container_instance_arn != task.container_instance_arn
336
+ @container_instance_arn = task.container_instance_arn
337
+ report_container_instance(@container_instance_arn)
325
338
  end
326
- unless started_at
327
- started_at = task.started_at
328
- if started_at
329
- Hako.logger.info "Started at #{started_at}"
339
+ unless @started_at
340
+ @started_at = task.started_at
341
+ if @started_at
342
+ Hako.logger.info "Started at #{@started_at}"
330
343
  end
331
344
  end
332
345
 
333
346
  Hako.logger.debug " status #{task.last_status}"
334
347
 
335
348
  if task.last_status == 'STOPPED'
336
- Hako.logger.info "Stopped at #{task.stopped_at}"
349
+ Hako.logger.info "Stopped at #{task.stopped_at} (reason: #{task.stopped_reason})"
337
350
  containers = {}
338
351
  task.containers.each do |c|
339
352
  containers[c.name] = c
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Hako
3
- VERSION = '0.8.4'
3
+ VERSION = '0.8.5'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hako
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki