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

Add idempotent logger middleware #10

Merged
merged 2 commits into from
Nov 3, 2023
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
},
"editor.formatOnSave": true,
"isort.args": ["--profile", "black"],
"isort.check": true
"isort.check": true,
"mypy-type-checker.preferDaemon": true
}
15 changes: 5 additions & 10 deletions examples/flask/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import logging

import flask
import src.inngest
from src.inngest import inngest_client

import examples.functions
import inngest.flask
from examples import functions

app = flask.Flask(__name__)


log = logging.getLogger("werkzeug")
log.setLevel(logging.ERROR)
inngest_client.set_logger(app.logger)


inngest.flask.serve(
app,
src.inngest.inngest_client,
examples.functions.functions_sync,
inngest_client,
functions.functions_sync,
)
app.run(port=8000)
7 changes: 4 additions & 3 deletions inngest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
TriggerCron,
TriggerEvent,
)
from ._internal.middleware_lib import CallInputTransform

# TODO: Uncomment when middleware is ready for external use.
# from ._internal.middleware_lib import (
# Middleware,
# MiddlewareSync,
# )
# from ._internal.execution import TransformableCallInput
from ._internal.step_lib import Step, StepSync
from ._internal.types import Serializable
from ._internal.types import Logger, Serializable

__all__ = [
"Batch",
Expand All @@ -28,10 +28,11 @@
"Event",
"Function",
"Inngest",
"CallInputTransform",
"Logger",
# TODO: Uncomment when middleware is ready for external use.
# "Middleware",
# "MiddlewareSync",
# "TransformableCallInput",
"NonRetriableError",
"RateLimit",
"Serializable",
Expand Down
26 changes: 22 additions & 4 deletions inngest/_internal/client_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,39 @@
import logging
import os
import time
import typing
import urllib.parse

import httpx

from . import const, env, errors, event_lib, middleware_lib, net, result
from . import const, env, errors, event_lib, middleware_lib, net, result, types


class Inngest:
middleware: list[
typing.Type[middleware_lib.Middleware | middleware_lib.MiddlewareSync]
]

def __init__(
self,
*,
app_id: str,
base_url: str | None = None,
event_key: str | None = None,
is_production: bool | None = None,
logger: logging.Logger | None = None,
logger: types.Logger | None = None,
middleware: list[
middleware_lib.Middleware | middleware_lib.MiddlewareSync
typing.Type[
middleware_lib.Middleware | middleware_lib.MiddlewareSync
]
]
| None = None,
) -> None:
self.app_id = app_id
self.base_url = base_url
self.is_production = is_production or env.is_prod()
self.logger = logger or logging.getLogger(__name__)
self.middleware = middleware_lib.MiddlewareManager(middleware or [])
self.middleware = middleware or []

if event_key is None:
if not self.is_production:
Expand Down Expand Up @@ -80,6 +87,14 @@ def _build_send_request(
)
)

def add_middleware(
self,
middleware: typing.Type[
middleware_lib.Middleware | middleware_lib.MiddlewareSync
],
) -> None:
self.middleware = [*self.middleware, middleware]

async def send(
self,
events: event_lib.Event | list[event_lib.Event],
Expand Down Expand Up @@ -110,6 +125,9 @@ def send_sync(
raise err
return ids

def set_logger(self, logger: types.Logger) -> None:
self.logger = logger


def _extract_ids(body: object) -> list[str]:
if not isinstance(body, dict) or "ids" not in body:
Expand Down
Loading
Loading