Class: OpenStax::Aws::Distribution

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/distribution.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, region:, logger: nil) ⇒ Distribution

Returns a new instance of Distribution.



6
7
8
9
10
# File 'lib/openstax/aws/distribution.rb', line 6

def initialize(id:, region:, logger: nil)
  @id = id
  @region = region
  @logger ||= Logger.new(IO::NULL)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/openstax/aws/distribution.rb', line 4

def id
  @id
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/openstax/aws/distribution.rb', line 4

def logger
  @logger
end

#regionObject (readonly)

Returns the value of attribute region.



4
5
6
# File 'lib/openstax/aws/distribution.rb', line 4

def region
  @region
end

Instance Method Details

#invalidate(paths:, wait: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openstax/aws/distribution.rb', line 12

def invalidate(paths:, wait: false)
  paths = [paths].flatten.compact

  resp = client.create_invalidation({
    distribution_id: id,
    invalidation_batch: {
      paths: {
        quantity: paths.length,
        items: paths,
      },
      caller_reference: "#{paths.join(' ')} #{Time.now.utc.strftime("%Y%m%d-%H%M%S")}"
    },
  })

  invalidation_id = resp.invalidation.id

  logger.info("Created invalidation #{invalidation_id} for paths #{paths.join(', ')}.")

  wait_message = OpenStax::Aws::WaitMessage.new(
    message: "Waiting for invalidation #{invalidation_id} to be completed"
  )

  begin
    Aws::CloudFront::Waiters::InvalidationCompleted.new(
      client: client,
      before_attempt: ->(*) { wait_message.say_it },
      max_attempts: 60
    ).wait(
      distribution_id: id,
      id: invalidation_id
    )
  rescue Aws::Waiters::Errors::WaiterFailed => error
    logger.error "Waiting failed: #{error.message}"
    raise
  end
  logger.info "Invalidation #{invalidation_id} has been completed!"
end