daytona 0.231.1 → 0.233.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 +4 -4
- data/README.md +61 -2
- data/lib/daytona/common/image.rb +4 -1
- data/lib/daytona/common/resources.rb +31 -2
- data/lib/daytona/daytona.rb +15 -1
- data/lib/daytona/otel.rb +12 -0
- data/lib/daytona/sandbox.rb +188 -67
- data/lib/daytona/sandbox_nodes.rb +610 -0
- data/lib/daytona/sdk/errors.rb +3 -0
- data/lib/daytona/sdk/version.rb +1 -1
- data/lib/daytona/sdk.rb +1 -0
- data/lib/daytona/snapshot_service.rb +1 -0
- data/scripts/generate-docs.rb +5 -0
- metadata +8 -7
data/lib/daytona/sandbox.rb
CHANGED
|
@@ -206,6 +206,15 @@ module Daytona
|
|
|
206
206
|
# @return [Daytona::Process]
|
|
207
207
|
attr_reader :process
|
|
208
208
|
|
|
209
|
+
# @return [Daytona::SandboxNodes] Rank-ordered node handles. A gang head includes all ranks. A direct worker lookup includes only that worker.
|
|
210
|
+
attr_reader :nodes
|
|
211
|
+
# @return [Integer] Number of nodes reported by the last refresh
|
|
212
|
+
attr_reader :node_count
|
|
213
|
+
# @return [Integer, nil] Current topology generation
|
|
214
|
+
attr_reader :topology_generation
|
|
215
|
+
# @return [Boolean, nil] Whether the API reports a degraded gang
|
|
216
|
+
attr_reader :degraded
|
|
217
|
+
|
|
209
218
|
# @return [Daytona::FileSystem]
|
|
210
219
|
attr_reader :fs
|
|
211
220
|
|
|
@@ -283,6 +292,8 @@ module Daytona
|
|
|
283
292
|
@info_api = info_api
|
|
284
293
|
@server_api = server_api
|
|
285
294
|
@system_api = system_api
|
|
295
|
+
@nodes = SandboxNodes.new(self)
|
|
296
|
+
@nodes.send(:update, sandbox_dto)
|
|
286
297
|
|
|
287
298
|
ensure_subscribed
|
|
288
299
|
end
|
|
@@ -471,11 +482,12 @@ module Daytona
|
|
|
471
482
|
timeout:,
|
|
472
483
|
message: "Sandbox #{id} failed to be destroyed within the #{timeout} seconds timeout period",
|
|
473
484
|
setup: nil
|
|
474
|
-
) do
|
|
485
|
+
) do |deadline|
|
|
475
486
|
wait_for_state(
|
|
476
487
|
target_states: [DaytonaApiClient::SandboxState::DESTROYED],
|
|
477
488
|
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
|
|
478
|
-
safe_refresh: true
|
|
489
|
+
safe_refresh: true,
|
|
490
|
+
deadline:
|
|
479
491
|
)
|
|
480
492
|
end
|
|
481
493
|
ensure
|
|
@@ -735,12 +747,7 @@ module Daytona
|
|
|
735
747
|
timeout:,
|
|
736
748
|
message: "Sandbox #{id} failed to become ready within the #{timeout} seconds timeout period",
|
|
737
749
|
setup: proc { process_response(sandbox_api.start_sandbox(id)) }
|
|
738
|
-
)
|
|
739
|
-
wait_for_state(
|
|
740
|
-
target_states: [DaytonaApiClient::SandboxState::STARTED],
|
|
741
|
-
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
742
|
-
)
|
|
743
|
-
end
|
|
750
|
+
) { |deadline| wait_for_start(deadline) }
|
|
744
751
|
end
|
|
745
752
|
|
|
746
753
|
# Recovers the Sandbox from a recoverable error and waits for it to be ready.
|
|
@@ -757,12 +764,7 @@ module Daytona
|
|
|
757
764
|
timeout:,
|
|
758
765
|
message: "Sandbox #{id} failed to recover within the #{timeout} seconds timeout period",
|
|
759
766
|
setup: proc { process_response(sandbox_api.recover_sandbox(id)) }
|
|
760
|
-
)
|
|
761
|
-
wait_for_state(
|
|
762
|
-
target_states: [DaytonaApiClient::SandboxState::STARTED],
|
|
763
|
-
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
764
|
-
)
|
|
765
|
-
end
|
|
767
|
+
) { |deadline| wait_for_start(deadline) }
|
|
766
768
|
rescue *Sdk::API_ERROR_CLASSES => e
|
|
767
769
|
raise Sdk.wrap_error(e, 'Failed to recover sandbox')
|
|
768
770
|
end
|
|
@@ -772,7 +774,7 @@ module Daytona
|
|
|
772
774
|
# @param timeout [Numeric] Maximum wait time in seconds (defaults to 60 s).
|
|
773
775
|
# @param force [Boolean] If true, uses SIGKILL instead of SIGTERM (defaults to false).
|
|
774
776
|
# @return [void]
|
|
775
|
-
def stop(timeout = DEFAULT_TIMEOUT, force: false)
|
|
777
|
+
def stop(timeout = DEFAULT_TIMEOUT, force: false)
|
|
776
778
|
with_timeout(
|
|
777
779
|
timeout:,
|
|
778
780
|
message: "Sandbox #{id} failed to become stopped within the #{timeout} seconds timeout period",
|
|
@@ -780,12 +782,7 @@ module Daytona
|
|
|
780
782
|
sandbox_api.stop_sandbox(id, { force: force })
|
|
781
783
|
refresh
|
|
782
784
|
}
|
|
783
|
-
)
|
|
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
|
|
785
|
+
) { |deadline| wait_for_stop(deadline) }
|
|
789
786
|
end
|
|
790
787
|
|
|
791
788
|
# Resizes the Sandbox resources.
|
|
@@ -808,6 +805,7 @@ module Daytona
|
|
|
808
805
|
def resize(resources, timeout = DEFAULT_TIMEOUT)
|
|
809
806
|
raise Sdk::Error, 'Resources must not be nil' if resources.nil?
|
|
810
807
|
|
|
808
|
+
resources.validate_scalar! if resources.is_a?(Resources)
|
|
811
809
|
if resources.gpu || resources.gpu_type
|
|
812
810
|
raise Sdk::Error,
|
|
813
811
|
'Resize does not support changes to gpu or gpu_type — to change GPU, create a new sandbox'
|
|
@@ -824,7 +822,10 @@ module Daytona
|
|
|
824
822
|
resize_request = DaytonaApiClient::ResizeSandbox.new(resize_attrs)
|
|
825
823
|
process_response(sandbox_api.resize_sandbox(id, resize_request))
|
|
826
824
|
}
|
|
827
|
-
)
|
|
825
|
+
) do |deadline|
|
|
826
|
+
# Under the resize deadline, but recorded as the wait_for_resize_complete call that it replaces.
|
|
827
|
+
instrument_as(:wait_for_resize_complete, component: 'Sandbox') { wait_for_resize(deadline) }
|
|
828
|
+
end
|
|
828
829
|
end
|
|
829
830
|
|
|
830
831
|
# Waits for the Sandbox resize operation to complete.
|
|
@@ -837,13 +838,7 @@ module Daytona
|
|
|
837
838
|
timeout:,
|
|
838
839
|
message: "Sandbox #{id} resize did not complete within the #{timeout} seconds timeout period",
|
|
839
840
|
setup: nil
|
|
840
|
-
)
|
|
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
|
|
841
|
+
) { |deadline| wait_for_resize(deadline) }
|
|
847
842
|
end
|
|
848
843
|
|
|
849
844
|
# Creates a new Language Server Protocol (LSP) server instance.
|
|
@@ -876,12 +871,7 @@ module Daytona
|
|
|
876
871
|
timeout:,
|
|
877
872
|
message: "Sandbox #{id} failed to start within the #{timeout} seconds timeout period",
|
|
878
873
|
setup: nil
|
|
879
|
-
)
|
|
880
|
-
wait_for_state(
|
|
881
|
-
target_states: [DaytonaApiClient::SandboxState::STARTED],
|
|
882
|
-
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
883
|
-
)
|
|
884
|
-
end
|
|
874
|
+
) { |deadline| wait_for_start(deadline) }
|
|
885
875
|
end
|
|
886
876
|
|
|
887
877
|
# Waits for the Sandbox to reach the 'stopped' state. Polls the Sandbox status until it
|
|
@@ -895,12 +885,7 @@ module Daytona
|
|
|
895
885
|
timeout:,
|
|
896
886
|
message: "Sandbox #{id} failed to stop within the #{timeout} seconds timeout period",
|
|
897
887
|
setup: nil
|
|
898
|
-
)
|
|
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
|
|
888
|
+
) { |deadline| wait_for_stop(deadline) }
|
|
904
889
|
end
|
|
905
890
|
|
|
906
891
|
# Forks the Sandbox, creating a new Sandbox with an identical filesystem.
|
|
@@ -918,7 +903,7 @@ module Daytona
|
|
|
918
903
|
setup: proc {
|
|
919
904
|
forked_dto = sandbox_api.fork_sandbox(id, DaytonaApiClient::ForkSandbox.new(name:))
|
|
920
905
|
}
|
|
921
|
-
) do
|
|
906
|
+
) do |deadline|
|
|
922
907
|
forked = Sandbox.new(
|
|
923
908
|
sandbox_dto: forked_dto,
|
|
924
909
|
config:,
|
|
@@ -927,7 +912,8 @@ module Daytona
|
|
|
927
912
|
otel_state:,
|
|
928
913
|
analytics_api_url_provider: @analytics_api_url_provider
|
|
929
914
|
)
|
|
930
|
-
|
|
915
|
+
# Under the fork deadline, but recorded as the wait_for_sandbox_start call that it replaces.
|
|
916
|
+
instrument_as(:wait_for_sandbox_start, component: 'Sandbox') { forked.wait_for_start(deadline) }
|
|
931
917
|
return forked
|
|
932
918
|
end
|
|
933
919
|
end
|
|
@@ -946,7 +932,7 @@ module Daytona
|
|
|
946
932
|
response = sandbox_api.create_sandbox_snapshot(id, DaytonaApiClient::CreateSandboxSnapshot.new(name:))
|
|
947
933
|
process_response(response)
|
|
948
934
|
}
|
|
949
|
-
) { wait_for_snapshot_complete }
|
|
935
|
+
) { |deadline| wait_for_snapshot_complete(deadline) }
|
|
950
936
|
end
|
|
951
937
|
|
|
952
938
|
# Deprecated: Use +fork+ instead. This method will be removed in a future version.
|
|
@@ -979,12 +965,12 @@ module Daytona
|
|
|
979
965
|
sandbox_api.pause_sandbox(id)
|
|
980
966
|
refresh
|
|
981
967
|
}
|
|
982
|
-
) do
|
|
968
|
+
) do |deadline|
|
|
983
969
|
error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
984
970
|
exclude = [DaytonaApiClient::SandboxState::PAUSING] + error_states
|
|
985
971
|
target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
|
|
986
972
|
|
|
987
|
-
wait_for_state(target_states:, error_states:)
|
|
973
|
+
wait_for_state(target_states:, error_states:, deadline:)
|
|
988
974
|
end
|
|
989
975
|
end
|
|
990
976
|
|
|
@@ -1009,6 +995,21 @@ module Daytona
|
|
|
1009
995
|
:fork, :create_snapshot, :pause,
|
|
1010
996
|
component: 'Sandbox'
|
|
1011
997
|
|
|
998
|
+
protected
|
|
999
|
+
|
|
1000
|
+
# Waits for the 'started' state until +deadline+ (nil waits without a limit). Protected so that #fork
|
|
1001
|
+
# can wait for the forked Sandbox under its own deadline.
|
|
1002
|
+
#
|
|
1003
|
+
# @param deadline [WaitDeadline, nil]
|
|
1004
|
+
# @return [void]
|
|
1005
|
+
def wait_for_start(deadline)
|
|
1006
|
+
wait_for_state(
|
|
1007
|
+
target_states: [DaytonaApiClient::SandboxState::STARTED],
|
|
1008
|
+
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
|
|
1009
|
+
deadline:
|
|
1010
|
+
)
|
|
1011
|
+
end
|
|
1012
|
+
|
|
1012
1013
|
private
|
|
1013
1014
|
|
|
1014
1015
|
def pivot_sandbox_metrics(series)
|
|
@@ -1141,6 +1142,7 @@ module Daytona
|
|
|
1141
1142
|
return if sandbox_dto.nil?
|
|
1142
1143
|
|
|
1143
1144
|
new_state = sandbox_dto.state
|
|
1145
|
+
@node_count ||= 1
|
|
1144
1146
|
|
|
1145
1147
|
# Fields shared by both DaytonaApiClient::Sandbox and DaytonaApiClient::SandboxListItem.
|
|
1146
1148
|
@id = sandbox_dto.id
|
|
@@ -1193,6 +1195,10 @@ module Daytona
|
|
|
1193
1195
|
@volumes = sandbox_dto.volumes
|
|
1194
1196
|
@build_info = sandbox_dto.build_info
|
|
1195
1197
|
@backup_created_at = sandbox_dto.backup_created_at
|
|
1198
|
+
@node_count = sandbox_dto.node_count unless sandbox_dto.node_count.nil?
|
|
1199
|
+
@topology_generation = sandbox_dto.topology_generation unless sandbox_dto.topology_generation.nil?
|
|
1200
|
+
@degraded = sandbox_dto.degraded unless sandbox_dto.degraded.nil?
|
|
1201
|
+
@nodes&.send(:update, sandbox_dto)
|
|
1196
1202
|
end
|
|
1197
1203
|
|
|
1198
1204
|
apply_state(new_state)
|
|
@@ -1212,25 +1218,38 @@ module Daytona
|
|
|
1212
1218
|
|
|
1213
1219
|
# Monitors block not to exceed max execution time.
|
|
1214
1220
|
#
|
|
1221
|
+
# Timeout.timeout alone does not bound the block: its exception is lost when it arrives while
|
|
1222
|
+
# a request is inside libcurl, and the block then runs without a limit. So the block also gets
|
|
1223
|
+
# the deadline to check itself (see #wait_for_state).
|
|
1224
|
+
#
|
|
1215
1225
|
# @param setup [#call, Nil] Optional setup block
|
|
1216
1226
|
# @param timeout [Numeric] Maximum wait time in seconds (defaults to 60 s)
|
|
1217
1227
|
# @param message [String] Error message
|
|
1228
|
+
# @yieldparam deadline [WaitDeadline, nil] When the block must give up; nil when there is no timeout
|
|
1218
1229
|
# @return [void]
|
|
1219
1230
|
# @raise [Daytona::Sdk::Error]
|
|
1220
|
-
def with_timeout(message:, setup:, timeout: DEFAULT_TIMEOUT
|
|
1221
|
-
|
|
1222
|
-
|
|
1231
|
+
def with_timeout(message:, setup:, timeout: DEFAULT_TIMEOUT)
|
|
1232
|
+
effective = setup ? timeout_left_after(setup, timeout:, message:) : timeout
|
|
1233
|
+
|
|
1234
|
+
deadline = WaitDeadline.after(effective, message) if effective.positive?
|
|
1235
|
+
Timeout.timeout(effective, Sdk::TimeoutError, message) { yield deadline }
|
|
1236
|
+
end
|
|
1223
1237
|
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1238
|
+
# Runs +setup+ and returns what is left of +timeout+ after it.
|
|
1239
|
+
#
|
|
1240
|
+
# @param setup [#call]
|
|
1241
|
+
# @param timeout [Numeric] Sandbox::NO_TIMEOUT stays as is
|
|
1242
|
+
# @param message [String] Message of the Sdk::TimeoutError raised when setup used up the whole timeout
|
|
1243
|
+
# @return [Numeric]
|
|
1244
|
+
# @raise [Daytona::Sdk::TimeoutError]
|
|
1245
|
+
def timeout_left_after(setup, timeout:, message:)
|
|
1246
|
+
start_at = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
1247
|
+
setup.call
|
|
1227
1248
|
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
timeout
|
|
1231
|
-
end
|
|
1249
|
+
remaining = timeout - (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start_at)
|
|
1250
|
+
raise Sdk::TimeoutError, message if timeout.positive? && remaining <= 0
|
|
1232
1251
|
|
|
1233
|
-
|
|
1252
|
+
[NO_TIMEOUT, remaining].max
|
|
1234
1253
|
end
|
|
1235
1254
|
|
|
1236
1255
|
# Waits for the Sandbox to reach one of the target states via WebSocket events
|
|
@@ -1239,9 +1258,11 @@ module Daytona
|
|
|
1239
1258
|
# @param target_states [Array<DaytonaApiClient::SandboxState>] States that indicate success.
|
|
1240
1259
|
# @param error_states [Array<DaytonaApiClient::SandboxState>] States that indicate failure.
|
|
1241
1260
|
# @param safe_refresh [Boolean] If true, use refresh_data_safe for polling (swallows transient errors).
|
|
1261
|
+
# @param deadline [WaitDeadline, nil] Raise Sdk::TimeoutError with its message once it passes; nil waits
|
|
1262
|
+
# without a limit.
|
|
1242
1263
|
# @return [void]
|
|
1243
1264
|
# @raise [Daytona::Sdk::Error]
|
|
1244
|
-
def wait_for_state(target_states:, error_states:, safe_refresh: false) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
|
1265
|
+
def wait_for_state(target_states:, error_states:, safe_refresh: false, deadline: nil) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
|
1245
1266
|
ensure_subscribed
|
|
1246
1267
|
subscribed = !@sub_id.nil?
|
|
1247
1268
|
|
|
@@ -1253,6 +1274,8 @@ module Daytona
|
|
|
1253
1274
|
mutex = Mutex.new
|
|
1254
1275
|
state_changed = ConditionVariable.new
|
|
1255
1276
|
result_state = nil
|
|
1277
|
+
# The error reason that came with result_state; a refresh still in flight may replace error_reason after.
|
|
1278
|
+
result_reason = nil
|
|
1256
1279
|
|
|
1257
1280
|
waiter = {
|
|
1258
1281
|
matches: lambda do |new_state|
|
|
@@ -1263,6 +1286,7 @@ module Daytona
|
|
|
1263
1286
|
next if result_state
|
|
1264
1287
|
|
|
1265
1288
|
result_state = new_state
|
|
1289
|
+
result_reason = error_reason
|
|
1266
1290
|
state_changed.signal
|
|
1267
1291
|
end
|
|
1268
1292
|
end
|
|
@@ -1282,21 +1306,26 @@ module Daytona
|
|
|
1282
1306
|
should_refresh = mutex.synchronize do
|
|
1283
1307
|
next false if result_state
|
|
1284
1308
|
|
|
1285
|
-
state_changed.wait(mutex, poll_interval)
|
|
1309
|
+
state_changed.wait(mutex, deadline ? deadline.remaining.clamp(0, poll_interval) : poll_interval)
|
|
1286
1310
|
result_state.nil?
|
|
1287
1311
|
end
|
|
1288
1312
|
next unless should_refresh
|
|
1289
1313
|
end
|
|
1290
1314
|
first_poll = false
|
|
1291
1315
|
|
|
1292
|
-
|
|
1316
|
+
raise Sdk::TimeoutError, deadline.message if deadline&.expired?
|
|
1317
|
+
|
|
1318
|
+
poll_refresh(safe: safe_refresh, http_timeout: deadline&.remaining, deadline:)
|
|
1293
1319
|
raise_destroyed_lifecycle_error_if_needed(target_strings)
|
|
1294
1320
|
|
|
1295
1321
|
mutex.synchronize do
|
|
1296
1322
|
next if result_state
|
|
1297
1323
|
|
|
1298
1324
|
current = state.to_s
|
|
1299
|
-
|
|
1325
|
+
if target_strings.include?(current) || error_strings.include?(current)
|
|
1326
|
+
result_state = current
|
|
1327
|
+
result_reason = error_reason
|
|
1328
|
+
end
|
|
1300
1329
|
end
|
|
1301
1330
|
|
|
1302
1331
|
poll_interval = next_wait_for_state_poll_interval(
|
|
@@ -1308,11 +1337,11 @@ module Daytona
|
|
|
1308
1337
|
|
|
1309
1338
|
if result_state && error_strings.include?(result_state)
|
|
1310
1339
|
raise Sdk::Error,
|
|
1311
|
-
"Sandbox #{id} entered error state: #{result_state}, error reason: #{
|
|
1340
|
+
"Sandbox #{id} entered error state: #{result_state}, error reason: #{result_reason}"
|
|
1312
1341
|
end
|
|
1313
1342
|
rescue Sdk::TimeoutError => e
|
|
1314
1343
|
begin
|
|
1315
|
-
safe_refresh
|
|
1344
|
+
poll_refresh(safe: safe_refresh, http_timeout: deadline && FINAL_REFRESH_TIMEOUT)
|
|
1316
1345
|
rescue StandardError
|
|
1317
1346
|
raise e
|
|
1318
1347
|
end
|
|
@@ -1334,13 +1363,68 @@ module Daytona
|
|
|
1334
1363
|
end
|
|
1335
1364
|
end
|
|
1336
1365
|
|
|
1337
|
-
|
|
1338
|
-
|
|
1366
|
+
# @param api [DaytonaApiClient::SandboxApi] sandbox_api, or a copy of it with an HTTP timeout
|
|
1367
|
+
def refresh_data_safe(api = sandbox_api)
|
|
1368
|
+
refresh_through(api)
|
|
1339
1369
|
rescue DaytonaApiClient::ApiError => e
|
|
1340
1370
|
apply_state(DaytonaApiClient::SandboxState::DESTROYED) if e.code == 404
|
|
1341
1371
|
rescue StandardError # rubocop:disable Lint/SuppressedException
|
|
1342
1372
|
end
|
|
1343
1373
|
|
|
1374
|
+
# Does what #refresh does, recorded as #refresh and renewing the event subscription, but through +api+.
|
|
1375
|
+
#
|
|
1376
|
+
# @param api [DaytonaApiClient::SandboxApi] sandbox_api, or a copy of it with an HTTP timeout
|
|
1377
|
+
# @return [void]
|
|
1378
|
+
def refresh_through(api)
|
|
1379
|
+
return refresh if api.equal?(sandbox_api)
|
|
1380
|
+
|
|
1381
|
+
instrument_as(:refresh, component: 'Sandbox') do
|
|
1382
|
+
ensure_subscribed
|
|
1383
|
+
process_response(api.get_sandbox(id))
|
|
1384
|
+
end
|
|
1385
|
+
end
|
|
1386
|
+
|
|
1387
|
+
# Refreshes the Sandbox data for #wait_for_state.
|
|
1388
|
+
#
|
|
1389
|
+
# @param safe [Boolean] Swallow errors as #refresh_data_safe does, a timed out request included
|
|
1390
|
+
# @param http_timeout [Numeric, nil] Most seconds the request may take; nil leaves the API client's timeout
|
|
1391
|
+
# @param deadline [WaitDeadline, nil] Deadline that http_timeout is the time left on, if any
|
|
1392
|
+
# @return [void]
|
|
1393
|
+
# @raise [Daytona::Sdk::TimeoutError] If safe is false and the request times out at the deadline
|
|
1394
|
+
def poll_refresh(safe:, http_timeout:, deadline: nil)
|
|
1395
|
+
api = http_timeout ? sandbox_api_with_timeout(http_timeout) : sandbox_api
|
|
1396
|
+
safe ? refresh_data_safe(api) : refresh_through(api)
|
|
1397
|
+
rescue DaytonaApiClient::ApiError => e
|
|
1398
|
+
raise unless deadline && deadline_timed_out?(e, http_timeout)
|
|
1399
|
+
|
|
1400
|
+
raise Sdk::TimeoutError, deadline.message
|
|
1401
|
+
end
|
|
1402
|
+
|
|
1403
|
+
# @param error [DaytonaApiClient::ApiError]
|
|
1404
|
+
# @param http_timeout [Numeric]
|
|
1405
|
+
# @return [Boolean] Whether +error+ is the request timing out after +http_timeout+, not after a shorter
|
|
1406
|
+
# timeout of the API client
|
|
1407
|
+
def deadline_timed_out?(error, http_timeout)
|
|
1408
|
+
# The generated client raises this, without a status code, when the request times out.
|
|
1409
|
+
return false unless error.code.nil? && error.message == 'Connection timed out'
|
|
1410
|
+
|
|
1411
|
+
client_timeout = sandbox_api.api_client.config.timeout
|
|
1412
|
+
client_timeout.nil? || !client_timeout.positive? || client_timeout >= http_timeout
|
|
1413
|
+
end
|
|
1414
|
+
|
|
1415
|
+
# A copy of sandbox_api whose requests time out after +timeout+ seconds, or sooner when its API client
|
|
1416
|
+
# already has a shorter timeout. Headers and settings changed on the API client still apply.
|
|
1417
|
+
#
|
|
1418
|
+
# @param timeout [Numeric] HTTP timeout in seconds, raised to 1 ms since 0 means no timeout
|
|
1419
|
+
# @return [DaytonaApiClient::SandboxApi]
|
|
1420
|
+
def sandbox_api_with_timeout(timeout)
|
|
1421
|
+
client = sandbox_api.api_client
|
|
1422
|
+
config = client.config.dup.tap do |c|
|
|
1423
|
+
c.timeout = [[timeout, MIN_HTTP_TIMEOUT].max, c.timeout].compact.select(&:positive?).min
|
|
1424
|
+
end
|
|
1425
|
+
DaytonaApiClient::SandboxApi.new(client.dup.tap { |copy| copy.config = config })
|
|
1426
|
+
end
|
|
1427
|
+
|
|
1344
1428
|
def raise_destroyed_lifecycle_error_if_needed(target_strings)
|
|
1345
1429
|
return unless state.to_s == DaytonaApiClient::SandboxState::DESTROYED.to_s
|
|
1346
1430
|
return if target_strings.include?(DaytonaApiClient::SandboxState::DESTROYED.to_s)
|
|
@@ -1394,6 +1478,9 @@ module Daytona
|
|
|
1394
1478
|
if event_name == 'sandbox.created'
|
|
1395
1479
|
process_response(DaytonaApiClient::Sandbox.build_from_hash(raw))
|
|
1396
1480
|
else
|
|
1481
|
+
# Set before the state: a wait that ends on an error state reports this reason. The event omits the field
|
|
1482
|
+
# when the sandbox has none.
|
|
1483
|
+
@error_reason = raw['errorReason'] if data.key?('sandbox')
|
|
1397
1484
|
new_state = raw['state'] || data['newState']
|
|
1398
1485
|
apply_state(new_state) if new_state
|
|
1399
1486
|
end
|
|
@@ -1429,12 +1516,46 @@ module Daytona
|
|
|
1429
1516
|
POLL_BACKOFF_DELAY = 5.0
|
|
1430
1517
|
private_constant :POLL_BACKOFF_DELAY
|
|
1431
1518
|
|
|
1432
|
-
|
|
1519
|
+
# Most seconds the refresh after a state wait timed out may take.
|
|
1520
|
+
FINAL_REFRESH_TIMEOUT = 5.0
|
|
1521
|
+
private_constant :FINAL_REFRESH_TIMEOUT
|
|
1522
|
+
|
|
1523
|
+
# Shortest HTTP timeout, as 0 means no timeout.
|
|
1524
|
+
MIN_HTTP_TIMEOUT = 0.001
|
|
1525
|
+
private_constant :MIN_HTTP_TIMEOUT
|
|
1526
|
+
|
|
1527
|
+
# When a #with_timeout block must give up, on the monotonic clock, and the message to give up with.
|
|
1528
|
+
WaitDeadline = Struct.new(:at, :message) do
|
|
1529
|
+
def self.after(seconds, message) = new(::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + seconds, message)
|
|
1530
|
+
|
|
1531
|
+
def remaining = at - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
1532
|
+
|
|
1533
|
+
def expired? = remaining <= 0
|
|
1534
|
+
end
|
|
1535
|
+
private_constant :WaitDeadline
|
|
1536
|
+
|
|
1537
|
+
def wait_for_stop(deadline)
|
|
1538
|
+
wait_for_state(
|
|
1539
|
+
target_states: [DaytonaApiClient::SandboxState::STOPPED, DaytonaApiClient::SandboxState::DESTROYED],
|
|
1540
|
+
error_states: [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED],
|
|
1541
|
+
deadline:
|
|
1542
|
+
)
|
|
1543
|
+
end
|
|
1544
|
+
|
|
1545
|
+
def wait_for_resize(deadline)
|
|
1546
|
+
error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
1547
|
+
exclude = [DaytonaApiClient::SandboxState::RESIZING] + error_states
|
|
1548
|
+
target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
|
|
1549
|
+
|
|
1550
|
+
wait_for_state(target_states:, error_states:, deadline:)
|
|
1551
|
+
end
|
|
1552
|
+
|
|
1553
|
+
def wait_for_snapshot_complete(deadline)
|
|
1433
1554
|
error_states = [DaytonaApiClient::SandboxState::ERROR, DaytonaApiClient::SandboxState::BUILD_FAILED]
|
|
1434
1555
|
exclude = [DaytonaApiClient::SandboxState::SNAPSHOTTING] + error_states
|
|
1435
1556
|
target_states = DaytonaApiClient::SandboxState.all_vars.reject { |s| exclude.include?(s) }
|
|
1436
1557
|
|
|
1437
|
-
wait_for_state(target_states:, error_states:)
|
|
1558
|
+
wait_for_state(target_states:, error_states:, deadline:)
|
|
1438
1559
|
end
|
|
1439
1560
|
end
|
|
1440
1561
|
end
|