Class: Modal::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/modal/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_id) ⇒ Image

Returns a new instance of Image.



5
6
7
# File 'lib/modal/image.rb', line 5

def initialize(image_id)
  @image_id = image_id
end

Instance Attribute Details

#image_idObject (readonly)

Returns the value of attribute image_id.



3
4
5
# File 'lib/modal/image.rb', line 3

def image_id
  @image_id
end

Class Method Details

.from_registry_internal(app_id, tag, image_registry_config = nil) ⇒ Object



9
10
11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/modal/image.rb', line 9

def self.from_registry_internal(app_id, tag, image_registry_config = nil)
  request = Modal::Client::ImageGetOrCreateRequest.new(
    app_id: app_id,
    image: Modal::Client::Image.new(
      dockerfile_commands: ["FROM #{tag}"],
      image_registry_config: image_registry_config
    ),
    namespace: Modal::Client::DeploymentNamespace::DEPLOYMENT_NAMESPACE_WORKSPACE,
    builder_version: Config.image_builder_version
  )

  resp = Modal.client.call(:image_get_or_create, request)
  result = resp.result
   = resp.

  if result && result.status != :GENERIC_STATUS_UNSPECIFIED
     = resp.
  else
    last_entry_id = ""

    loop do
      streaming_request = Modal::Client::ImageJoinStreamingRequest.new(
        image_id: resp.image_id,
        timeout: 55,
        last_entry_id: last_entry_id
      )

      puts "Waiting for image build for #{resp.image_id}..."

      begin
        stream_resp = Modal.client.call(:image_join_streaming, streaming_request)

        if stream_resp.result && stream_resp.result.status != Modal::Client::GenericResult::GenericStatus::GENERIC_STATUS_UNSPECIFIED
          result = stream_resp.result
           = stream_resp.
          break
        end

        if stream_resp.entry_id && !stream_resp.entry_id.empty?
          last_entry_id = stream_resp.entry_id
        end
      rescue => e
        puts "Error checking build status: #{e.message}"
        sleep(5)
        next
      end

      sleep(2) # Wait before next check
    end
  end

  case result.status
  when :GENERIC_STATUS_FAILURE
    raise "Image build for #{resp.image_id} failed with the exception:\n#{result.exception}"
  when :GENERIC_STATUS_TERMINATED
    raise "Image build for #{resp.image_id} terminated due to external shut-down. Please try again."
  when :GENERIC_STATUS_TIMEOUT
    raise "Image build for #{resp.image_id} timed out. Please try again with a larger timeout parameter."
  when :GENERIC_STATUS_SUCCESS
    new(resp.image_id)
  else
    raise "Image build for #{resp.image_id} failed with unknown status: #{result.status}"
  end
end