daytona 0.231.0 → 0.232.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '046268edaf239ded95c63d58a8ba7c579a14985453be8e71d24aede6301629cc'
4
- data.tar.gz: 995ffbbfbe01d513101bec67af13fad646c0d3f3e9fdf3afd4a7e25b1cdc2072
3
+ metadata.gz: 639f8438b56a2524ed7f3fd14437964fecacb3d6e0d81a5640551c010a26b3f0
4
+ data.tar.gz: 73b0b1a8f7fd58b8170fcae01e494aa2e82c0c0d534cf4dd01d42f139660dea6
5
5
  SHA512:
6
- metadata.gz: 73bccba273baa4ec4f50e2081a307b169c919af3e67bd30d529abb545e323dce96b476a0aae284a673dbffed578aaababa6454b84dd95192228c8a2742879c86
7
- data.tar.gz: d8932d511681f66374119c82f1df6b19b866f3fb22b89fb3f80d1f3becac3268ac0044d1581ddf2fc167ac621e266152a4ea853f521d22b7084aab09b333bb7b
6
+ metadata.gz: ba662cc483441d4d8bc87e33bad3d79a1494559161b162704cc7de83ebbc9e1d6f0fee097fdebbaead55eb988a673370461edbc14e8fdc8d77a9bec272a4cab2
7
+ data.tar.gz: 583decad8cb93b6bdc825490a9b432d7fde7be0e332eed9d901b6a67d9689f1f25fbcca74bebfa72994bc80d53bb65074493ac7ad004bc77a4ffe83688568676
data/README.md CHANGED
@@ -11,15 +11,17 @@ Requires **Ruby >= 3.2**. Check your version with `ruby -v` before installing
11
11
  Install the package using **gem**:
12
12
 
13
13
  ```bash
14
- gem install daytona
14
+ gem install daytona -v '< 1'
15
15
  ```
16
16
 
17
17
  or add it to your **Gemfile**:
18
18
 
19
19
  ```ruby
20
- gem 'daytona'
20
+ gem 'daytona', '< 1'
21
21
  ```
22
22
 
23
+ The `< 1` pin keeps you on the 0.x line: `daytona` 2.x is a new major version with breaking changes.
24
+
23
25
  ## Get API key
24
26
 
25
27
  Generate an API key from the [Daytona Dashboard ↗](https://app.daytona.io/dashboard/keys) to authenticate SDK requests and access Daytona services. For more information, see the [API keys](https://www.daytona.io/docs/en/api-keys/) documentation.
@@ -60,7 +60,10 @@ module Daytona
60
60
  return self if pkgs.empty?
61
61
 
62
62
  extra_args = format_pip_install_args(find_links:, index_url:, extra_index_urls:, pre:, extra_options:)
63
- @dockerfile += "RUN python -m pip install #{Shellwords.join(pkgs.sort)}#{extra_args}\n"
63
+ # pip reads an argument beginning with `-` as an option, and shell quoting does not
64
+ # change that — the escaping covers shell metacharacters, not a leading dash. `--`
65
+ # closes the option list, so every package is parsed as a requirement.
66
+ @dockerfile += "RUN python -m pip install#{extra_args} -- #{Shellwords.join(pkgs.sort)}\n"
64
67
 
65
68
  self
66
69
  end
@@ -174,7 +177,7 @@ module Daytona
174
177
  def run_commands(*commands)
175
178
  commands.each do |command|
176
179
  if command.is_a?(Array)
177
- escaped = command.map { |c| c.gsub('"', '\\"').gsub("'", "\\'") }
180
+ escaped = command.map { |c| c.gsub('"', '\\"') }
178
181
  @dockerfile += "RUN #{escaped.map { |c| "\"#{c}\"" }.join(' ')}\n"
179
182
  else
180
183
  @dockerfile += "RUN #{command}\n"
@@ -227,8 +230,7 @@ module Daytona
227
230
  end
228
231
 
229
232
  args_str = flatten_str_args('entrypoint', 'entrypoint_commands', entrypoint_commands)
230
- args_str = args_str.map { |arg| "\"#{arg}\"" }.join(', ') if args_str.any?
231
- @dockerfile += "ENTRYPOINT [#{args_str}]\n"
233
+ @dockerfile += "ENTRYPOINT [#{dockerfile_json_array(args_str)}]\n"
232
234
 
233
235
  self
234
236
  end
@@ -246,8 +248,7 @@ module Daytona
246
248
  end
247
249
 
248
250
  cmd_str = flatten_str_args('cmd', 'cmd', cmd)
249
- cmd_str = cmd_str.map { |arg| "\"#{arg}\"" }.join(', ') if cmd_str.any?
250
- @dockerfile += "CMD [#{cmd_str}]\n"
251
+ @dockerfile += "CMD [#{dockerfile_json_array(cmd_str)}]\n"
251
252
  self
252
253
  end
253
254
 
@@ -598,6 +599,19 @@ module Daytona
598
599
 
599
600
  private
600
601
 
602
+ def dockerfile_json_array(args)
603
+ legacy = args.map { |arg| "\"#{arg}\"" }.join(', ')
604
+ begin
605
+ parsed = JSON.parse("[#{legacy}]")
606
+ if parsed.is_a?(Array) && parsed.length == args.length && parsed.all? { |item| item.is_a?(String) }
607
+ return legacy
608
+ end
609
+ rescue JSON::ParserError
610
+ nil
611
+ end
612
+ args.map { |arg| JSON.generate(arg) }.join(', ')
613
+ end
614
+
601
615
  # Flattens a list of strings and arrays of strings into a single array of strings
602
616
  #
603
617
  # @param function_name [String] The name of the function that is being called
data/lib/daytona/otel.rb CHANGED
@@ -179,5 +179,17 @@ module Daytona
179
179
  end
180
180
  end
181
181
  end
182
+
183
+ private
184
+
185
+ # Records the block with the span and duration metric of the instrumented +method_name+, for code that
186
+ # does that method's work without calling it.
187
+ #
188
+ # @param method_name [String, Symbol]
189
+ # @param component [String] component name for span/metric attributes
190
+ # @return [Object] The block's return value
191
+ def instrument_as(method_name, component:, &)
192
+ ::Daytona.with_instrumentation(otel_state, component, method_name.to_s, &)
193
+ end
182
194
  end
183
195
  end
@@ -471,11 +471,12 @@ module Daytona
471
471
  timeout:,
472
472
  message: "Sandbox #{id} failed to be destroyed within the #{timeout} seconds timeout period",
473
473
  setup: nil
474
- ) do
474
+ ) do |deadline|
475
475
  wait_for_state(
476
476
  target_states: [DaytonaApiClient::SandboxState::DESTROYED],
477
477
  error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
478
- safe_refresh: true
478
+ safe_refresh: true,
479
+ deadline:
479
480
  )
480
481
  end
481
482
  ensure
@@ -735,12 +736,7 @@ module Daytona
735
736
  timeout:,
736
737
  message: "Sandbox #{id} failed to become ready within the #{timeout} seconds timeout period",
737
738
  setup: proc { process_response(sandbox_api.start_sandbox(id)) }
738
- ) do
739
- wait_for_state(
740
- target_states: [DaytonaApiClient::SandboxState::STARTED],
741
- error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
742
- )
743
- end
739
+ ) { |deadline| wait_for_start(deadline) }
744
740
  end
745
741
 
746
742
  # Recovers the Sandbox from a recoverable error and waits for it to be ready.
@@ -757,12 +753,7 @@ module Daytona
757
753
  timeout:,
758
754
  message: "Sandbox #{id} failed to recover within the #{timeout} seconds timeout period",
759
755
  setup: proc { process_response(sandbox_api.recover_sandbox(id)) }
760
- ) do
761
- wait_for_state(
762
- target_states: [DaytonaApiClient::SandboxState::STARTED],
763
- error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
764
- )
765
- end
756
+ ) { |deadline| wait_for_start(deadline) }
766
757
  rescue *Sdk::API_ERROR_CLASSES => e
767
758
  raise Sdk.wrap_error(e, 'Failed to recover sandbox')
768
759
  end
@@ -772,7 +763,7 @@ module Daytona
772
763
  # @param timeout [Numeric] Maximum wait time in seconds (defaults to 60 s).
773
764
  # @param force [Boolean] If true, uses SIGKILL instead of SIGTERM (defaults to false).
774
765
  # @return [void]
775
- def stop(timeout = DEFAULT_TIMEOUT, force: false) # rubocop:disable Metrics/MethodLength
766
+ def stop(timeout = DEFAULT_TIMEOUT, force: false)
776
767
  with_timeout(
777
768
  timeout:,
778
769
  message: "Sandbox #{id} failed to become stopped within the #{timeout} seconds timeout period",
@@ -780,12 +771,7 @@ module Daytona
780
771
  sandbox_api.stop_sandbox(id, { force: force })
781
772
  refresh
782
773
  }
783
- ) do
784
- wait_for_state(
785
- target_states: [DaytonaApiClient::SandboxState::STOPPED, DaytonaApiClient::SandboxState::DESTROYED],
786
- error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
787
- )
788
- end
774
+ ) { |deadline| wait_for_stop(deadline) }
789
775
  end
790
776
 
791
777
  # Resizes the Sandbox resources.
@@ -824,7 +810,10 @@ module Daytona
824
810
  resize_request = DaytonaApiClient::ResizeSandbox.new(resize_attrs)
825
811
  process_response(sandbox_api.resize_sandbox(id, resize_request))
826
812
  }
827
- ) { wait_for_resize_complete(timeout) }
813
+ ) do |deadline|
814
+ # Under the resize deadline, but recorded as the wait_for_resize_complete call that it replaces.
815
+ instrument_as(:wait_for_resize_complete, component: 'Sandbox') { wait_for_resize(deadline) }
816
+ end
828
817
  end
829
818
 
830
819
  # Waits for the Sandbox resize operation to complete.
@@ -837,13 +826,7 @@ module Daytona
837
826
  timeout:,
838
827
  message: "Sandbox #{id} resize did not complete within the #{timeout} seconds timeout period",
839
828
  setup: nil
840
- ) do
841
- error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
842
- exclude = [DaytonaApiClient::SandboxState::RESIZING] + error_states
843
- target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
844
-
845
- wait_for_state(target_states:, error_states:)
846
- end
829
+ ) { |deadline| wait_for_resize(deadline) }
847
830
  end
848
831
 
849
832
  # Creates a new Language Server Protocol (LSP) server instance.
@@ -876,12 +859,7 @@ module Daytona
876
859
  timeout:,
877
860
  message: "Sandbox #{id} failed to start within the #{timeout} seconds timeout period",
878
861
  setup: nil
879
- ) do
880
- wait_for_state(
881
- target_states: [DaytonaApiClient::SandboxState::STARTED],
882
- error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
883
- )
884
- end
862
+ ) { |deadline| wait_for_start(deadline) }
885
863
  end
886
864
 
887
865
  # Waits for the Sandbox to reach the 'stopped' state. Polls the Sandbox status until it
@@ -895,12 +873,7 @@ module Daytona
895
873
  timeout:,
896
874
  message: "Sandbox #{id} failed to stop within the #{timeout} seconds timeout period",
897
875
  setup: nil
898
- ) do
899
- wait_for_state(
900
- target_states: [DaytonaApiClient::SandboxState::STOPPED, DaytonaApiClient::SandboxState::DESTROYED],
901
- error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
902
- )
903
- end
876
+ ) { |deadline| wait_for_stop(deadline) }
904
877
  end
905
878
 
906
879
  # Forks the Sandbox, creating a new Sandbox with an identical filesystem.
@@ -918,7 +891,7 @@ module Daytona
918
891
  setup: proc {
919
892
  forked_dto = sandbox_api.fork_sandbox(id, DaytonaApiClient::ForkSandbox.new(name:))
920
893
  }
921
- ) do
894
+ ) do |deadline|
922
895
  forked = Sandbox.new(
923
896
  sandbox_dto: forked_dto,
924
897
  config:,
@@ -927,7 +900,8 @@ module Daytona
927
900
  otel_state:,
928
901
  analytics_api_url_provider: @analytics_api_url_provider
929
902
  )
930
- forked.wait_for_sandbox_start(timeout)
903
+ # Under the fork deadline, but recorded as the wait_for_sandbox_start call that it replaces.
904
+ instrument_as(:wait_for_sandbox_start, component: 'Sandbox') { forked.wait_for_start(deadline) }
931
905
  return forked
932
906
  end
933
907
  end
@@ -946,7 +920,7 @@ module Daytona
946
920
  response = sandbox_api.create_sandbox_snapshot(id, DaytonaApiClient::CreateSandboxSnapshot.new(name:))
947
921
  process_response(response)
948
922
  }
949
- ) { wait_for_snapshot_complete }
923
+ ) { |deadline| wait_for_snapshot_complete(deadline) }
950
924
  end
951
925
 
952
926
  # Deprecated: Use +fork+ instead. This method will be removed in a future version.
@@ -979,12 +953,12 @@ module Daytona
979
953
  sandbox_api.pause_sandbox(id)
980
954
  refresh
981
955
  }
982
- ) do
956
+ ) do |deadline|
983
957
  error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
984
958
  exclude = [DaytonaApiClient::SandboxState::PAUSING] + error_states
985
959
  target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
986
960
 
987
- wait_for_state(target_states:, error_states:)
961
+ wait_for_state(target_states:, error_states:, deadline:)
988
962
  end
989
963
  end
990
964
 
@@ -1009,6 +983,21 @@ module Daytona
1009
983
  :fork, :create_snapshot, :pause,
1010
984
  component: 'Sandbox'
1011
985
 
986
+ protected
987
+
988
+ # Waits for the 'started' state until +deadline+ (nil waits without a limit). Protected so that #fork
989
+ # can wait for the forked Sandbox under its own deadline.
990
+ #
991
+ # @param deadline [WaitDeadline, nil]
992
+ # @return [void]
993
+ def wait_for_start(deadline)
994
+ wait_for_state(
995
+ target_states: [DaytonaApiClient::SandboxState::STARTED],
996
+ error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
997
+ deadline:
998
+ )
999
+ end
1000
+
1012
1001
  private
1013
1002
 
1014
1003
  def pivot_sandbox_metrics(series)
@@ -1212,25 +1201,38 @@ module Daytona
1212
1201
 
1213
1202
  # Monitors block not to exceed max execution time.
1214
1203
  #
1204
+ # Timeout.timeout alone does not bound the block: its exception is lost when it arrives while
1205
+ # a request is inside libcurl, and the block then runs without a limit. So the block also gets
1206
+ # the deadline to check itself (see #wait_for_state).
1207
+ #
1215
1208
  # @param setup [#call, Nil] Optional setup block
1216
1209
  # @param timeout [Numeric] Maximum wait time in seconds (defaults to 60 s)
1217
1210
  # @param message [String] Error message
1211
+ # @yieldparam deadline [WaitDeadline, nil] When the block must give up; nil when there is no timeout
1218
1212
  # @return [void]
1219
1213
  # @raise [Daytona::Sdk::Error]
1220
- def with_timeout(message:, setup:, timeout: DEFAULT_TIMEOUT, &)
1221
- start_at = Time.now
1222
- setup&.call
1214
+ def with_timeout(message:, setup:, timeout: DEFAULT_TIMEOUT)
1215
+ effective = setup ? timeout_left_after(setup, timeout:, message:) : timeout
1216
+
1217
+ deadline = WaitDeadline.after(effective, message) if effective.positive?
1218
+ Timeout.timeout(effective, Sdk::TimeoutError, message) { yield deadline }
1219
+ end
1223
1220
 
1224
- effective = if setup
1225
- remaining = timeout - (Time.now - start_at)
1226
- raise Sdk::TimeoutError, message if timeout.positive? && remaining <= 0
1221
+ # Runs +setup+ and returns what is left of +timeout+ after it.
1222
+ #
1223
+ # @param setup [#call]
1224
+ # @param timeout [Numeric] Sandbox::NO_TIMEOUT stays as is
1225
+ # @param message [String] Message of the Sdk::TimeoutError raised when setup used up the whole timeout
1226
+ # @return [Numeric]
1227
+ # @raise [Daytona::Sdk::TimeoutError]
1228
+ def timeout_left_after(setup, timeout:, message:)
1229
+ start_at = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
1230
+ setup.call
1227
1231
 
1228
- [NO_TIMEOUT, remaining].max
1229
- else
1230
- timeout
1231
- end
1232
+ remaining = timeout - (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start_at)
1233
+ raise Sdk::TimeoutError, message if timeout.positive? && remaining <= 0
1232
1234
 
1233
- Timeout.timeout(effective, Sdk::TimeoutError, message, &)
1235
+ [NO_TIMEOUT, remaining].max
1234
1236
  end
1235
1237
 
1236
1238
  # Waits for the Sandbox to reach one of the target states via WebSocket events
@@ -1239,9 +1241,11 @@ module Daytona
1239
1241
  # @param target_states [Array<DaytonaApiClient::SandboxState>] States that indicate success.
1240
1242
  # @param error_states [Array<DaytonaApiClient::SandboxState>] States that indicate failure.
1241
1243
  # @param safe_refresh [Boolean] If true, use refresh_data_safe for polling (swallows transient errors).
1244
+ # @param deadline [WaitDeadline, nil] Raise Sdk::TimeoutError with its message once it passes; nil waits
1245
+ # without a limit.
1242
1246
  # @return [void]
1243
1247
  # @raise [Daytona::Sdk::Error]
1244
- def wait_for_state(target_states:, error_states:, safe_refresh: false) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
1248
+ def wait_for_state(target_states:, error_states:, safe_refresh: false, deadline: nil) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
1245
1249
  ensure_subscribed
1246
1250
  subscribed = !@sub_id.nil?
1247
1251
 
@@ -1282,14 +1286,16 @@ module Daytona
1282
1286
  should_refresh = mutex.synchronize do
1283
1287
  next false if result_state
1284
1288
 
1285
- state_changed.wait(mutex, poll_interval)
1289
+ state_changed.wait(mutex, deadline ? deadline.remaining.clamp(0, poll_interval) : poll_interval)
1286
1290
  result_state.nil?
1287
1291
  end
1288
1292
  next unless should_refresh
1289
1293
  end
1290
1294
  first_poll = false
1291
1295
 
1292
- safe_refresh ? refresh_data_safe : refresh
1296
+ raise Sdk::TimeoutError, deadline.message if deadline&.expired?
1297
+
1298
+ poll_refresh(safe: safe_refresh, http_timeout: deadline&.remaining, deadline:)
1293
1299
  raise_destroyed_lifecycle_error_if_needed(target_strings)
1294
1300
 
1295
1301
  mutex.synchronize do
@@ -1312,7 +1318,7 @@ module Daytona
1312
1318
  end
1313
1319
  rescue Sdk::TimeoutError => e
1314
1320
  begin
1315
- safe_refresh ? refresh_data_safe : refresh
1321
+ poll_refresh(safe: safe_refresh, http_timeout: deadline && FINAL_REFRESH_TIMEOUT)
1316
1322
  rescue StandardError
1317
1323
  raise e
1318
1324
  end
@@ -1334,13 +1340,68 @@ module Daytona
1334
1340
  end
1335
1341
  end
1336
1342
 
1337
- def refresh_data_safe
1338
- refresh
1343
+ # @param api [DaytonaApiClient::SandboxApi] sandbox_api, or a copy of it with an HTTP timeout
1344
+ def refresh_data_safe(api = sandbox_api)
1345
+ refresh_through(api)
1339
1346
  rescue DaytonaApiClient::ApiError => e
1340
1347
  apply_state(DaytonaApiClient::SandboxState::DESTROYED) if e.code == 404
1341
1348
  rescue StandardError # rubocop:disable Lint/SuppressedException
1342
1349
  end
1343
1350
 
1351
+ # Does what #refresh does, recorded as #refresh and renewing the event subscription, but through +api+.
1352
+ #
1353
+ # @param api [DaytonaApiClient::SandboxApi] sandbox_api, or a copy of it with an HTTP timeout
1354
+ # @return [void]
1355
+ def refresh_through(api)
1356
+ return refresh if api.equal?(sandbox_api)
1357
+
1358
+ instrument_as(:refresh, component: 'Sandbox') do
1359
+ ensure_subscribed
1360
+ process_response(api.get_sandbox(id))
1361
+ end
1362
+ end
1363
+
1364
+ # Refreshes the Sandbox data for #wait_for_state.
1365
+ #
1366
+ # @param safe [Boolean] Swallow errors as #refresh_data_safe does, a timed out request included
1367
+ # @param http_timeout [Numeric, nil] Most seconds the request may take; nil leaves the API client's timeout
1368
+ # @param deadline [WaitDeadline, nil] Deadline that http_timeout is the time left on, if any
1369
+ # @return [void]
1370
+ # @raise [Daytona::Sdk::TimeoutError] If safe is false and the request times out at the deadline
1371
+ def poll_refresh(safe:, http_timeout:, deadline: nil)
1372
+ api = http_timeout ? sandbox_api_with_timeout(http_timeout) : sandbox_api
1373
+ safe ? refresh_data_safe(api) : refresh_through(api)
1374
+ rescue DaytonaApiClient::ApiError => e
1375
+ raise unless deadline && deadline_timed_out?(e, http_timeout)
1376
+
1377
+ raise Sdk::TimeoutError, deadline.message
1378
+ end
1379
+
1380
+ # @param error [DaytonaApiClient::ApiError]
1381
+ # @param http_timeout [Numeric]
1382
+ # @return [Boolean] Whether +error+ is the request timing out after +http_timeout+, not after a shorter
1383
+ # timeout of the API client
1384
+ def deadline_timed_out?(error, http_timeout)
1385
+ # The generated client raises this, without a status code, when the request times out.
1386
+ return false unless error.code.nil? && error.message == 'Connection timed out'
1387
+
1388
+ client_timeout = sandbox_api.api_client.config.timeout
1389
+ client_timeout.nil? || !client_timeout.positive? || client_timeout >= http_timeout
1390
+ end
1391
+
1392
+ # A copy of sandbox_api whose requests time out after +timeout+ seconds, or sooner when its API client
1393
+ # already has a shorter timeout. Headers and settings changed on the API client still apply.
1394
+ #
1395
+ # @param timeout [Numeric] HTTP timeout in seconds, raised to 1 ms since 0 means no timeout
1396
+ # @return [DaytonaApiClient::SandboxApi]
1397
+ def sandbox_api_with_timeout(timeout)
1398
+ client = sandbox_api.api_client
1399
+ config = client.config.dup.tap do |c|
1400
+ c.timeout = [[timeout, MIN_HTTP_TIMEOUT].max, c.timeout].compact.select(&:positive?).min
1401
+ end
1402
+ DaytonaApiClient::SandboxApi.new(client.dup.tap { |copy| copy.config = config })
1403
+ end
1404
+
1344
1405
  def raise_destroyed_lifecycle_error_if_needed(target_strings)
1345
1406
  return unless state.to_s == DaytonaApiClient::SandboxState::DESTROYED.to_s
1346
1407
  return if target_strings.include?(DaytonaApiClient::SandboxState::DESTROYED.to_s)
@@ -1429,12 +1490,46 @@ module Daytona
1429
1490
  POLL_BACKOFF_DELAY = 5.0
1430
1491
  private_constant :POLL_BACKOFF_DELAY
1431
1492
 
1432
- def wait_for_snapshot_complete
1493
+ # Most seconds the refresh after a state wait timed out may take.
1494
+ FINAL_REFRESH_TIMEOUT = 5.0
1495
+ private_constant :FINAL_REFRESH_TIMEOUT
1496
+
1497
+ # Shortest HTTP timeout, as 0 means no timeout.
1498
+ MIN_HTTP_TIMEOUT = 0.001
1499
+ private_constant :MIN_HTTP_TIMEOUT
1500
+
1501
+ # When a #with_timeout block must give up, on the monotonic clock, and the message to give up with.
1502
+ WaitDeadline = Struct.new(:at, :message) do
1503
+ def self.after(seconds, message) = new(::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + seconds, message)
1504
+
1505
+ def remaining = at - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
1506
+
1507
+ def expired? = remaining <= 0
1508
+ end
1509
+ private_constant :WaitDeadline
1510
+
1511
+ def wait_for_stop(deadline)
1512
+ wait_for_state(
1513
+ target_states: [DaytonaApiClient::SandboxState::STOPPED, DaytonaApiClient::SandboxState::DESTROYED],
1514
+ error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
1515
+ deadline:
1516
+ )
1517
+ end
1518
+
1519
+ def wait_for_resize(deadline)
1520
+ error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
1521
+ exclude = [DaytonaApiClient::SandboxState::RESIZING] + error_states
1522
+ target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
1523
+
1524
+ wait_for_state(target_states:, error_states:, deadline:)
1525
+ end
1526
+
1527
+ def wait_for_snapshot_complete(deadline)
1433
1528
  error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
1434
1529
  exclude = [DaytonaApiClient::SandboxState::SNAPSHOTTING] + error_states
1435
1530
  target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
1436
1531
 
1437
- wait_for_state(target_states:, error_states:)
1532
+ wait_for_state(target_states:, error_states:, deadline:)
1438
1533
  end
1439
1534
  end
1440
1535
  end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Daytona
7
7
  module Sdk
8
- VERSION = '0.231.0'
8
+ VERSION = '0.232.0'
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daytona
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.231.0
4
+ version: 0.232.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daytona Platforms Inc.
@@ -91,42 +91,42 @@ dependencies:
91
91
  requirements:
92
92
  - - '='
93
93
  - !ruby/object:Gem::Version
94
- version: 0.231.0
94
+ version: 0.232.0
95
95
  type: :runtime
96
96
  prerelease: false
97
97
  version_requirements: !ruby/object:Gem::Requirement
98
98
  requirements:
99
99
  - - '='
100
100
  - !ruby/object:Gem::Version
101
- version: 0.231.0
101
+ version: 0.232.0
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: daytona_api_client
104
104
  requirement: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - '='
107
107
  - !ruby/object:Gem::Version
108
- version: 0.231.0
108
+ version: 0.232.0
109
109
  type: :runtime
110
110
  prerelease: false
111
111
  version_requirements: !ruby/object:Gem::Requirement
112
112
  requirements:
113
113
  - - '='
114
114
  - !ruby/object:Gem::Version
115
- version: 0.231.0
115
+ version: 0.232.0
116
116
  - !ruby/object:Gem::Dependency
117
117
  name: daytona_toolbox_api_client
118
118
  requirement: !ruby/object:Gem::Requirement
119
119
  requirements:
120
120
  - - '='
121
121
  - !ruby/object:Gem::Version
122
- version: 0.231.0
122
+ version: 0.232.0
123
123
  type: :runtime
124
124
  prerelease: false
125
125
  version_requirements: !ruby/object:Gem::Requirement
126
126
  requirements:
127
127
  - - '='
128
128
  - !ruby/object:Gem::Version
129
- version: 0.231.0
129
+ version: 0.232.0
130
130
  - !ruby/object:Gem::Dependency
131
131
  name: dotenv
132
132
  requirement: !ruby/object:Gem::Requirement