Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `max_wait_time`, `initial_interval`, `max_interval`, `backoff_coefficient` keyword arguments to `instances.create()`

### Changed

- Cap `instances.create()` retry interval to 5 seconds; add exponential backoff; increase default `max_wait_time` from 60 to 180 seconds

## [1.14.0] - 2025-08-15

### Added
Expand Down
27 changes: 18 additions & 9 deletions datacrunch/instances/instances.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import itertools
from typing import List, Union, Optional, Dict, Literal
from dataclasses import dataclass
from dataclasses_json import dataclass_json
Expand Down Expand Up @@ -123,7 +124,12 @@ def create(self,
is_spot: bool = False,
contract: Optional[Contract] = None,
pricing: Optional[Pricing] = None,
coupon: Optional[str] = None) -> Instance:
coupon: Optional[str] = None,
*,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* is a keyword-only argument marker, probably a good idea to do it for all new arguments.

https://docs.python.org/3/glossary.html#term-parameter

max_wait_time: float = 180,
initial_interval: float = 0.5,
max_interval: float = 5,
backoff_coefficient: float = 2.0) -> Instance:
"""Creates and deploys a new cloud instance.

Args:
Expand All @@ -141,6 +147,10 @@ def create(self,
contract: Optional contract type for the instance.
pricing: Optional pricing model for the instance.
coupon: Optional coupon code for discounts.
max_wait_time: Maximum total wait for the instance to start provisioning, in seconds (default: 180)
initial_interval: Initial interval, in seconds (default: 0.5)
max_interval: The longest single delay allowed between retries, in seconds (default: 5)
backoff_coefficient: Coefficient to calculate the next retry interval (default 2.0)

Returns:
The newly created instance object.
Expand Down Expand Up @@ -169,20 +179,19 @@ def create(self,
id = self._http_client.post(INSTANCES_ENDPOINT, json=payload).text

# Wait for instance to enter provisioning state with timeout
MAX_WAIT_TIME = 60 # Maximum wait time in seconds
POLL_INTERVAL = 0.5 # Time between status checks

start_time = time.time()
while True:
deadline = time.monotonic() + max_wait_time
for i in itertools.count():
instance = self.get_by_id(id)
if instance.status != InstanceStatus.ORDERED:
return instance

if time.time() - start_time > MAX_WAIT_TIME:
now = time.monotonic()
Comment thread
shamrin marked this conversation as resolved.
if now >= deadline:
raise TimeoutError(
f"Instance {id} did not enter provisioning state within {MAX_WAIT_TIME} seconds")
f"Instance {id} did not enter provisioning state within {max_wait_time:.1f} seconds")

time.sleep(POLL_INTERVAL)
interval = min(initial_interval * backoff_coefficient ** i, max_interval, deadline - now)
time.sleep(interval)

def action(self, id_list: Union[List[str], str], action: str, volume_ids: Optional[List[str]] = None) -> None:
"""Performs an action on one or more instances.
Expand Down