Class: Barbeque::Executor::Hako

Inherits:
Object
  • Object
show all
Defined in:
lib/barbeque/executor/hako.rb

Defined Under Namespace

Classes: HakoCommandError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hako_dir:, hako_env: {}, yaml_dir: nil, definition_dir: nil, oneshot_notification_prefix:) ⇒ Hako

Returns a new instance of Hako.

Parameters:

  • hako_dir (String)
  • hako_env (Hash) (defaults to: {})
  • definition_dir (String) (defaults to: nil)
  • yaml_dir (String) (defaults to: nil)

    (deprecated: renamed to definition_dir)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/barbeque/executor/hako.rb', line 19

def initialize(hako_dir:, hako_env: {}, yaml_dir: nil, definition_dir: nil, oneshot_notification_prefix:)
  @hako_dir = hako_dir
  @hako_env = hako_env
  @definition_dir =
    if definition_dir
      definition_dir
    elsif yaml_dir
      warn 'yaml_dir option is renamed to definition_dir. Please update config/barbeque.yml'
      yaml_dir
    else
      raise ArgumentError.new('definition_dir is required')
    end
  @hako_s3_client = HakoS3Client.new(oneshot_notification_prefix)
end

Instance Attribute Details

#hako_s3_clientObject (readonly)

Returns the value of attribute hako_s3_client.



13
14
15
# File 'lib/barbeque/executor/hako.rb', line 13

def hako_s3_client
  @hako_s3_client
end

Instance Method Details

#poll_execution(job_execution) ⇒ Object

Parameters:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/barbeque/executor/hako.rb', line 80

def poll_execution(job_execution)
  hako_task = Barbeque::EcsHakoTask.find_by!(message_id: job_execution.message_id)
  task = @hako_s3_client.get_stopped_result(hako_task)
  if task
    status = :failed
    task.containers.each do |container|
      if container.name == 'app'
        status = container.exit_code == 0 ? :success : :failed
      end
    end
    job_execution.update!(status: status, finished_at: task.stopped_at)
    Barbeque::SlackNotifier.notify_job_execution(job_execution)
    if status == :failed
      job_execution.retry_if_possible!
    end
  end
end

#poll_retry(job_retry) ⇒ Object

Parameters:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/barbeque/executor/hako.rb', line 99

def poll_retry(job_retry)
  hako_task = Barbeque::EcsHakoTask.find_by!(message_id: job_retry.message_id)
  job_execution = job_retry.job_execution
  task = @hako_s3_client.get_stopped_result(hako_task)
  if task
    status = :failed
    task.containers.each do |container|
      if container.name == 'app'
        status = container.exit_code == 0 ? :success : :failed
      end
    end
    Barbeque::ApplicationRecord.transaction do
      job_retry.update!(status: status, finished_at: task.stopped_at)
      job_execution.update!(status: status)
    end
    Barbeque::SlackNotifier.notify_job_retry(job_retry)
    if status == :failed
      job_execution.retry_if_possible!
    end
  end
end

#start_execution(job_execution, envs) ⇒ Object

Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/barbeque/executor/hako.rb', line 36

def start_execution(job_execution, envs)
  docker_image = DockerImage.new(job_execution.job_definition.app.docker_image)
  cmd = build_hako_oneshot_command(docker_image, job_execution.job_definition.command, envs)
  stdout, stderr, status = Bundler.with_clean_env { Open3.capture3(@hako_env, *cmd, chdir: @hako_dir) }
  if status.success?
    cluster, task_arn = extract_task_info(stdout)
    Barbeque::EcsHakoTask.create!(message_id: job_execution.message_id, cluster: cluster, task_arn: task_arn)
    Barbeque::ExecutionLog.try_save_stdout_and_stderr(job_execution, stdout, stderr)
    job_execution.update!(status: :running)
  else
    Barbeque::ExecutionLog.try_save_stdout_and_stderr(job_execution, stdout, stderr)
    job_execution.update!(status: :failed, finished_at: Time.zone.now)
    Barbeque::SlackNotifier.notify_job_execution(job_execution)
    job_execution.retry_if_possible!
  end
end

#start_retry(job_retry, envs) ⇒ Object

Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/barbeque/executor/hako.rb', line 55

def start_retry(job_retry, envs)
  job_execution = job_retry.job_execution
  docker_image = DockerImage.new(job_execution.job_definition.app.docker_image)
  cmd = build_hako_oneshot_command(docker_image, job_execution.job_definition.command, envs)
  stdout, stderr, status = Bundler.with_clean_env { Open3.capture3(@hako_env, *cmd, chdir: @hako_dir) }
  if status.success?
    cluster, task_arn = extract_task_info(stdout)
    Barbeque::EcsHakoTask.create!(message_id: job_retry.message_id, cluster: cluster, task_arn: task_arn)
    Barbeque::ExecutionLog.try_save_stdout_and_stderr(job_retry, stdout, stderr)
    Barbeque::ApplicationRecord.transaction do
      job_execution.update!(status: :retried)
      job_retry.update!(status: :running)
    end
  else
    Barbeque::ExecutionLog.try_save_stdout_and_stderr(job_retry, stdout, stderr)
    Barbeque::ApplicationRecord.transaction do
      job_retry.update!(status: :failed, finished_at: Time.zone.now)
      job_execution.update!(status: :failed)
    end
    Barbeque::SlackNotifier.notify_job_retry(job_retry)
    job_execution.retry_if_possible!
  end
end