dbos 0.5.0a12__tar.gz → 0.6.0__tar.gz
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-0.5.0a12 → dbos-0.6.0}/PKG-INFO +19 -17
- {dbos-0.5.0a12 → dbos-0.6.0}/README.md +18 -16
- dbos-0.6.0/dbos/__init__.py +22 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/context.py +18 -2
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/core.py +17 -1
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/dbos.py +91 -12
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/dbos_config.py +1 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/error.py +2 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/fastapi.py +1 -3
- dbos-0.6.0/dbos/flask.py +79 -0
- dbos-0.6.0/dbos/kafka.py +94 -0
- dbos-0.6.0/dbos/kafka_message.py +15 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/request.py +2 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/roles.py +1 -1
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/system_database.py +20 -13
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/__package/main.py +15 -22
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/__package/schema.py +2 -2
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/migrations/versions/2024_07_31_180642_init.py +2 -2
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/tracer.py +9 -1
- {dbos-0.5.0a12 → dbos-0.6.0}/pyproject.toml +7 -3
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/classdefs.py +1 -1
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/conftest.py +19 -1
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/scheduler/test_scheduler.py +3 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_admin_server.py +3 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_classdecorators.py +6 -6
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_dbos.py +9 -51
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_fastapi.py +3 -0
- dbos-0.6.0/tests/test_fastapi_roles.py +342 -0
- dbos-0.6.0/tests/test_flask.py +104 -0
- dbos-0.6.0/tests/test_kafka.py +80 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_singleton.py +12 -0
- dbos-0.5.0a12/dbos/__init__.py +0 -24
- {dbos-0.5.0a12 → dbos-0.6.0}/LICENSE +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/admin_sever.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/application_database.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/cli.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/dbos-config.schema.json +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/decorators.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/logger.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/migrations/env.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/migrations/script.py.mako +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/migrations/versions/5c361fc04708_added_system_tables.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/migrations/versions/a3b18ad34abe_added_triggers.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/py.typed +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/recovery.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/registrations.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/scheduler/croniter.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/scheduler/scheduler.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/schemas/__init__.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/schemas/application_database.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/schemas/system_database.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/README.md +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/__package/__init__.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/alembic.ini +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/dbos-config.yaml.dbos +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/migrations/env.py.dbos +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/migrations/script.py.mako +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/templates/hello/start_postgres_docker.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/dbos/utils.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/__init__.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/atexit_no_ctor.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/atexit_no_launch.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/more_classdefs.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/scheduler/test_croniter.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_concurrency.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_config.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_failures.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_package.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/tests/test_schema_migration.py +0 -0
- {dbos-0.5.0a12 → dbos-0.6.0}/version/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: dbos
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Ultra-lightweight durable execution in Python
|
|
5
5
|
Author-Email: "DBOS, Inc." <contact@dbos.dev>
|
|
6
6
|
License: MIT
|
|
@@ -21,20 +21,17 @@ Requires-Dist: psutil>=6.0.0
|
|
|
21
21
|
Requires-Dist: tomlkit>=0.13.2
|
|
22
22
|
Description-Content-Type: text/markdown
|
|
23
23
|
|
|
24
|
-
## 🚀 DBOS Transact - Ultra-Lightweight Durable Execution in Python 🚀
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
<div align="center">
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
# DBOS Transact: Ultra-Lightweight Durable Execution
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
#### [Documentation](https://docs.dbos.dev/) • [Examples](https://docs.dbos.dev/examples) • [Github](https://github.com/dbos-inc) • [Discord](https://discord.com/invite/jsmC6pXGgX)
|
|
30
|
+
</div>
|
|
31
31
|
|
|
32
32
|
---
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
**DBOS Python is under construction! 🚧🚧🚧 Check back regularly for updates, release coming in mid-September!**
|
|
36
|
-
|
|
37
|
-
DBOS Transact is a **Python library** providing ultra-lightweight durable execution.
|
|
34
|
+
DBOS Transact is a Python library providing **ultra-lightweight durable execution**.
|
|
38
35
|
For example:
|
|
39
36
|
|
|
40
37
|
```python
|
|
@@ -69,14 +66,14 @@ Some more cool features include:
|
|
|
69
66
|
|
|
70
67
|
## Getting Started
|
|
71
68
|
|
|
72
|
-
|
|
69
|
+
Install and configure with:
|
|
73
70
|
|
|
74
71
|
```shell
|
|
75
|
-
pip install
|
|
72
|
+
pip install dbos
|
|
76
73
|
dbos init --config
|
|
77
74
|
```
|
|
78
75
|
|
|
79
|
-
|
|
76
|
+
Then, try it out with this simple program (requires Postgres):
|
|
80
77
|
|
|
81
78
|
```python
|
|
82
79
|
from fastapi import FastAPI
|
|
@@ -106,22 +103,27 @@ def endpoint():
|
|
|
106
103
|
workflow()
|
|
107
104
|
```
|
|
108
105
|
|
|
109
|
-
Save the program into `main.py`,
|
|
110
|
-
Visit `localhost:8000` in your browser
|
|
106
|
+
Save the program into `main.py`, edit `dbos-config.yaml` to configure your Postgres connection settings, and start it with `fastapi run`.
|
|
107
|
+
Visit `localhost:8000` in your browser to start the workflow.
|
|
111
108
|
When prompted, press `Control + \` to force quit your application.
|
|
112
109
|
It should crash midway through the workflow, having completed step one but not step two.
|
|
113
110
|
Then, restart your app with `fastapi run`.
|
|
114
111
|
It should resume the workflow from where it left off, completing step two without re-executing step one.
|
|
115
112
|
|
|
116
|
-
To learn how to build more complex
|
|
113
|
+
To learn how to build more complex workflows, see our [programming guide](https://docs.dbos.dev/python/programming-guide) or [examples](https://docs.dbos.dev/examples).
|
|
117
114
|
|
|
118
115
|
## Documentation
|
|
119
116
|
|
|
120
|
-
|
|
117
|
+
[https://docs.dbos.dev](https://docs.dbos.dev)
|
|
121
118
|
|
|
122
119
|
## Examples
|
|
123
120
|
|
|
124
|
-
|
|
121
|
+
|
|
122
|
+
- [**AI-Powered Slackbot**](https://docs.dbos.dev/python/examples/rag-slackbot) — A Slackbot that answers questions about previous Slack conversations, using DBOS to durably orchestrate its RAG pipeline.
|
|
123
|
+
- [**Widget Store**](https://docs.dbos.dev/python/examples/widget-store) — An online storefront that uses DBOS durable workflows to be resilient to any failure.
|
|
124
|
+
- [**Earthquake Tracker**](https://docs.dbos.dev/python/examples/earthquake-tracker) — A real-time earthquake dashboard that uses DBOS to stream data from the USGS into Postgres, then visualizes it with Streamlit.
|
|
125
|
+
|
|
126
|
+
More examples [here](https://docs.dbos.dev/examples)!
|
|
125
127
|
|
|
126
128
|
## Community
|
|
127
129
|
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
## 🚀 DBOS Transact - Ultra-Lightweight Durable Execution in Python 🚀
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
<div align="center">
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
# DBOS Transact: Ultra-Lightweight Durable Execution
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
#### [Documentation](https://docs.dbos.dev/) • [Examples](https://docs.dbos.dev/examples) • [Github](https://github.com/dbos-inc) • [Discord](https://discord.com/invite/jsmC6pXGgX)
|
|
7
|
+
</div>
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
**DBOS Python is under construction! 🚧🚧🚧 Check back regularly for updates, release coming in mid-September!**
|
|
13
|
-
|
|
14
|
-
DBOS Transact is a **Python library** providing ultra-lightweight durable execution.
|
|
11
|
+
DBOS Transact is a Python library providing **ultra-lightweight durable execution**.
|
|
15
12
|
For example:
|
|
16
13
|
|
|
17
14
|
```python
|
|
@@ -46,14 +43,14 @@ Some more cool features include:
|
|
|
46
43
|
|
|
47
44
|
## Getting Started
|
|
48
45
|
|
|
49
|
-
|
|
46
|
+
Install and configure with:
|
|
50
47
|
|
|
51
48
|
```shell
|
|
52
|
-
pip install
|
|
49
|
+
pip install dbos
|
|
53
50
|
dbos init --config
|
|
54
51
|
```
|
|
55
52
|
|
|
56
|
-
|
|
53
|
+
Then, try it out with this simple program (requires Postgres):
|
|
57
54
|
|
|
58
55
|
```python
|
|
59
56
|
from fastapi import FastAPI
|
|
@@ -83,22 +80,27 @@ def endpoint():
|
|
|
83
80
|
workflow()
|
|
84
81
|
```
|
|
85
82
|
|
|
86
|
-
Save the program into `main.py`,
|
|
87
|
-
Visit `localhost:8000` in your browser
|
|
83
|
+
Save the program into `main.py`, edit `dbos-config.yaml` to configure your Postgres connection settings, and start it with `fastapi run`.
|
|
84
|
+
Visit `localhost:8000` in your browser to start the workflow.
|
|
88
85
|
When prompted, press `Control + \` to force quit your application.
|
|
89
86
|
It should crash midway through the workflow, having completed step one but not step two.
|
|
90
87
|
Then, restart your app with `fastapi run`.
|
|
91
88
|
It should resume the workflow from where it left off, completing step two without re-executing step one.
|
|
92
89
|
|
|
93
|
-
To learn how to build more complex
|
|
90
|
+
To learn how to build more complex workflows, see our [programming guide](https://docs.dbos.dev/python/programming-guide) or [examples](https://docs.dbos.dev/examples).
|
|
94
91
|
|
|
95
92
|
## Documentation
|
|
96
93
|
|
|
97
|
-
|
|
94
|
+
[https://docs.dbos.dev](https://docs.dbos.dev)
|
|
98
95
|
|
|
99
96
|
## Examples
|
|
100
97
|
|
|
101
|
-
|
|
98
|
+
|
|
99
|
+
- [**AI-Powered Slackbot**](https://docs.dbos.dev/python/examples/rag-slackbot) — A Slackbot that answers questions about previous Slack conversations, using DBOS to durably orchestrate its RAG pipeline.
|
|
100
|
+
- [**Widget Store**](https://docs.dbos.dev/python/examples/widget-store) — An online storefront that uses DBOS durable workflows to be resilient to any failure.
|
|
101
|
+
- [**Earthquake Tracker**](https://docs.dbos.dev/python/examples/earthquake-tracker) — A real-time earthquake dashboard that uses DBOS to stream data from the USGS into Postgres, then visualizes it with Streamlit.
|
|
102
|
+
|
|
103
|
+
More examples [here](https://docs.dbos.dev/examples)!
|
|
102
104
|
|
|
103
105
|
## Community
|
|
104
106
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from . import error as error
|
|
2
|
+
from .context import DBOSContextEnsure, SetWorkflowID
|
|
3
|
+
from .dbos import DBOS, DBOSConfiguredInstance, WorkflowHandle, WorkflowStatus
|
|
4
|
+
from .dbos_config import ConfigFile, get_dbos_database_url, load_config
|
|
5
|
+
from .kafka_message import KafkaMessage
|
|
6
|
+
from .system_database import GetWorkflowsInput, WorkflowStatusString
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"ConfigFile",
|
|
10
|
+
"DBOS",
|
|
11
|
+
"DBOSConfiguredInstance",
|
|
12
|
+
"DBOSContextEnsure",
|
|
13
|
+
"GetWorkflowsInput",
|
|
14
|
+
"KafkaMessage",
|
|
15
|
+
"SetWorkflowID",
|
|
16
|
+
"WorkflowHandle",
|
|
17
|
+
"WorkflowStatus",
|
|
18
|
+
"WorkflowStatusString",
|
|
19
|
+
"load_config",
|
|
20
|
+
"get_dbos_database_url",
|
|
21
|
+
"error",
|
|
22
|
+
]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import json
|
|
3
4
|
import os
|
|
4
5
|
import uuid
|
|
5
6
|
from contextvars import ContextVar
|
|
@@ -39,6 +40,9 @@ class TracedAttributes(TypedDict, total=False):
|
|
|
39
40
|
applicationID: Optional[str]
|
|
40
41
|
applicationVersion: Optional[str]
|
|
41
42
|
executorID: Optional[str]
|
|
43
|
+
authenticatedUser: Optional[str]
|
|
44
|
+
authenticatedUserRoles: Optional[str]
|
|
45
|
+
authenticatedUserAssumedRole: Optional[str]
|
|
42
46
|
|
|
43
47
|
|
|
44
48
|
class DBOSContext:
|
|
@@ -159,6 +163,13 @@ class DBOSContext:
|
|
|
159
163
|
attributes["operationUUID"] = (
|
|
160
164
|
self.workflow_id if len(self.workflow_id) > 0 else None
|
|
161
165
|
)
|
|
166
|
+
attributes["authenticatedUser"] = self.authenticated_user
|
|
167
|
+
attributes["authenticatedUserRoles"] = (
|
|
168
|
+
json.dumps(self.authenticated_roles)
|
|
169
|
+
if self.authenticated_roles is not None
|
|
170
|
+
else ""
|
|
171
|
+
)
|
|
172
|
+
attributes["authenticatedUserAssumedRole"] = self.assumed_role
|
|
162
173
|
span = dbos_tracer.start_span(
|
|
163
174
|
attributes, parent=self.spans[-1] if len(self.spans) > 0 else None
|
|
164
175
|
)
|
|
@@ -178,6 +189,11 @@ class DBOSContext:
|
|
|
178
189
|
) -> None:
|
|
179
190
|
self.authenticated_user = user
|
|
180
191
|
self.authenticated_roles = roles
|
|
192
|
+
if user is not None and len(self.spans) > 0:
|
|
193
|
+
self.spans[-1].set_attribute("authenticatedUser", user)
|
|
194
|
+
self.spans[-1].set_attribute(
|
|
195
|
+
"authenticatedUserRoles", json.dumps(roles) if roles is not None else ""
|
|
196
|
+
)
|
|
181
197
|
|
|
182
198
|
|
|
183
199
|
##############################################################
|
|
@@ -217,13 +233,13 @@ class DBOSContextEnsure:
|
|
|
217
233
|
def __init__(self) -> None:
|
|
218
234
|
self.created_ctx = False
|
|
219
235
|
|
|
220
|
-
def __enter__(self) ->
|
|
236
|
+
def __enter__(self) -> DBOSContext:
|
|
221
237
|
# Code to create a basic context
|
|
222
238
|
ctx = get_local_dbos_context()
|
|
223
239
|
if ctx is None:
|
|
224
240
|
self.created_ctx = True
|
|
225
241
|
set_local_dbos_context(DBOSContext())
|
|
226
|
-
return
|
|
242
|
+
return assert_current_dbos_context()
|
|
227
243
|
|
|
228
244
|
def __exit__(
|
|
229
245
|
self,
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
+
import json
|
|
1
2
|
import sys
|
|
2
3
|
import time
|
|
3
4
|
import traceback
|
|
4
5
|
from concurrent.futures import Future
|
|
5
6
|
from functools import wraps
|
|
6
|
-
from typing import
|
|
7
|
+
from typing import (
|
|
8
|
+
TYPE_CHECKING,
|
|
9
|
+
Any,
|
|
10
|
+
Callable,
|
|
11
|
+
Generic,
|
|
12
|
+
List,
|
|
13
|
+
Optional,
|
|
14
|
+
Tuple,
|
|
15
|
+
TypeVar,
|
|
16
|
+
cast,
|
|
17
|
+
)
|
|
7
18
|
|
|
8
19
|
from dbos.application_database import ApplicationDatabase, TransactionResultInternal
|
|
9
20
|
|
|
@@ -134,6 +145,11 @@ def _init_workflow(
|
|
|
134
145
|
"executor_id": ctx.executor_id,
|
|
135
146
|
"request": (utils.serialize(ctx.request) if ctx.request is not None else None),
|
|
136
147
|
"recovery_attempts": None,
|
|
148
|
+
"authenticated_user": ctx.authenticated_user,
|
|
149
|
+
"authenticated_roles": (
|
|
150
|
+
json.dumps(ctx.authenticated_roles) if ctx.authenticated_roles else None
|
|
151
|
+
),
|
|
152
|
+
"assumed_role": ctx.assumed_role,
|
|
137
153
|
}
|
|
138
154
|
|
|
139
155
|
# If we have a class name, the first arg is the instance and do not serialize
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import atexit
|
|
4
|
+
import json
|
|
4
5
|
import os
|
|
5
6
|
import sys
|
|
6
7
|
import threading
|
|
@@ -19,6 +20,7 @@ from typing import (
|
|
|
19
20
|
Tuple,
|
|
20
21
|
Type,
|
|
21
22
|
TypeVar,
|
|
23
|
+
cast,
|
|
22
24
|
)
|
|
23
25
|
|
|
24
26
|
from opentelemetry.trace import Span
|
|
@@ -52,10 +54,14 @@ from .tracer import dbos_tracer
|
|
|
52
54
|
|
|
53
55
|
if TYPE_CHECKING:
|
|
54
56
|
from fastapi import FastAPI
|
|
57
|
+
from dbos.kafka import KafkaConsumerWorkflow
|
|
55
58
|
from .request import Request
|
|
59
|
+
from flask import Flask
|
|
56
60
|
|
|
57
61
|
from sqlalchemy.orm import Session
|
|
58
62
|
|
|
63
|
+
from dbos.request import Request
|
|
64
|
+
|
|
59
65
|
if sys.version_info < (3, 10):
|
|
60
66
|
from typing_extensions import ParamSpec, TypeAlias
|
|
61
67
|
else:
|
|
@@ -205,6 +211,7 @@ class DBOS:
|
|
|
205
211
|
*,
|
|
206
212
|
config: Optional[ConfigFile] = None,
|
|
207
213
|
fastapi: Optional["FastAPI"] = None,
|
|
214
|
+
flask: Optional["Flask"] = None,
|
|
208
215
|
) -> DBOS:
|
|
209
216
|
global _dbos_global_instance
|
|
210
217
|
global _dbos_global_registry
|
|
@@ -219,7 +226,7 @@ class DBOS:
|
|
|
219
226
|
)
|
|
220
227
|
config = _dbos_global_registry.config
|
|
221
228
|
_dbos_global_instance = super().__new__(cls)
|
|
222
|
-
_dbos_global_instance.__init__(fastapi=fastapi, config=config) # type: ignore
|
|
229
|
+
_dbos_global_instance.__init__(fastapi=fastapi, config=config, flask=flask) # type: ignore
|
|
223
230
|
else:
|
|
224
231
|
if (config is not None and _dbos_global_instance.config is not config) or (
|
|
225
232
|
_dbos_global_instance.fastapi is not fastapi
|
|
@@ -243,6 +250,7 @@ class DBOS:
|
|
|
243
250
|
*,
|
|
244
251
|
config: Optional[ConfigFile] = None,
|
|
245
252
|
fastapi: Optional["FastAPI"] = None,
|
|
253
|
+
flask: Optional["Flask"] = None,
|
|
246
254
|
) -> None:
|
|
247
255
|
if hasattr(self, "_initialized") and self._initialized:
|
|
248
256
|
return
|
|
@@ -264,14 +272,44 @@ class DBOS:
|
|
|
264
272
|
self._admin_server: Optional[AdminServer] = None
|
|
265
273
|
self.stop_events: List[threading.Event] = []
|
|
266
274
|
self.fastapi: Optional["FastAPI"] = fastapi
|
|
275
|
+
self.flask: Optional["Flask"] = flask
|
|
267
276
|
self._executor: Optional[ThreadPoolExecutor] = None
|
|
277
|
+
|
|
278
|
+
# If using FastAPI, set up middleware and lifecycle events
|
|
268
279
|
if self.fastapi is not None:
|
|
280
|
+
from fastapi.requests import Request as FARequest
|
|
281
|
+
from fastapi.responses import JSONResponse
|
|
282
|
+
|
|
283
|
+
async def dbos_error_handler(
|
|
284
|
+
request: FARequest, gexc: Exception
|
|
285
|
+
) -> JSONResponse:
|
|
286
|
+
exc: DBOSException = cast(DBOSException, gexc)
|
|
287
|
+
status_code = 500
|
|
288
|
+
if exc.status_code is not None:
|
|
289
|
+
status_code = exc.status_code
|
|
290
|
+
return JSONResponse(
|
|
291
|
+
status_code=status_code,
|
|
292
|
+
content={
|
|
293
|
+
"message": str(exc.message),
|
|
294
|
+
"dbos_error_code": str(exc.dbos_error_code),
|
|
295
|
+
"dbos_error": str(exc.__class__.__name__),
|
|
296
|
+
},
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
self.fastapi.add_exception_handler(DBOSException, dbos_error_handler)
|
|
300
|
+
|
|
269
301
|
from dbos.fastapi import setup_fastapi_middleware
|
|
270
302
|
|
|
271
303
|
setup_fastapi_middleware(self.fastapi)
|
|
272
304
|
self.fastapi.on_event("startup")(self._launch)
|
|
273
305
|
self.fastapi.on_event("shutdown")(self._destroy)
|
|
274
306
|
|
|
307
|
+
# If using Flask, set up middleware
|
|
308
|
+
if self.flask is not None:
|
|
309
|
+
from dbos.flask import setup_flask_middleware
|
|
310
|
+
|
|
311
|
+
setup_flask_middleware(self.flask)
|
|
312
|
+
|
|
275
313
|
# Register send_stub as a workflow
|
|
276
314
|
def send_temp_workflow(
|
|
277
315
|
destination_id: str, message: Any, topic: Optional[str]
|
|
@@ -471,6 +509,20 @@ class DBOS:
|
|
|
471
509
|
|
|
472
510
|
return scheduled(_get_or_create_dbos_registry(), cron)
|
|
473
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
|
+
|
|
474
526
|
@classmethod
|
|
475
527
|
def start_workflow(
|
|
476
528
|
cls,
|
|
@@ -502,9 +554,13 @@ class DBOS:
|
|
|
502
554
|
recovery_attempts=stat["recovery_attempts"],
|
|
503
555
|
class_name=stat["class_name"],
|
|
504
556
|
config_name=stat["config_name"],
|
|
505
|
-
authenticated_user=
|
|
506
|
-
assumed_role=
|
|
507
|
-
|
|
557
|
+
authenticated_user=stat["authenticated_user"],
|
|
558
|
+
assumed_role=stat["assumed_role"],
|
|
559
|
+
authenticated_roles=(
|
|
560
|
+
json.loads(stat["authenticated_roles"])
|
|
561
|
+
if stat["authenticated_roles"] is not None
|
|
562
|
+
else None
|
|
563
|
+
),
|
|
508
564
|
)
|
|
509
565
|
|
|
510
566
|
@classmethod
|
|
@@ -663,6 +719,33 @@ class DBOS:
|
|
|
663
719
|
ctx = assert_current_dbos_context()
|
|
664
720
|
return ctx.request
|
|
665
721
|
|
|
722
|
+
@classproperty
|
|
723
|
+
def authenticated_user(cls) -> Optional[str]:
|
|
724
|
+
"""Return the current authenticated user, if any, associated with the current context."""
|
|
725
|
+
ctx = assert_current_dbos_context()
|
|
726
|
+
return ctx.authenticated_user
|
|
727
|
+
|
|
728
|
+
@classproperty
|
|
729
|
+
def authenticated_roles(cls) -> Optional[List[str]]:
|
|
730
|
+
"""Return the roles granted to the current authenticated user, if any, associated with the current context."""
|
|
731
|
+
ctx = assert_current_dbos_context()
|
|
732
|
+
return ctx.authenticated_roles
|
|
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
|
+
|
|
740
|
+
@classmethod
|
|
741
|
+
def set_authentication(
|
|
742
|
+
cls, authenticated_user: Optional[str], authenticated_roles: Optional[List[str]]
|
|
743
|
+
) -> None:
|
|
744
|
+
"""Set the current authenticated user and granted roles into the current context."""
|
|
745
|
+
ctx = assert_current_dbos_context()
|
|
746
|
+
ctx.authenticated_user = authenticated_user
|
|
747
|
+
ctx.authenticated_roles = authenticated_roles
|
|
748
|
+
|
|
666
749
|
|
|
667
750
|
@dataclass
|
|
668
751
|
class WorkflowStatus:
|
|
@@ -679,7 +762,7 @@ class WorkflowStatus:
|
|
|
679
762
|
config_name(str): For instance member functions, the name of the class instance for the execution
|
|
680
763
|
authenticated_user(str): The user who invoked the workflow
|
|
681
764
|
assumed_role(str): The access role used by the user to allow access to the workflow function
|
|
682
|
-
|
|
765
|
+
authenticated_roles(List[str]): List of all access roles available to the authenticated user
|
|
683
766
|
recovery_attempts(int): Number of times the workflow has been restarted (usually by recovery)
|
|
684
767
|
|
|
685
768
|
"""
|
|
@@ -691,7 +774,7 @@ class WorkflowStatus:
|
|
|
691
774
|
config_name: Optional[str]
|
|
692
775
|
authenticated_user: Optional[str]
|
|
693
776
|
assumed_role: Optional[str]
|
|
694
|
-
|
|
777
|
+
authenticated_roles: Optional[List[str]]
|
|
695
778
|
recovery_attempts: Optional[int]
|
|
696
779
|
|
|
697
780
|
|
|
@@ -740,13 +823,9 @@ class DBOSConfiguredInstance:
|
|
|
740
823
|
|
|
741
824
|
"""
|
|
742
825
|
|
|
743
|
-
def __init__(self, config_name: str
|
|
826
|
+
def __init__(self, config_name: str) -> None:
|
|
744
827
|
self.config_name = config_name
|
|
745
|
-
|
|
746
|
-
assert isinstance(dbos, DBOS)
|
|
747
|
-
dbos._registry.register_instance(self)
|
|
748
|
-
else:
|
|
749
|
-
DBOS.register_instance(self)
|
|
828
|
+
DBOS.register_instance(self)
|
|
750
829
|
|
|
751
830
|
|
|
752
831
|
# Apps that import DBOS probably don't exit. If they do, let's see if
|
|
@@ -17,6 +17,7 @@ class DBOSException(Exception):
|
|
|
17
17
|
def __init__(self, message: str, dbos_error_code: Optional[int] = None):
|
|
18
18
|
self.message = message
|
|
19
19
|
self.dbos_error_code = dbos_error_code
|
|
20
|
+
self.status_code: Optional[int] = None
|
|
20
21
|
super().__init__(self.message)
|
|
21
22
|
|
|
22
23
|
def __str__(self) -> str:
|
|
@@ -104,6 +105,7 @@ class DBOSNotAuthorizedError(DBOSException):
|
|
|
104
105
|
msg,
|
|
105
106
|
dbos_error_code=DBOSErrorCode.NotAuthorized.value,
|
|
106
107
|
)
|
|
108
|
+
self.status_code = 403
|
|
107
109
|
|
|
108
110
|
|
|
109
111
|
class DBOSMaxStepRetriesExceeded(DBOSException):
|
|
@@ -11,9 +11,7 @@ from .context import (
|
|
|
11
11
|
TracedAttributes,
|
|
12
12
|
assert_current_dbos_context,
|
|
13
13
|
)
|
|
14
|
-
from .request import Address, Request
|
|
15
|
-
|
|
16
|
-
request_id_header = "x-request-id"
|
|
14
|
+
from .request import Address, Request, request_id_header
|
|
17
15
|
|
|
18
16
|
|
|
19
17
|
def get_or_generate_request_id(request: FastAPIRequest) -> str:
|
dbos-0.6.0/dbos/flask.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from typing import Any
|
|
3
|
+
from urllib.parse import urlparse
|
|
4
|
+
|
|
5
|
+
from flask import Flask, request
|
|
6
|
+
from werkzeug.wrappers import Request as WRequest
|
|
7
|
+
|
|
8
|
+
from dbos.context import (
|
|
9
|
+
EnterDBOSHandler,
|
|
10
|
+
OperationType,
|
|
11
|
+
SetWorkflowID,
|
|
12
|
+
TracedAttributes,
|
|
13
|
+
assert_current_dbos_context,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from .request import Address, Request, request_id_header
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class FlaskMiddleware:
|
|
20
|
+
def __init__(self, app: Any) -> None:
|
|
21
|
+
self.app = app
|
|
22
|
+
|
|
23
|
+
def __call__(self, environ: Any, start_response: Any) -> Any:
|
|
24
|
+
request = WRequest(environ)
|
|
25
|
+
attributes: TracedAttributes = {
|
|
26
|
+
"name": urlparse(request.url).path,
|
|
27
|
+
"requestID": get_or_generate_request_id(request),
|
|
28
|
+
"requestIP": (
|
|
29
|
+
request.remote_addr if request.remote_addr is not None else None
|
|
30
|
+
),
|
|
31
|
+
"requestURL": request.url,
|
|
32
|
+
"requestMethod": request.method,
|
|
33
|
+
"operationType": OperationType.HANDLER.value,
|
|
34
|
+
}
|
|
35
|
+
with EnterDBOSHandler(attributes):
|
|
36
|
+
ctx = assert_current_dbos_context()
|
|
37
|
+
ctx.request = make_request(request)
|
|
38
|
+
workflow_id = request.headers.get("dbos-idempotency-key", "")
|
|
39
|
+
with SetWorkflowID(workflow_id):
|
|
40
|
+
response = self.app(environ, start_response)
|
|
41
|
+
return response
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def get_or_generate_request_id(request: WRequest) -> str:
|
|
45
|
+
request_id = request.headers.get(request_id_header, None)
|
|
46
|
+
if request_id is not None:
|
|
47
|
+
return request_id
|
|
48
|
+
else:
|
|
49
|
+
return str(uuid.uuid4())
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def make_request(request: WRequest) -> Request:
|
|
53
|
+
parsed_url = urlparse(request.url)
|
|
54
|
+
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
|
|
55
|
+
|
|
56
|
+
client = None
|
|
57
|
+
if request.remote_addr:
|
|
58
|
+
hostname = request.remote_addr
|
|
59
|
+
port = request.environ.get("REMOTE_PORT")
|
|
60
|
+
if port:
|
|
61
|
+
client = Address(hostname=hostname, port=int(port))
|
|
62
|
+
else:
|
|
63
|
+
# If port is not available, use 0 as a placeholder
|
|
64
|
+
client = Address(hostname=hostname, port=0)
|
|
65
|
+
|
|
66
|
+
return Request(
|
|
67
|
+
headers=dict(request.headers),
|
|
68
|
+
path_params={},
|
|
69
|
+
query_params=dict(request.args),
|
|
70
|
+
url=request.url,
|
|
71
|
+
base_url=base_url,
|
|
72
|
+
client=client,
|
|
73
|
+
cookies=dict(request.cookies),
|
|
74
|
+
method=request.method,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def setup_flask_middleware(app: Flask) -> None:
|
|
79
|
+
app.wsgi_app = FlaskMiddleware(app.wsgi_app) # type: ignore
|