Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invoke: app ID default and data type #186

Merged
merged 2 commits into from
Dec 6, 2024
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
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
},
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.organizeImports.ruff": "explicit"
},
"editor.formatOnSave": true,
"mypy-type-checker.importStrategy": "fromEnvironment",
"mypy-type-checker.preferDaemon": true,
"python.analysis.typeCheckingMode": "basic",
Expand Down
11 changes: 7 additions & 4 deletions inngest/_internal/step_lib/step_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ async def invoke(
step_id: str,
*,
function: function.Function,
data: typing.Optional[types.JSON] = None,
data: typing.Optional[typing.Mapping[str, object]] = None,
timeout: typing.Union[int, datetime.timedelta, None] = None,
user: typing.Optional[types.JSON] = None,
user: typing.Optional[typing.Mapping[str, object]] = None,
v: typing.Optional[str] = None,
) -> object:
"""
Expand Down Expand Up @@ -79,9 +79,9 @@ async def invoke_by_id(
*,
app_id: typing.Optional[str] = None,
function_id: str,
data: typing.Optional[types.JSON] = None,
data: typing.Optional[typing.Mapping[str, object]] = None,
timeout: typing.Union[int, datetime.timedelta, None] = None,
user: typing.Optional[types.JSON] = None,
user: typing.Optional[typing.Mapping[str, object]] = None,
v: typing.Optional[str] = None,
) -> object:
"""
Expand All @@ -106,6 +106,9 @@ async def invoke_by_id(
v: Will become `event.v` in the invoked function.
"""

if app_id is None:
app_id = self._client.app_id

parsed_step_id = self._parse_step_id(step_id)

timeout_str = transforms.to_maybe_duration_str(timeout)
Expand Down
11 changes: 7 additions & 4 deletions inngest/_internal/step_lib/step_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def invoke(
step_id: str,
*,
function: function.Function,
data: typing.Optional[types.JSON] = None,
data: typing.Optional[typing.Mapping[str, object]] = None,
timeout: typing.Union[int, datetime.timedelta, None] = None,
user: typing.Optional[types.JSON] = None,
user: typing.Optional[typing.Mapping[str, object]] = None,
v: typing.Optional[str] = None,
) -> object:
"""
Expand Down Expand Up @@ -84,9 +84,9 @@ def invoke_by_id(
*,
app_id: typing.Optional[str] = None,
function_id: str,
data: typing.Optional[types.JSON] = None,
data: typing.Optional[typing.Mapping[str, object]] = None,
timeout: typing.Union[int, datetime.timedelta, None] = None,
user: typing.Optional[types.JSON] = None,
user: typing.Optional[typing.Mapping[str, object]] = None,
v: typing.Optional[str] = None,
) -> object:
"""
Expand All @@ -111,6 +111,9 @@ def invoke_by_id(
v: Will become `event.v` in the invoked function.
"""

if app_id is None:
app_id = self._client.app_id

parsed_step_id = self._parse_step_id(step_id)

memo = self._get_memo_sync(parsed_step_id.hashed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
decrypt_unexpected_encryption_field,
encrypt_overridden_encryption_field,
fallback_decryption_key,
invoke,
step_and_fn_output,
)

Expand All @@ -17,6 +18,7 @@
decrypt_unexpected_encryption_field,
encrypt_overridden_encryption_field,
fallback_decryption_key,
invoke,
step_and_fn_output,
)

Expand Down
125 changes: 125 additions & 0 deletions tests/test_experimental/test_encryption_middleware/cases/invoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Ensure that invoke works.
"""

import nacl.encoding
import nacl.hash
import nacl.secret
import nacl.utils

import inngest
import tests.helper
from inngest._internal import server_lib
from inngest.experimental.encryption_middleware import EncryptionMiddleware

from . import base

_secret_key = "my-secret-key"


enc = base.Encryptor(
nacl.hash.blake2b(
_secret_key.encode("utf-8"),
digest_size=nacl.secret.SecretBox.KEY_SIZE,
)
)


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()

@client.create_function(
fn_id=f"{fn_id}/child",
middleware=[EncryptionMiddleware.factory(_secret_key)],
retries=0,
trigger=inngest.TriggerEvent(event="never"),
)
def child_fn_sync(
ctx: inngest.Context,
step: inngest.StepSync,
) -> str:
return f"Hello, {ctx.event.data['name']}!"

@client.create_function(
fn_id=fn_id,
middleware=[EncryptionMiddleware.factory(_secret_key)],
retries=0,
trigger=inngest.TriggerEvent(event=event_name),
)
def fn_sync(
ctx: inngest.Context,
step: inngest.StepSync,
) -> None:
state.run_id = ctx.run_id

result = step.invoke(
"invoke",
function=child_fn_sync,
data={"name": "Alice"},
)
assert isinstance(result, str)
assert result == "Hello, Alice!"

@client.create_function(
fn_id=f"{fn_id}/child",
middleware=[EncryptionMiddleware.factory(_secret_key)],
retries=0,
trigger=inngest.TriggerEvent(event="never"),
)
async def child_fn_async(
ctx: inngest.Context,
step: inngest.Step,
) -> str:
return f"Hello, {ctx.event.data['name']}!"

@client.create_function(
fn_id=fn_id,
middleware=[EncryptionMiddleware.factory(_secret_key)],
retries=0,
trigger=inngest.TriggerEvent(event=event_name),
)
async def fn_async(
ctx: inngest.Context,
step: inngest.Step,
) -> None:
state.run_id = ctx.run_id

result = step.invoke(
"invoke",
function=child_fn_sync,
data={"name": "Alice"},
)
assert isinstance(result, str)
assert result == "Hello, Alice!"

async def run_test(self: base.TestClass) -> None:
self.client.send_sync(inngest.Event(name=event_name))

run_id = state.wait_for_run_id()
tests.helper.client.wait_for_run_status(
run_id,
tests.helper.RunStatus.COMPLETED,
)

if is_sync:
fn = [child_fn_sync, fn_sync]
else:
fn = [child_fn_async, fn_async]

return base.Case(
fn=fn,
run_test=run_test,
name=test_name,
)
6 changes: 4 additions & 2 deletions tests/test_function/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@

django.conf.settings.configure(
ALLOWED_HOSTS=["*"],
# We send up to 4 MB
DATA_UPLOAD_MAX_MEMORY_SIZE=1024 * 1024 * 5,
# We send up to 5 MB
# TODO: Change this back to 5 MB after we release a Dev Server version with
# the max request size fix (https://github.com/inngest/inngest/pull/2009).
DATA_UPLOAD_MAX_MEMORY_SIZE=1024 * 1024 * 8,
DEBUG=True,
ROOT_URLCONF=__name__,
SECRET_KEY="fake",
Expand Down
Loading