-
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
6 changed files
with
229 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Remote state middleware for Inngest. This middleware allows you to store state | ||
where you want, rather than in Inngest's infrastructure. This is useful for: | ||
- Reducing bandwidth to/from the Inngest server. | ||
- Avoiding step output size limits. | ||
NOT STABLE! This is an experimental feature and may change in the future. If | ||
you'd like to use it, we recommend copying this file into your source code. | ||
""" | ||
|
||
from .in_memory_driver import InMemoryDriver | ||
from .middleware import RemoteStateMiddleware, StateDriver | ||
|
||
__all__ = [ | ||
"InMemoryDriver", | ||
"RemoteStateMiddleware", | ||
"StateDriver", | ||
] |
66 changes: 66 additions & 0 deletions
66
inngest/experimental/remote_state_middleware/in_memory_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,66 @@ | ||
import secrets | ||
import string | ||
import typing | ||
|
||
import pydantic | ||
|
||
import inngest | ||
|
||
from .middleware import StateDriver | ||
|
||
|
||
class _StatePlaceholder(pydantic.BaseModel): | ||
__REMOTE_STATE__: typing.Literal[True] = True | ||
key: str | ||
|
||
|
||
class InMemoryDriver(StateDriver): | ||
""" | ||
In-memory driver for remote state middleware. | ||
""" | ||
|
||
# Marker to indicate that the data is stored remotely. | ||
_marker: typing.Final = "__REMOTE_STATE__" | ||
|
||
def __init__(self) -> None: # noqa: D107 | ||
self._data: dict[str, object] = {} | ||
|
||
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 | ||
|
||
try: | ||
placeholder = _StatePlaceholder.model_validate(step.data) | ||
except pydantic.ValidationError: | ||
continue | ||
|
||
step.data = self._data[placeholder.key] | ||
|
||
def save_step( | ||
self, | ||
value: object, | ||
) -> dict[str, object]: | ||
""" | ||
Save a step's output to the remote store and return a placeholder. | ||
""" | ||
|
||
key = self._create_key() | ||
self._data[key] = value | ||
|
||
placeholder: dict[str, object] = { | ||
self._marker: True, | ||
**_StatePlaceholder(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
7 changes: 5 additions & 2 deletions
7
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
119 changes: 119 additions & 0 deletions
119
tests/test_experimental/test_remote_state_middleware/cases/step_failed.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,119 @@ | ||
""" | ||
Ensure step and function output is encrypted and decrypted correctly | ||
""" | ||
|
||
import json | ||
|
||
import inngest | ||
import tests.helper | ||
from inngest._internal import server_lib | ||
from inngest.experimental import remote_state_middleware | ||
|
||
from . import base | ||
|
||
|
||
class _State(base.BaseState): | ||
event: inngest.Event | ||
events: list[inngest.Event] | ||
|
||
|
||
def create( | ||
client: inngest.Inngest, | ||
framework: server_lib.Framework, | ||
is_sync: bool, | ||
) -> base.Case: | ||
test_name = base.create_test_name(__file__) | ||
event_name = base.create_event_name(framework, test_name) | ||
fn_id = base.create_fn_id(test_name) | ||
state = _State() | ||
driver = remote_state_middleware.InMemoryDriver() | ||
|
||
@client.create_function( | ||
fn_id=fn_id, | ||
middleware=[ | ||
remote_state_middleware.RemoteStateMiddleware.factory(driver) | ||
], | ||
retries=0, | ||
trigger=inngest.TriggerEvent(event=event_name), | ||
) | ||
def fn_sync( | ||
ctx: inngest.Context, | ||
step: inngest.StepSync, | ||
) -> str: | ||
state.run_id = ctx.run_id | ||
|
||
def _step() -> str: | ||
raise Exception("oh no") | ||
|
||
try: | ||
step.run("step_1", _step) | ||
except Exception as e: | ||
print("hi", str(e)) | ||
return str(e) | ||
|
||
return "unreachable" | ||
|
||
@client.create_function( | ||
fn_id=fn_id, | ||
middleware=[ | ||
remote_state_middleware.RemoteStateMiddleware.factory(driver) | ||
], | ||
retries=0, | ||
trigger=inngest.TriggerEvent(event=event_name), | ||
) | ||
async def fn_async( | ||
ctx: inngest.Context, | ||
step: inngest.Step, | ||
) -> str: | ||
state.run_id = ctx.run_id | ||
|
||
state.run_id = ctx.run_id | ||
|
||
def _step() -> str: | ||
raise Exception("oh no") | ||
|
||
try: | ||
await step.run("step_1", _step) | ||
except Exception as e: | ||
return str(e) | ||
|
||
return "unreachable" | ||
|
||
async def run_test(self: base.TestClass) -> None: | ||
self.client.send_sync(inngest.Event(name=event_name)) | ||
|
||
run_id = state.wait_for_run_id() | ||
run = tests.helper.client.wait_for_run_status( | ||
run_id, | ||
tests.helper.RunStatus.COMPLETED, | ||
) | ||
|
||
# Ensure that step_1 output is encrypted and its value is correct | ||
output = json.loads( | ||
tests.helper.client.get_step_output( | ||
run_id=run_id, | ||
step_id="step_1", | ||
) | ||
) | ||
assert isinstance(output, dict) | ||
assert output.get("data") is None | ||
error = output.get("error") | ||
assert isinstance(error, dict) | ||
|
||
# Ensure that the error data was not remotely stored. | ||
assert driver._marker not in error | ||
assert error.get("message") == "oh no" | ||
|
||
assert run.output is not None | ||
assert json.loads(run.output) == "oh no" | ||
|
||
if is_sync: | ||
fn = fn_sync | ||
else: | ||
fn = fn_async | ||
|
||
return base.Case( | ||
fn=fn, | ||
run_test=run_test, | ||
name=test_name, | ||
) |
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