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:, oneshot_notification_prefix:) ⇒ Hako

Returns a new instance of Hako.

Parameters:

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


18
19
20
21
22
23
# File 'lib/barbeque/executor/hako.rb', line 18

def initialize(hako_dir:, hako_env: {}, yaml_dir:, oneshot_notification_prefix:)
  @hako_dir = hako_dir
  @hako_env = hako_env
  @yaml_dir = yaml_dir
  @hako_s3_client = HakoS3Client.new(oneshot_notification_prefix)
end

Instance Attribute Details

#hako_s3_client ⇒ Object (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:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/barbeque/executor/hako.rb', line 69

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)
  end
end

#poll_retry(job_retry) ⇒ Object

Parameters:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/barbeque/executor/hako.rb', line 85

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)
  end
end

#start_execution(job_execution, envs) ⇒ Object

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/barbeque/executor/hako.rb', line 27

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)
  end
end

#start_retry(job_retry, envs) ⇒ Object

Parameters:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/barbeque/executor/hako.rb', line 45

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)
  end
end