Class: Modal::Function_

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

Constant Summary collapse

MAX_OBJECT_SIZE_BYTES =

2 MiB

2 * 1024 * 1024
MAX_SYSTEM_RETRIES =
8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function_id, method_name = nil) ⇒ Function_

Returns a new instance of Function_.



11
12
13
14
# File 'lib/modal/function.rb', line 11

def initialize(function_id, method_name = nil)
  @function_id = function_id
  @method_name = method_name
end

Instance Attribute Details

#function_idObject (readonly)

Returns the value of attribute function_id.



6
7
8
# File 'lib/modal/function.rb', line 6

def function_id
  @function_id
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



6
7
8
# File 'lib/modal/function.rb', line 6

def method_name
  @method_name
end

Class Method Details

.lookup(app_name, name, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/modal/function.rb', line 16

def self.lookup(app_name, name, options = {})
  environment = options[:environment] || Modal::Config.environment_name
  request = Modal::Client::FunctionGetRequest.new(
    app_name: app_name,
    object_tag: name,
    environment_name: Config.environment_name(environment)
  )
  resp = Modal.client.call(:function_get, request)
  new(resp.function_id)
rescue NotFoundError
  raise NotFoundError.new("Function '#{app_name}/#{name}' not found")
end

Instance Method Details

#remote(args = [], kwargs = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/modal/function.rb', line 29

def remote(args = [], kwargs = {})
  input = create_input(args, kwargs)
  invocation = ControlPlaneInvocation.create(@function_id, input, Modal::Client::FunctionCallInvocationType::FUNCTION_CALL_INVOCATION_TYPE_SYNC)

  retry_count = 0
  loop do
    return invocation.await_output
  rescue InternalFailure => e
    if retry_count <= MAX_SYSTEM_RETRIES
      invocation.retry(retry_count)
      retry_count += 1
    else
      raise e
    end
  end
end

#spawn(args = [], kwargs = {}) ⇒ Object



46
47
48
49
50
# File 'lib/modal/function.rb', line 46

def spawn(args = [], kwargs = {})
  input = create_input(args, kwargs)
  invocation = ControlPlaneInvocation.create(@function_id, input, Modal::Client::FunctionCallInvocationType::FUNCTION_CALL_INVOCATION_TYPE_ASYNC)
  FunctionCall.new(invocation.function_call_id)
end