Class: JobWorkflow::AutoScaling::Adapter::AwsAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/job_workflow/auto_scaling/adapter/aws_adapter.rb,
sig/generated/job_workflow/auto_scaling/adapter/aws_adapter.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ecs_client: nil) ⇒ AwsAdapter

: (?ecs_client: Aws::ECS::Client?) -> void

Parameters:

  • ecs_client: (Aws::ECS::Client, nil) (defaults to: nil)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 12

def initialize(ecs_client: nil)
  unless defined?(Aws::ECS::Client)
    raise Error, "aws-sdk-ecs is required for JobWorkflow::AutoScaling::Adapter::AwsAdapter"
  end

   = ENV.fetch("ECS_CONTAINER_METADATA_URI_V4", nil)
  raise Error, "ECS_CONTAINER_METADATA_URI_V4 is required on ECS" if .nil?

  task_meta = JSON.parse(Net::HTTP.get(URI.parse("#{}/task")))

  @ecs_client = ecs_client || Aws::ECS::Client.new
  @cluster = task_meta.fetch("Cluster")
  @task_arn = task_meta.fetch("TaskARN")
end

Instance Attribute Details

#clusterString (readonly)

Signature:

  • String

Returns:

  • (String)


39
40
41
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 39

def cluster
  @cluster
end

#ecs_clientAws::ECS::Client (readonly)

Signature:

  • Aws::ECS::Client

Returns:

  • (Aws::ECS::Client)


38
39
40
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 38

def ecs_client
  @ecs_client
end

#task_arnString (readonly)

Signature:

  • String

Returns:

  • (String)


40
41
42
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 40

def task_arn
  @task_arn
end

Instance Method Details

#describe_serviceAws::ECS::Types::Service

: () -> Aws::ECS::Types::Service

Returns:

  • (Aws::ECS::Types::Service)


51
52
53
54
55
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 51

def describe_service
  service_name = describe_service_name
  response = ecs_client.describe_services({ cluster: cluster, services: [service_name] })
  response.services.first || (raise Error, "Service(#{service_name}) does not exist in cluster!")
end

#describe_service_nameString

: () -> String

Returns:

  • (String)


43
44
45
46
47
48
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 43

def describe_service_name
  task = ecs_client.describe_tasks({ cluster: cluster, tasks: [task_arn] }).tasks.first
  raise Error, "Task(#{task_arn}) does not exist in cluster!" if task.nil?

  task.group.delete_prefix("service:")
end

#update_desired_count(desired_count) ⇒ Integer?

: (Integer) -> Integer?

Parameters:

  • (Integer)

Returns:

  • (Integer, nil)


28
29
30
31
32
33
34
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 28

def update_desired_count(desired_count)
  service = describe_service
  return if service.desired_count == desired_count

  update_service(service: service, desired_count: desired_count)
  desired_count
end

#update_service(service:, desired_count:) ⇒ Aws::ECS::Types::UpdateServiceResponse

: (service: Aws::ECS::Types::Service, desired_count: Integer) -> Aws::ECS::Types::UpdateServiceResponse

Parameters:

  • service: (Aws::ECS::Types::Service)
  • desired_count: (Integer)

Returns:

  • (Aws::ECS::Types::UpdateServiceResponse)


58
59
60
61
62
# File 'lib/job_workflow/auto_scaling/adapter/aws_adapter.rb', line 58

def update_service(service:, desired_count:)
  ecs_client.update_service(
    { cluster: service.cluster_arn, service: service.service_name, desired_count: desired_count }
  )
end