Class: ReplicateClient::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/replicate-client/deployment.rb

Constant Summary collapse

INDEX_PATH =
"/deployments"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ReplicateClient::Deployment

Initialize a new deployment instance.

Parameters:

  • attributes (Hash)

    The attributes of the deployment.



138
139
140
# File 'lib/replicate-client/deployment.rb', line 138

def initialize(attributes)
  reset_attributes(attributes)
end

Instance Attribute Details

#current_releaseObject

Attributes for deployment.



131
132
133
# File 'lib/replicate-client/deployment.rb', line 131

def current_release
  @current_release
end

#nameObject

Attributes for deployment.



131
132
133
# File 'lib/replicate-client/deployment.rb', line 131

def name
  @name
end

#ownerObject

Attributes for deployment.



131
132
133
# File 'lib/replicate-client/deployment.rb', line 131

def owner
  @owner
end

Class Method Details

.auto_paging_each {|ReplicateClient::Deployment| ... } ⇒ void

This method returns an undefined value.

List all deployments.

Yields:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/replicate-client/deployment.rb', line 13

def auto_paging_each(&block)
  cursor = nil

  loop do
    url_params = cursor ? "?cursor=#{cursor}" : ""
    attributes = ReplicateClient.client.get("#{INDEX_PATH}#{url_params}")

    deployments = attributes["results"].map { |deployment| new(deployment) }

    deployments.each(&block)

    cursor = attributes["next"] ? URI.decode_www_form(URI.parse(attributes["next"]).query).to_h["cursor"] : nil
    break if cursor.nil?
  end
end

.build_path(owner:, name:) ⇒ String

Build the path for a specific deployment.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

Returns:

  • (String)


115
116
117
# File 'lib/replicate-client/deployment.rb', line 115

def build_path(owner:, name:)
  "#{INDEX_PATH}/#{owner}/#{name}"
end

.create!(name:, model:, hardware:, min_instances:, max_instances:, version_id: nil) ⇒ ReplicateClient::Deployment

Create a new deployment.

Parameters:

  • name (String)

    The name of the deployment.

  • model (ReplicateClient::Model, String)

    The model identifier in “owner/name” format.

  • version_id (String, nil) (defaults to: nil)

    The version ID of the model.

  • hardware (ReplicateClient::Hardware, String)

    The hardware SKU.

  • min_instances (Integer)

    The minimum number of instances.

  • max_instances (Integer)

    The maximum number of instances.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/replicate-client/deployment.rb', line 39

def create!(name:, model:, hardware:, min_instances:, max_instances:, version_id: nil)
  model_full_name = model.is_a?(Model) ? model.full_name : model
  hardware_sku = hardware.is_a?(Hardware) ? hardware.sku : hardware
  version = if version_id
              version_id
            elsif model.is_a?(Model)
              model.version_id
            else
              Model.find(model).latest_version.id
            end

  body = {
    name: name,
    model: model_full_name,
    version: version,
    hardware: hardware_sku,
    min_instances: min_instances,
    max_instances: max_instances
  }

  attributes = ReplicateClient.client.post(INDEX_PATH, body)
  new(attributes)
end

.destroy!(owner:, name:) ⇒ void

This method returns an undefined value.

Delete a deployment.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.



104
105
106
107
# File 'lib/replicate-client/deployment.rb', line 104

def destroy!(owner:, name:)
  path = build_path(owner: owner, name: name)
  ReplicateClient.client.delete(path)
end

.find(full_name) ⇒ ReplicateClient::Deployment

Find a deployment by owner and name.

Parameters:

  • full_name (String)

    The full name of the deployment in “owner/name” format.

Returns:



68
69
70
71
72
# File 'lib/replicate-client/deployment.rb', line 68

def find(full_name)
  path = build_path(**parse_full_name(full_name))
  attributes = ReplicateClient.client.get(path)
  new(attributes)
end

.find_by(owner:, name:) ⇒ ReplicateClient::Deployment?

Find a deployment by owner and name.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

Returns:



92
93
94
95
96
# File 'lib/replicate-client/deployment.rb', line 92

def find_by(owner:, name:)
  find_by!(owner: owner, name: name)
rescue ReplicateClient::NotFoundError
  nil
end

.find_by!(owner:, name:) ⇒ ReplicateClient::Deployment

Find a deployment by owner and name.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

Returns:



80
81
82
83
84
# File 'lib/replicate-client/deployment.rb', line 80

def find_by!(owner:, name:)
  path = build_path(owner: owner, name: name)
  attributes = ReplicateClient.client.get(path)
  new(attributes)
end

.parse_full_name(full_name) ⇒ Hash

Parse the full name for a deployment.

Parameters:

  • full_name (String)

    The full name of the deployment.

Returns:

  • (Hash)


124
125
126
127
# File 'lib/replicate-client/deployment.rb', line 124

def parse_full_name(full_name)
  parts = full_name.split("/")
  { owner: parts[0], name: parts[1] }
end

Instance Method Details

#create_prediction!(input, webhook_url: nil, webhook_events_filter: nil) ⇒ ReplicateClient::Prediction

Create prediction for the deployment.

Parameters:

  • input (Hash)

    The input for the prediction.

  • webhook_url (String, nil) (defaults to: nil)

    The URL to send webhook events to.

  • webhook_events_filter (Array<String>, nil) (defaults to: nil)

    The events to send to the webhook.

Returns:



193
194
195
196
197
198
199
200
# File 'lib/replicate-client/deployment.rb', line 193

def create_prediction!(input, webhook_url: nil, webhook_events_filter: nil)
  Prediction.create_for_deployment!(
    deployment: self,
    input: input,
    webhook_url: webhook_url,
    webhook_events_filter: webhook_events_filter
  )
end

#destroy!void

This method returns an undefined value.

Destroy the deployment.



145
146
147
# File 'lib/replicate-client/deployment.rb', line 145

def destroy!
  self.class.destroy!(owner: owner, name: name)
end

#pathString

Build the path for the deployment.

Returns:

  • (String)


182
183
184
# File 'lib/replicate-client/deployment.rb', line 182

def path
  self.class.build_path(owner: owner, name: name)
end

#reload!void

This method returns an undefined value.

Reload the deployment.



174
175
176
177
# File 'lib/replicate-client/deployment.rb', line 174

def reload!
  attributes = ReplicateClient.client.get(path)
  reset_attributes(attributes)
end

#update!(hardware: nil, min_instances: nil, max_instances: nil, version: nil) ⇒ void

This method returns an undefined value.

Update the deployment.

Parameters:

  • hardware (String, nil) (defaults to: nil)

    The hardware SKU.

  • min_instances (Integer, nil) (defaults to: nil)

    The minimum number of instances.

  • max_instances (Integer, nil) (defaults to: nil)

    The maximum number of instances.

  • version (ReplicateClient::Version, String, nil) (defaults to: nil)

    The version ID of the model.



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/replicate-client/deployment.rb', line 157

def update!(hardware: nil, min_instances: nil, max_instances: nil, version: nil)
  version_id = version.is_a?(Version) ? version.id : version
  path = build_path(owner: owner, name: name)
  body = {
    hardware: hardware,
    min_instances: min_instances,
    max_instances: max_instances,
    version: version_id
  }.compact

  attributes = ReplicateClient.client.patch(path, body)
  reset_attributes(attributes)
end