dbos 0.6.0a3__py3-none-any.whl → 0.7.0a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dbos might be problematic. Click here for more details.
- dbos/__init__.py +2 -0
- dbos/dbos.py +25 -6
- dbos/dbos_config.py +2 -1
- dbos/kafka.py +94 -0
- dbos/kafka_message.py +15 -0
- {dbos-0.6.0a3.dist-info → dbos-0.7.0a0.dist-info}/METADATA +1 -1
- {dbos-0.6.0a3.dist-info → dbos-0.7.0a0.dist-info}/RECORD +10 -8
- {dbos-0.6.0a3.dist-info → dbos-0.7.0a0.dist-info}/WHEEL +0 -0
- {dbos-0.6.0a3.dist-info → dbos-0.7.0a0.dist-info}/entry_points.txt +0 -0
- {dbos-0.6.0a3.dist-info → dbos-0.7.0a0.dist-info}/licenses/LICENSE +0 -0
dbos/__init__.py
CHANGED
|
@@ -2,6 +2,7 @@ from . import error as error
|
|
|
2
2
|
from .context import DBOSContextEnsure, SetWorkflowID
|
|
3
3
|
from .dbos import DBOS, DBOSConfiguredInstance, WorkflowHandle, WorkflowStatus
|
|
4
4
|
from .dbos_config import ConfigFile, get_dbos_database_url, load_config
|
|
5
|
+
from .kafka_message import KafkaMessage
|
|
5
6
|
from .system_database import GetWorkflowsInput, WorkflowStatusString
|
|
6
7
|
|
|
7
8
|
__all__ = [
|
|
@@ -10,6 +11,7 @@ __all__ = [
|
|
|
10
11
|
"DBOSConfiguredInstance",
|
|
11
12
|
"DBOSContextEnsure",
|
|
12
13
|
"GetWorkflowsInput",
|
|
14
|
+
"KafkaMessage",
|
|
13
15
|
"SetWorkflowID",
|
|
14
16
|
"WorkflowHandle",
|
|
15
17
|
"WorkflowStatus",
|
dbos/dbos.py
CHANGED
|
@@ -54,11 +54,14 @@ from .tracer import dbos_tracer
|
|
|
54
54
|
|
|
55
55
|
if TYPE_CHECKING:
|
|
56
56
|
from fastapi import FastAPI
|
|
57
|
+
from dbos.kafka import KafkaConsumerWorkflow
|
|
57
58
|
from .request import Request
|
|
58
59
|
from flask import Flask
|
|
59
60
|
|
|
60
61
|
from sqlalchemy.orm import Session
|
|
61
62
|
|
|
63
|
+
from dbos.request import Request
|
|
64
|
+
|
|
62
65
|
if sys.version_info < (3, 10):
|
|
63
66
|
from typing_extensions import ParamSpec, TypeAlias
|
|
64
67
|
else:
|
|
@@ -506,6 +509,20 @@ class DBOS:
|
|
|
506
509
|
|
|
507
510
|
return scheduled(_get_or_create_dbos_registry(), cron)
|
|
508
511
|
|
|
512
|
+
@classmethod
|
|
513
|
+
def kafka_consumer(
|
|
514
|
+
cls, config: dict[str, Any], topics: list[str]
|
|
515
|
+
) -> Callable[[KafkaConsumerWorkflow], KafkaConsumerWorkflow]:
|
|
516
|
+
"""Decorate a function to be used as a Kafka consumer."""
|
|
517
|
+
try:
|
|
518
|
+
from dbos.kafka import kafka_consumer
|
|
519
|
+
|
|
520
|
+
return kafka_consumer(_get_or_create_dbos_registry(), config, topics)
|
|
521
|
+
except ModuleNotFoundError as e:
|
|
522
|
+
raise DBOSException(
|
|
523
|
+
f"{e.name} dependency not found. Please install {e.name} via your package manager."
|
|
524
|
+
) from e
|
|
525
|
+
|
|
509
526
|
@classmethod
|
|
510
527
|
def start_workflow(
|
|
511
528
|
cls,
|
|
@@ -714,6 +731,12 @@ class DBOS:
|
|
|
714
731
|
ctx = assert_current_dbos_context()
|
|
715
732
|
return ctx.authenticated_roles
|
|
716
733
|
|
|
734
|
+
@classproperty
|
|
735
|
+
def assumed_role(cls) -> Optional[str]:
|
|
736
|
+
"""Return the role currently assumed by the authenticated user, if any, associated with the current context."""
|
|
737
|
+
ctx = assert_current_dbos_context()
|
|
738
|
+
return ctx.assumed_role
|
|
739
|
+
|
|
717
740
|
@classmethod
|
|
718
741
|
def set_authentication(
|
|
719
742
|
cls, authenticated_user: Optional[str], authenticated_roles: Optional[List[str]]
|
|
@@ -800,13 +823,9 @@ class DBOSConfiguredInstance:
|
|
|
800
823
|
|
|
801
824
|
"""
|
|
802
825
|
|
|
803
|
-
def __init__(self, config_name: str
|
|
826
|
+
def __init__(self, config_name: str) -> None:
|
|
804
827
|
self.config_name = config_name
|
|
805
|
-
|
|
806
|
-
assert isinstance(dbos, DBOS)
|
|
807
|
-
dbos._registry.register_instance(self)
|
|
808
|
-
else:
|
|
809
|
-
DBOS.register_instance(self)
|
|
828
|
+
DBOS.register_instance(self)
|
|
810
829
|
|
|
811
830
|
|
|
812
831
|
# Apps that import DBOS probably don't exit. If they do, let's see if
|
dbos/dbos_config.py
CHANGED
|
@@ -2,7 +2,7 @@ import json
|
|
|
2
2
|
import os
|
|
3
3
|
import re
|
|
4
4
|
from importlib import resources
|
|
5
|
-
from typing import Dict, List, Optional, TypedDict
|
|
5
|
+
from typing import Any, Dict, List, Optional, TypedDict
|
|
6
6
|
|
|
7
7
|
import yaml
|
|
8
8
|
from jsonschema import ValidationError, validate
|
|
@@ -69,6 +69,7 @@ class ConfigFile(TypedDict, total=False):
|
|
|
69
69
|
database: DatabaseConfig
|
|
70
70
|
telemetry: Optional[TelemetryConfig]
|
|
71
71
|
env: Dict[str, str]
|
|
72
|
+
application: Dict[str, Any]
|
|
72
73
|
|
|
73
74
|
|
|
74
75
|
def substitute_env_vars(content: str) -> str:
|
dbos/kafka.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
import traceback
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING, Any, Callable, Generator, NoReturn, Optional, Union
|
|
5
|
+
|
|
6
|
+
from confluent_kafka import Consumer, KafkaError, KafkaException
|
|
7
|
+
from confluent_kafka import Message as CTypeMessage
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from dbos.dbos import _DBOSRegistry
|
|
11
|
+
|
|
12
|
+
from .context import SetWorkflowID
|
|
13
|
+
from .kafka_message import KafkaMessage
|
|
14
|
+
from .logger import dbos_logger
|
|
15
|
+
|
|
16
|
+
KafkaConsumerWorkflow = Callable[[KafkaMessage], None]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _kafka_consumer_loop(
|
|
20
|
+
func: KafkaConsumerWorkflow,
|
|
21
|
+
config: dict[str, Any],
|
|
22
|
+
topics: list[str],
|
|
23
|
+
stop_event: threading.Event,
|
|
24
|
+
) -> None:
|
|
25
|
+
|
|
26
|
+
def on_error(err: KafkaError) -> NoReturn:
|
|
27
|
+
raise KafkaException(err)
|
|
28
|
+
|
|
29
|
+
config["error_cb"] = on_error
|
|
30
|
+
if "auto.offset.reset" not in config:
|
|
31
|
+
config["auto.offset.reset"] = "earliest"
|
|
32
|
+
|
|
33
|
+
consumer = Consumer(config)
|
|
34
|
+
try:
|
|
35
|
+
consumer.subscribe(topics)
|
|
36
|
+
while not stop_event.is_set():
|
|
37
|
+
cmsg = consumer.poll(1.0)
|
|
38
|
+
|
|
39
|
+
if stop_event.is_set():
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
if cmsg is None:
|
|
43
|
+
continue
|
|
44
|
+
|
|
45
|
+
err = cmsg.error()
|
|
46
|
+
if err is not None:
|
|
47
|
+
dbos_logger.error(
|
|
48
|
+
f"Kafka error {err.code()} ({err.name()}): {err.str()}"
|
|
49
|
+
)
|
|
50
|
+
# fatal errors require an updated consumer instance
|
|
51
|
+
if err.code() == KafkaError._FATAL or err.fatal():
|
|
52
|
+
original_consumer = consumer
|
|
53
|
+
try:
|
|
54
|
+
consumer = Consumer(config)
|
|
55
|
+
consumer.subscribe(topics)
|
|
56
|
+
finally:
|
|
57
|
+
original_consumer.close()
|
|
58
|
+
else:
|
|
59
|
+
msg = KafkaMessage(
|
|
60
|
+
headers=cmsg.headers(),
|
|
61
|
+
key=cmsg.key(),
|
|
62
|
+
latency=cmsg.latency(),
|
|
63
|
+
leader_epoch=cmsg.leader_epoch(),
|
|
64
|
+
offset=cmsg.offset(),
|
|
65
|
+
partition=cmsg.partition(),
|
|
66
|
+
timestamp=cmsg.timestamp(),
|
|
67
|
+
topic=cmsg.topic(),
|
|
68
|
+
value=cmsg.value(),
|
|
69
|
+
)
|
|
70
|
+
with SetWorkflowID(
|
|
71
|
+
f"kafka-unique-id-{msg.topic}-{msg.partition}-{msg.offset}"
|
|
72
|
+
):
|
|
73
|
+
try:
|
|
74
|
+
func(msg)
|
|
75
|
+
except Exception as e:
|
|
76
|
+
dbos_logger.error(
|
|
77
|
+
f"Exception encountered in Kafka consumer: {traceback.format_exc()}"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
finally:
|
|
81
|
+
consumer.close()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def kafka_consumer(
|
|
85
|
+
dbosreg: "_DBOSRegistry", config: dict[str, Any], topics: list[str]
|
|
86
|
+
) -> Callable[[KafkaConsumerWorkflow], KafkaConsumerWorkflow]:
|
|
87
|
+
def decorator(func: KafkaConsumerWorkflow) -> KafkaConsumerWorkflow:
|
|
88
|
+
stop_event = threading.Event()
|
|
89
|
+
dbosreg.register_poller(
|
|
90
|
+
stop_event, _kafka_consumer_loop, func, config, topics, stop_event
|
|
91
|
+
)
|
|
92
|
+
return func
|
|
93
|
+
|
|
94
|
+
return decorator
|
dbos/kafka_message.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Optional, Union
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@dataclass
|
|
6
|
+
class KafkaMessage:
|
|
7
|
+
headers: Optional[list[tuple[str, Union[str, bytes]]]]
|
|
8
|
+
key: Optional[Union[str, bytes]]
|
|
9
|
+
latency: Optional[float]
|
|
10
|
+
leader_epoch: Optional[int]
|
|
11
|
+
offset: Optional[int]
|
|
12
|
+
partition: Optional[int]
|
|
13
|
+
timestamp: tuple[int, int]
|
|
14
|
+
topic: Optional[str]
|
|
15
|
+
value: Optional[Union[str, bytes]]
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
dbos-0.
|
|
2
|
-
dbos-0.
|
|
3
|
-
dbos-0.
|
|
4
|
-
dbos-0.
|
|
5
|
-
dbos/__init__.py,sha256=
|
|
1
|
+
dbos-0.7.0a0.dist-info/METADATA,sha256=x4O-g52DZCDhnpYf5SKAfG872Ee83HodkUzffOAhKhA,5000
|
|
2
|
+
dbos-0.7.0a0.dist-info/WHEEL,sha256=rSwsxJWe3vzyR5HCwjWXQruDgschpei4h_giTm0dJVE,90
|
|
3
|
+
dbos-0.7.0a0.dist-info/entry_points.txt,sha256=3PmOPbM4FYxEmggRRdJw0oAsiBzKR8U0yx7bmwUmMOM,39
|
|
4
|
+
dbos-0.7.0a0.dist-info/licenses/LICENSE,sha256=VGZit_a5-kdw9WT6fY5jxAWVwGQzgLFyPWrcVVUhVNU,1067
|
|
5
|
+
dbos/__init__.py,sha256=heuB3bqRXlVdfea9sKHBIVKSqFP6UuwhQecQfV4hyas,642
|
|
6
6
|
dbos/admin_sever.py,sha256=Qg5T3YRrbPW05PR_99yAaxgo1ugQrAp_uTeTqSfjm_k,3397
|
|
7
7
|
dbos/application_database.py,sha256=1K3kE96BgGi_QWOd2heXluyNTwFAwlUVuAR6JKKUqf0,5659
|
|
8
8
|
dbos/cli.py,sha256=YARlQiWHUwFni-fEOr0k5P_-pqPS4xkywj_B0oTMXn0,8318
|
|
9
9
|
dbos/context.py,sha256=NVMGyvAa2RIiBVspvDz-8MBk_BQyGyYdPdorgO-GSng,16407
|
|
10
10
|
dbos/core.py,sha256=jeqO8DABPAUrFlJXOfRfFDSnA8BGwiPnMa1JNbGuYLs,28584
|
|
11
11
|
dbos/dbos-config.schema.json,sha256=azpfmoDZg7WfSy3kvIsk9iEiKB_-VZt03VEOoXJAkqE,5331
|
|
12
|
-
dbos/dbos.py,sha256=
|
|
13
|
-
dbos/dbos_config.py,sha256=
|
|
12
|
+
dbos/dbos.py,sha256=wzL51K7bY3J8grHXi0k0F0N09vFVBYWKwI0DGxyN4MY,29730
|
|
13
|
+
dbos/dbos_config.py,sha256=ih_TD_1zTKhPKxk8TPdEIp3ihu82R06SGKg-s4rHxws,5344
|
|
14
14
|
dbos/decorators.py,sha256=lbPefsLK6Cya4cb7TrOcLglOpGT3pc6qjZdsQKlfZLg,629
|
|
15
15
|
dbos/error.py,sha256=DDhB0VHmoZE_CP51ICdFMZSL2gmVS3Dm0aPNWncci94,3876
|
|
16
16
|
dbos/fastapi.py,sha256=s7LnwwYVpJm_QZZwBW5um8NV2Q2Qx85uVZqGcKlSZAo,1881
|
|
17
17
|
dbos/flask.py,sha256=azr4geMEGuuTBCyxIZmgDmmP-6s_pTIF-lGyp9Q4IB8,2430
|
|
18
|
+
dbos/kafka.py,sha256=FtngQHBu2TKfyDF7GFsKJAawFQJiOFxgKEUlNNxrdrw,3055
|
|
19
|
+
dbos/kafka_message.py,sha256=NYvOXNG3Qn7bghn1pv3fg4Pbs86ILZGcK4IB-MLUNu0,409
|
|
18
20
|
dbos/logger.py,sha256=D-aFSZUCHBP34J1IZ5YNkTrJW-rDiH3py_v9jLU4Yrk,3565
|
|
19
21
|
dbos/migrations/env.py,sha256=38SIGVbmn_VV2x2u1aHLcPOoWgZ84eCymf3g_NljmbU,1626
|
|
20
22
|
dbos/migrations/script.py.mako,sha256=MEqL-2qATlST9TAOeYgscMn1uy6HUS9NFvDgl93dMj8,635
|
|
@@ -44,4 +46,4 @@ dbos/templates/hello/start_postgres_docker.py,sha256=lQVLlYO5YkhGPEgPqwGc7Y8uDKs
|
|
|
44
46
|
dbos/tracer.py,sha256=GaXDhdKKF_IQp5SAMipGXiDVwteRKjNbrXyYCH1mor0,2520
|
|
45
47
|
dbos/utils.py,sha256=hWj9iWDrby2cVEhb0pG-IdnrxLqP64NhkaWUXiLc8bA,402
|
|
46
48
|
version/__init__.py,sha256=L4sNxecRuqdtSFdpUGX3TtBi9KL3k7YsZVIvv-fv9-A,1678
|
|
47
|
-
dbos-0.
|
|
49
|
+
dbos-0.7.0a0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|