-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
305 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
inngest/experimental/remote_state_middleware/s3_driver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import json | ||
import secrets | ||
import string | ||
import typing | ||
|
||
import boto3 | ||
import pydantic | ||
|
||
import inngest | ||
|
||
from .middleware import StateDriver | ||
|
||
|
||
class _StatePlaceholder(pydantic.BaseModel): | ||
bucket: str | ||
key: str | ||
|
||
|
||
class S3Driver(StateDriver): | ||
""" | ||
S3 driver for remote state middleware. | ||
""" | ||
|
||
# Marker to indicate that the data is stored remotely. | ||
_marker: typing.Final = "__REMOTE_STATE__" | ||
|
||
# Marker to indicate which strategy was used. This is useful for knowing | ||
# whether the official S3 driver was used. | ||
_strategy_marker: typing.Final = "__STRATEGY__" | ||
|
||
_strategy_identifier: typing.Final = "inngest/s3" | ||
|
||
def __init__( # noqa: D107 | ||
self, | ||
*, | ||
bucket: str, | ||
endpoint_url: typing.Optional[str] = None, | ||
region_name: str, | ||
) -> None: | ||
self._bucket = bucket | ||
self._client = boto3.client( | ||
"s3", | ||
endpoint_url=endpoint_url, | ||
region_name=region_name, | ||
) | ||
|
||
def _create_key(self) -> str: | ||
chars = string.ascii_letters + string.digits | ||
return "".join(secrets.choice(chars) for _ in range(32)) | ||
|
||
def load_steps(self, steps: inngest.StepMemos) -> None: | ||
""" | ||
Hydrate steps with remote state if necessary. | ||
""" | ||
|
||
for step in steps.values(): | ||
if not isinstance(step.data, dict): | ||
continue | ||
if self._marker not in step.data: | ||
continue | ||
if self._strategy_marker not in step.data: | ||
continue | ||
if step.data[self._strategy_marker] != self._strategy_identifier: | ||
continue | ||
|
||
placeholder = _StatePlaceholder.model_validate(step.data) | ||
|
||
step.data = json.loads( | ||
self._client.get_object( | ||
Bucket=placeholder.bucket, | ||
Key=placeholder.key, | ||
)["Body"] | ||
.read() | ||
.decode() | ||
) | ||
|
||
def save_step( | ||
self, | ||
run_id: str, | ||
value: object, | ||
) -> dict[str, object]: | ||
""" | ||
Save a step's output to the remote store and return a placeholder. | ||
""" | ||
|
||
key = f"inngest/remote_state/{run_id}/{self._create_key()}" | ||
self._client.create_bucket(Bucket=self._bucket) | ||
self._client.put_object( | ||
Body=json.dumps(value), | ||
Bucket=self._bucket, | ||
Key=key, | ||
) | ||
|
||
placeholder: dict[str, object] = { | ||
self._marker: True, | ||
self._strategy_marker: self._strategy_identifier, | ||
**_StatePlaceholder(bucket=self._bucket, key=key).model_dump(), | ||
} | ||
|
||
return placeholder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,21 @@ | ||
import random | ||
import contextlib | ||
import socket | ||
import time | ||
import typing | ||
|
||
HOST: typing.Final = "0.0.0.0" | ||
|
||
_used_ports: set[int] = set() | ||
_min_port: typing.Final = 9000 | ||
_max_port: typing.Final = 9999 | ||
|
||
|
||
def get_available_port() -> int: | ||
start_time = time.time() | ||
|
||
while True: | ||
if time.time() - start_time > 5: | ||
raise Exception("timeout finding available port") | ||
|
||
port = random.randint(9000, 9999) | ||
|
||
if port in _used_ports: | ||
continue | ||
|
||
if not _is_port_available(port): | ||
continue | ||
|
||
_used_ports.add(port) | ||
return port | ||
|
||
|
||
def _is_port_available(port: int) -> bool: | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
try: | ||
s.bind((HOST, port)) | ||
return True | ||
except OSError: | ||
return False | ||
for port in range(_min_port, _max_port + 1): | ||
with contextlib.closing( | ||
socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
) as sock: | ||
try: | ||
sock.bind((HOST, port)) | ||
return port | ||
except OSError: | ||
continue | ||
|
||
raise Exception("failed to find available port") |
5 changes: 3 additions & 2 deletions
5
tests/test_experimental/test_remote_state_middleware/cases/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.