Class: Daytona::Daytona

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/daytona/daytona.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

included

Constructor Details

#initialize(config = Config.new) ⇒ Daytona

Returns a new instance of Daytona.

Parameters:

  • config (Daytona::Config) (defaults to: Config.new)

    Configuration options. Defaults to Daytona::Config.new



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/daytona/daytona.rb', line 41

def initialize(config = Config.new)
  @config = config
  ensure_access_token_defined

  otel_enabled = config.otel_enabled ||
                 config._experimental&.dig('otel_enabled') ||
                 config.read_env('DAYTONA_OTEL_ENABLED') == 'true' ||
                 config.read_env('DAYTONA_EXPERIMENTAL_OTEL_ENABLED') == 'true'
  @otel_state = (::Daytona.init_otel(Sdk::VERSION) if otel_enabled)

  @api_client = build_api_client
  @sandbox_api = DaytonaApiClient::SandboxApi.new(api_client)
  @config_api = DaytonaApiClient::ConfigApi.new(api_client)
  @analytics_api_url = nil
  @analytics_api_url_fetched = false
  @analytics_api_url_mutex = Mutex.new
  @volume = VolumeService.new(DaytonaApiClient::VolumesApi.new(api_client), otel_state:)
  @secret = SecretService.new(DaytonaApiClient::SecretApi.new(api_client), otel_state:)
  @warm_pool = WarmPoolService.new(DaytonaApiClient::WarmPoolsApi.new(api_client), otel_state:)
  @object_storage_api = DaytonaApiClient::ObjectStorageApi.new(api_client)
  @snapshots_api = DaytonaApiClient::SnapshotsApi.new(api_client)
  @snapshot = SnapshotService.new(
    snapshots_api:,
    object_storage_api:,
    default_region_id: config.target,
    otel_state:
  )
  token = config.api_key || config.jwt_token

  if config.use_deprecated_polling
    warn(
      '[DEPRECATION] Polling-only mode (use_deprecated_polling / DAYTONA_USE_DEPRECATED_POLLING) ' \
      'is deprecated and will be removed in a future release.',
      uplevel: 1
    )
  else
    @event_dispatcher = EventDispatcher.new(
      api_url: config.api_url,
      token: token,
      organization_id: config.organization_id,
      source: SOURCE_RUBY,
      sdk_version: Sdk::VERSION
    )
    @event_dispatcher.ensure_connected
  end

  @subscription_manager = EventSubscriptionManager.new(@event_dispatcher)
end

Instance Attribute Details

#api_clientDaytonaApiClient (readonly)

Returns:

  • (DaytonaApiClient)


17
18
19
# File 'lib/daytona/daytona.rb', line 17

def api_client
  @api_client
end

#configDaytona::Config (readonly)

Returns:



14
15
16
# File 'lib/daytona/daytona.rb', line 14

def config
  @config
end

#object_storage_apiDaytonaApiClient::ObjectStorageApi (readonly)

Returns:

  • (DaytonaApiClient::ObjectStorageApi)


32
33
34
# File 'lib/daytona/daytona.rb', line 32

def object_storage_api
  @object_storage_api
end

#sandbox_apiDaytonaApiClient::SandboxApi (readonly)

Returns:

  • (DaytonaApiClient::SandboxApi)


20
21
22
# File 'lib/daytona/daytona.rb', line 20

def sandbox_api
  @sandbox_api
end

#secretDaytona::SecretService (readonly)



26
27
28
# File 'lib/daytona/daytona.rb', line 26

def secret
  @secret
end

#snapshotDaytona::SnapshotService (readonly)



38
39
40
# File 'lib/daytona/daytona.rb', line 38

def snapshot
  @snapshot
end

#snapshots_apiDaytonaApiClient::SnapshotsApi (readonly)

Returns:

  • (DaytonaApiClient::SnapshotsApi)


35
36
37
# File 'lib/daytona/daytona.rb', line 35

def snapshots_api
  @snapshots_api
end

#volumeDaytona::VolumeService (readonly)



23
24
25
# File 'lib/daytona/daytona.rb', line 23

def volume
  @volume
end

#warm_poolDaytona::WarmPoolService (readonly)



29
30
31
# File 'lib/daytona/daytona.rb', line 29

def warm_pool
  @warm_pool
end

Instance Method Details

#closevoid

This method returns an undefined value.

Shuts down OTel providers, flushing any pending telemetry data.



93
94
95
96
97
98
99
100
# File 'lib/daytona/daytona.rb', line 93

def close
  @subscription_manager&.shutdown
  @subscription_manager = nil
  @event_dispatcher&.disconnect
  @event_dispatcher = nil
  ::Daytona.shutdown_otel(@otel_state)
  @otel_state = nil
end

#create(params = nil, on_snapshot_create_logs: nil) ⇒ Daytona::Sandbox

Creates a sandbox with the specified parameters

Parameters:

Returns:

Raises:

  • (Daytona::Sdk::Error)

    If auto_stop_interval, auto_pause_interval, auto_archive_interval, or ttl_minutes is negative, or if auto_stop_interval and auto_pause_interval are both non-zero



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/daytona/daytona.rb', line 108

def create(params = nil, on_snapshot_create_logs: nil)
  if params.nil?
    params = CreateSandboxFromSnapshotParams.new(language: CodeLanguage::PYTHON)
  elsif params.language.nil?
    params.language = CodeLanguage::PYTHON
  end

  unless CodeLanguage::ALL.include?(params.language.to_s.to_sym)
    raise ArgumentError,
          "Invalid #{CODE_TOOLBOX_LANGUAGE_LABEL}: #{params.language}. Supported languages: #{CodeLanguage::ALL.join(', ')}"
  end

  _create(params, on_snapshot_create_logs:)
end

#delete(sandbox, wait: false) ⇒ void

This method returns an undefined value.

Deletes a Sandbox.

Parameters:

  • sandbox (Daytona::Sandbox)
  • wait (Boolean) (defaults to: false)

    When true, block until the Sandbox is destroyed.



128
# File 'lib/daytona/daytona.rb', line 128

def delete(sandbox, wait: false) = sandbox.delete(wait:)

#get(id) ⇒ Daytona::Sandbox

Gets a Sandbox by its ID.

Parameters:

  • id (String)

Returns:

Raises:



135
136
137
138
139
140
# File 'lib/daytona/daytona.rb', line 135

def get(id)
  sandbox_dto = sandbox_api.get_sandbox(id)
  to_sandbox(sandbox_dto:)
rescue *Sdk::API_ERROR_CLASSES => e
  raise Sdk.wrap_error(e, "Failed to get sandbox #{id}")
end

#list(query = nil) ⇒ Enumerator<Daytona::Sandbox>

Iterates over Sandboxes matching the given query.

Examples:

daytona.list(Daytona::ListSandboxesQuery.new(labels: { 'env' => 'dev' })).each do |sandbox|
  puts sandbox.id
end

Parameters:

Returns:

Raises:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/daytona/daytona.rb', line 152

def list(query = nil)
  q = query || ListSandboxesQuery.new

  Enumerator.new do |yielder|
    cursor = nil
    first_page = true
    while first_page || cursor
      first_page = false
      response = fetch_sandbox_page(q, cursor)
      response.items.each do |sandbox_dto|
        yielder << to_sandbox(sandbox_dto: sandbox_dto)
      end
      cursor = response.next_cursor
      break if cursor.nil? || (cursor.respond_to?(:empty?) && cursor.empty?)
    end
  end
end

#start(sandbox, timeout = Sandbox::DEFAULT_TIMEOUT) ⇒ void

This method returns an undefined value.

Starts a Sandbox and waits for it to be ready.

Parameters:

  • sandbox (Daytona::Sandbox)
  • timeout (Numeric) (defaults to: Sandbox::DEFAULT_TIMEOUT)

    Maximum wait time in seconds (defaults to 60 s).



175
# File 'lib/daytona/daytona.rb', line 175

def start(sandbox, timeout = Sandbox::DEFAULT_TIMEOUT) = sandbox.start(timeout)

#stop(sandbox, timeout = Sandbox::DEFAULT_TIMEOUT) ⇒ void

This method returns an undefined value.

Stops a Sandbox and waits for it to be stopped.

Parameters:

  • sandbox (Daytona::Sandbox)
  • timeout (Numeric) (defaults to: Sandbox::DEFAULT_TIMEOUT)

    Maximum wait time in seconds (defaults to 60 s).



182
# File 'lib/daytona/daytona.rb', line 182

def stop(sandbox, timeout = Sandbox::DEFAULT_TIMEOUT) = sandbox.stop(timeout)