Class: Rollo::Model::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/rollo/model/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(ecs_cluster_name, ecs_service_arn, region, ecs_resource = nil, waiter = nil) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rollo/model/service.rb', line 8

def initialize(
    ecs_cluster_name, ecs_service_arn, region,
        ecs_resource = nil, waiter = nil)
  @ecs_cluster_name = ecs_cluster_name
  @ecs_service_arn = ecs_service_arn
  @ecs_resource = ecs_resource || Aws::ECS::Resource.new(region: region)
  reload

  @waiter = waiter || Wait.new(attempts: 300, timeout: 30, delay: 5)
end

Instance Method Details

#decrease_instance_count_by(count_delta, options = {}, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rollo/model/service.rb', line 67

def decrease_instance_count_by(count_delta, options = {}, &block)
  minimum = options[:minimum_instance_count] || 0
  initial = desired_count
  decreased = initial - count_delta
  target = [decreased, minimum].max

  callbacks_for(block).try_respond_with(
      :prepare, initial, target)

  ensure_instance_count(target, &block)
end

#desired_countObject



39
40
41
# File 'lib/rollo/model/service.rb', line 39

def desired_count
  @ecs_service.desired_count
end

#desired_count=(count) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rollo/model/service.rb', line 43

def desired_count=(count)
  @ecs_resource.client
      .update_service(
          cluster: @ecs_cluster_name,
          service: @ecs_service_arn,
          desired_count: count)
end

#ensure_instance_count(count, &block) ⇒ Object



79
80
81
82
# File 'lib/rollo/model/service.rb', line 79

def ensure_instance_count(count, &block)
  self.desired_count = count
  wait_for_service_health(&block)
end

#has_desired_count?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rollo/model/service.rb', line 51

def has_desired_count?
  running_count == desired_count
end

#increase_instance_count_by(count_delta, options = {}, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rollo/model/service.rb', line 55

def increase_instance_count_by(count_delta, options = {}, &block)
  maximum = options[:maximum_instance_count] || Float::INFINITY
  initial = desired_count
  increased = initial + count_delta
  target = [increased, maximum].min

  callbacks_for(block).try_respond_with(
      :prepare, initial, target)

  ensure_instance_count(target, &block)
end

#instanceObject



23
24
25
# File 'lib/rollo/model/service.rb', line 23

def instance
  @ecs_service
end

#is_replica?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rollo/model/service.rb', line 31

def is_replica?
  @ecs_service.scheduling_strategy == 'REPLICA'
end

#nameObject



19
20
21
# File 'lib/rollo/model/service.rb', line 19

def name
  @ecs_service.service_name
end

#reloadObject



27
28
29
# File 'lib/rollo/model/service.rb', line 27

def reload
  @ecs_service = get_ecs_service
end

#running_countObject



35
36
37
# File 'lib/rollo/model/service.rb', line 35

def running_count
  @ecs_service.running_count
end

#wait_for_service_health(&block) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/rollo/model/service.rb', line 84

def wait_for_service_health(&block)
  @waiter.until do |attempt|
    reload
    callbacks_for(block)
        .try_respond_with(:waiting_for_health, attempt) if block
    has_desired_count?
  end
end