dbos 0.18.0a1__py3-none-any.whl → 0.19.0__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/_context.py +11 -2
- dbos/_core.py +30 -8
- dbos/_db_wizard.py +53 -12
- dbos/_dbos.py +1 -1
- dbos/_dbos_config.py +52 -7
- dbos/_error.py +11 -0
- dbos/_kafka.py +17 -1
- dbos/_migrations/versions/04ca4f231047_workflow_queues_executor_id.py +34 -0
- dbos/_queue.py +20 -0
- dbos/_schemas/system_database.py +1 -0
- dbos/_sys_db.py +100 -47
- dbos/_templates/hello/dbos-config.yaml.dbos +0 -4
- dbos/_workflow_commands.py +172 -0
- dbos/cli.py +100 -1
- dbos/dbos-config.schema.json +2 -11
- {dbos-0.18.0a1.dist-info → dbos-0.19.0.dist-info}/METADATA +21 -16
- {dbos-0.18.0a1.dist-info → dbos-0.19.0.dist-info}/RECORD +20 -18
- {dbos-0.18.0a1.dist-info → dbos-0.19.0.dist-info}/WHEEL +0 -0
- {dbos-0.18.0a1.dist-info → dbos-0.19.0.dist-info}/entry_points.txt +0 -0
- {dbos-0.18.0a1.dist-info → dbos-0.19.0.dist-info}/licenses/LICENSE +0 -0
dbos/_context.py
CHANGED
|
@@ -57,6 +57,7 @@ class DBOSContext:
|
|
|
57
57
|
self.request: Optional["Request"] = None
|
|
58
58
|
|
|
59
59
|
self.id_assigned_for_next_workflow: str = ""
|
|
60
|
+
self.is_within_set_workflow_id_block: bool = False
|
|
60
61
|
|
|
61
62
|
self.parent_workflow_id: str = ""
|
|
62
63
|
self.parent_workflow_fid: int = -1
|
|
@@ -78,6 +79,7 @@ class DBOSContext:
|
|
|
78
79
|
rv.logger = self.logger
|
|
79
80
|
rv.id_assigned_for_next_workflow = self.id_assigned_for_next_workflow
|
|
80
81
|
self.id_assigned_for_next_workflow = ""
|
|
82
|
+
rv.is_within_set_workflow_id_block = self.is_within_set_workflow_id_block
|
|
81
83
|
rv.parent_workflow_id = self.workflow_id
|
|
82
84
|
rv.parent_workflow_fid = self.function_id
|
|
83
85
|
rv.in_recovery = self.in_recovery
|
|
@@ -95,6 +97,10 @@ class DBOSContext:
|
|
|
95
97
|
if len(self.id_assigned_for_next_workflow) > 0:
|
|
96
98
|
wfid = self.id_assigned_for_next_workflow
|
|
97
99
|
else:
|
|
100
|
+
if self.is_within_set_workflow_id_block:
|
|
101
|
+
self.logger.warning(
|
|
102
|
+
f"Multiple workflows started in the same SetWorkflowID block. Only the first workflow is assigned the specified workflow ID; subsequent workflows will use a generated workflow ID."
|
|
103
|
+
)
|
|
98
104
|
wfid = str(uuid.uuid4())
|
|
99
105
|
return wfid
|
|
100
106
|
|
|
@@ -286,7 +292,7 @@ class DBOSContextSwap:
|
|
|
286
292
|
|
|
287
293
|
class SetWorkflowID:
|
|
288
294
|
"""
|
|
289
|
-
Set the workflow ID to be used for the enclosed workflow invocation.
|
|
295
|
+
Set the workflow ID to be used for the enclosed workflow invocation. Note: Only the first workflow will be started with the specified workflow ID within a `with SetWorkflowID` block.
|
|
290
296
|
|
|
291
297
|
Typical Usage
|
|
292
298
|
```
|
|
@@ -311,7 +317,9 @@ class SetWorkflowID:
|
|
|
311
317
|
if ctx is None:
|
|
312
318
|
self.created_ctx = True
|
|
313
319
|
_set_local_dbos_context(DBOSContext())
|
|
314
|
-
assert_current_dbos_context()
|
|
320
|
+
ctx = assert_current_dbos_context()
|
|
321
|
+
ctx.id_assigned_for_next_workflow = self.wfid
|
|
322
|
+
ctx.is_within_set_workflow_id_block = True
|
|
315
323
|
return self
|
|
316
324
|
|
|
317
325
|
def __exit__(
|
|
@@ -321,6 +329,7 @@ class SetWorkflowID:
|
|
|
321
329
|
traceback: Optional[TracebackType],
|
|
322
330
|
) -> Literal[False]:
|
|
323
331
|
# Code to clean up the basic context if we created it
|
|
332
|
+
assert_current_dbos_context().is_within_set_workflow_id_block = False
|
|
324
333
|
if self.created_ctx:
|
|
325
334
|
_clear_local_dbos_context()
|
|
326
335
|
return False # Did not handle
|
dbos/_core.py
CHANGED
|
@@ -84,7 +84,7 @@ if TYPE_CHECKING:
|
|
|
84
84
|
IsolationLevel,
|
|
85
85
|
)
|
|
86
86
|
|
|
87
|
-
from sqlalchemy.exc import DBAPIError
|
|
87
|
+
from sqlalchemy.exc import DBAPIError, InvalidRequestError
|
|
88
88
|
|
|
89
89
|
P = ParamSpec("P") # A generic type for workflow parameters
|
|
90
90
|
R = TypeVar("R", covariant=True) # A generic type for workflow return values
|
|
@@ -180,21 +180,24 @@ def _init_workflow(
|
|
|
180
180
|
if class_name is not None:
|
|
181
181
|
inputs = {"args": inputs["args"][1:], "kwargs": inputs["kwargs"]}
|
|
182
182
|
|
|
183
|
+
wf_status = status["status"]
|
|
183
184
|
if temp_wf_type != "transaction" or queue is not None:
|
|
184
185
|
# Synchronously record the status and inputs for workflows and single-step workflows
|
|
185
186
|
# We also have to do this for single-step workflows because of the foreign key constraint on the operation outputs table
|
|
186
187
|
# TODO: Make this transactional (and with the queue step below)
|
|
187
|
-
dbos._sys_db.update_workflow_status(
|
|
188
|
+
wf_status = dbos._sys_db.update_workflow_status(
|
|
188
189
|
status, False, ctx.in_recovery, max_recovery_attempts=max_recovery_attempts
|
|
189
190
|
)
|
|
191
|
+
# TODO: Modify the inputs if they were changed by `update_workflow_inputs`
|
|
190
192
|
dbos._sys_db.update_workflow_inputs(wfid, _serialization.serialize_args(inputs))
|
|
191
193
|
else:
|
|
192
194
|
# Buffer the inputs for single-transaction workflows, but don't buffer the status
|
|
193
195
|
dbos._sys_db.buffer_workflow_inputs(wfid, _serialization.serialize_args(inputs))
|
|
194
196
|
|
|
195
|
-
if queue is not None:
|
|
197
|
+
if queue is not None and wf_status == WorkflowStatusString.ENQUEUED.value:
|
|
196
198
|
dbos._sys_db.enqueue(wfid, queue)
|
|
197
199
|
|
|
200
|
+
status["status"] = wf_status
|
|
198
201
|
return status
|
|
199
202
|
|
|
200
203
|
|
|
@@ -413,7 +416,16 @@ def start_workflow(
|
|
|
413
416
|
max_recovery_attempts=fi.max_recovery_attempts,
|
|
414
417
|
)
|
|
415
418
|
|
|
416
|
-
|
|
419
|
+
wf_status = status["status"]
|
|
420
|
+
|
|
421
|
+
if (
|
|
422
|
+
not execute_workflow
|
|
423
|
+
or wf_status == WorkflowStatusString.ERROR.value
|
|
424
|
+
or wf_status == WorkflowStatusString.SUCCESS.value
|
|
425
|
+
):
|
|
426
|
+
dbos.logger.debug(
|
|
427
|
+
f"Workflow {new_wf_id} already completed with status {wf_status}. Directly returning a workflow handle."
|
|
428
|
+
)
|
|
417
429
|
return WorkflowHandlePolling(new_wf_id, dbos)
|
|
418
430
|
|
|
419
431
|
if fself is not None:
|
|
@@ -486,7 +498,7 @@ def workflow_wrapper(
|
|
|
486
498
|
temp_wf_type=get_temp_workflow_type(func),
|
|
487
499
|
max_recovery_attempts=max_recovery_attempts,
|
|
488
500
|
)
|
|
489
|
-
|
|
501
|
+
# TODO: maybe modify the parameters if they've been changed by `_init_workflow`
|
|
490
502
|
dbos.logger.debug(
|
|
491
503
|
f"Running workflow, id: {ctx.workflow_id}, name: {get_dbos_func_name(func)}"
|
|
492
504
|
)
|
|
@@ -545,6 +557,7 @@ def decorate_transaction(
|
|
|
545
557
|
max_retry_wait_seconds = 2.0
|
|
546
558
|
while True:
|
|
547
559
|
has_recorded_error = False
|
|
560
|
+
txn_error: Optional[Exception] = None
|
|
548
561
|
try:
|
|
549
562
|
with session.begin():
|
|
550
563
|
# This must be the first statement in the transaction!
|
|
@@ -608,15 +621,24 @@ def decorate_transaction(
|
|
|
608
621
|
max_retry_wait_seconds,
|
|
609
622
|
)
|
|
610
623
|
continue
|
|
624
|
+
txn_error = dbapi_error
|
|
625
|
+
raise
|
|
626
|
+
except InvalidRequestError as invalid_request_error:
|
|
627
|
+
dbos.logger.error(
|
|
628
|
+
f"InvalidRequestError in transaction {func.__qualname__} \033[1m Hint: Do not call commit() or rollback() within a DBOS transaction.\033[0m"
|
|
629
|
+
)
|
|
630
|
+
txn_error = invalid_request_error
|
|
611
631
|
raise
|
|
612
632
|
except Exception as error:
|
|
633
|
+
txn_error = error
|
|
634
|
+
raise
|
|
635
|
+
finally:
|
|
613
636
|
# Don't record the error if it was already recorded
|
|
614
|
-
if not has_recorded_error:
|
|
637
|
+
if txn_error and not has_recorded_error:
|
|
615
638
|
txn_output["error"] = (
|
|
616
|
-
_serialization.serialize_exception(
|
|
639
|
+
_serialization.serialize_exception(txn_error)
|
|
617
640
|
)
|
|
618
641
|
dbos._app_db.record_transaction_error(txn_output)
|
|
619
|
-
raise
|
|
620
642
|
return output
|
|
621
643
|
|
|
622
644
|
if inspect.iscoroutinefunction(func):
|
dbos/_db_wizard.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
1
3
|
import time
|
|
2
|
-
from typing import TYPE_CHECKING, Optional
|
|
4
|
+
from typing import TYPE_CHECKING, Optional, TypedDict
|
|
3
5
|
|
|
4
6
|
import docker # type: ignore
|
|
5
7
|
import typer
|
|
@@ -15,16 +17,29 @@ from ._cloudutils.databases import choose_database, get_user_db_credentials
|
|
|
15
17
|
from ._error import DBOSInitializationError
|
|
16
18
|
from ._logger import dbos_logger
|
|
17
19
|
|
|
20
|
+
DB_CONNECTION_PATH = os.path.join(".dbos", "db_connection")
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
|
|
23
|
+
class DatabaseConnection(TypedDict):
|
|
24
|
+
hostname: Optional[str]
|
|
25
|
+
port: Optional[int]
|
|
26
|
+
username: Optional[str]
|
|
27
|
+
password: Optional[str]
|
|
28
|
+
local_suffix: Optional[bool]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def db_wizard(config: "ConfigFile", config_file_path: str) -> "ConfigFile":
|
|
20
32
|
# 1. Check the connectivity to the database. Return if successful. If cannot connect, continue to the following steps.
|
|
21
33
|
db_connection_error = _check_db_connectivity(config)
|
|
22
34
|
if db_connection_error is None:
|
|
23
35
|
return config
|
|
24
36
|
|
|
25
37
|
# 2. If the error is due to password authentication or the configuration is non-default, surface the error and exit.
|
|
26
|
-
|
|
27
|
-
|
|
38
|
+
error_str = str(db_connection_error)
|
|
39
|
+
if (
|
|
40
|
+
"password authentication failed" in error_str
|
|
41
|
+
or "28P01" in error_str
|
|
42
|
+
or "no password supplied" in error_str
|
|
28
43
|
):
|
|
29
44
|
raise DBOSInitializationError(
|
|
30
45
|
f"Could not connect to Postgres: password authentication failed: {db_connection_error}"
|
|
@@ -79,17 +94,20 @@ def db_connect(config: "ConfigFile", config_file_path: str) -> "ConfigFile":
|
|
|
79
94
|
f"Could not connect to the database. Exception: {db_connection_error}"
|
|
80
95
|
)
|
|
81
96
|
|
|
82
|
-
# 6. Save the config to the
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
# 6. Save the config to the database connection file
|
|
98
|
+
updated_connection = DatabaseConnection(
|
|
99
|
+
hostname=config["database"]["hostname"],
|
|
100
|
+
port=config["database"]["port"],
|
|
101
|
+
username=config["database"]["username"],
|
|
102
|
+
password=config["database"]["password"],
|
|
103
|
+
local_suffix=config["database"]["local_suffix"],
|
|
104
|
+
)
|
|
105
|
+
save_db_connection(updated_connection)
|
|
87
106
|
return config
|
|
88
107
|
|
|
89
108
|
|
|
90
109
|
def _start_docker_postgres(config: "ConfigFile") -> bool:
|
|
91
110
|
print("Starting a Postgres Docker container...")
|
|
92
|
-
config["database"]["password"] = "dbos"
|
|
93
111
|
client = docker.from_env()
|
|
94
112
|
pg_data = "/var/lib/postgresql/data"
|
|
95
113
|
container_name = "dbos-db"
|
|
@@ -119,7 +137,7 @@ def _start_docker_postgres(config: "ConfigFile") -> bool:
|
|
|
119
137
|
continue
|
|
120
138
|
print("[green]Postgres Docker container started successfully![/green]")
|
|
121
139
|
break
|
|
122
|
-
except
|
|
140
|
+
except:
|
|
123
141
|
attempts -= 1
|
|
124
142
|
time.sleep(1)
|
|
125
143
|
|
|
@@ -148,7 +166,7 @@ def _check_db_connectivity(config: "ConfigFile") -> Optional[Exception]:
|
|
|
148
166
|
host=config["database"]["hostname"],
|
|
149
167
|
port=config["database"]["port"],
|
|
150
168
|
database="postgres",
|
|
151
|
-
query={"connect_timeout": "
|
|
169
|
+
query={"connect_timeout": "1"},
|
|
152
170
|
)
|
|
153
171
|
postgres_db_engine = create_engine(postgres_db_url)
|
|
154
172
|
try:
|
|
@@ -165,3 +183,26 @@ def _check_db_connectivity(config: "ConfigFile") -> Optional[Exception]:
|
|
|
165
183
|
postgres_db_engine.dispose()
|
|
166
184
|
|
|
167
185
|
return None
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def load_db_connection() -> DatabaseConnection:
|
|
189
|
+
try:
|
|
190
|
+
with open(DB_CONNECTION_PATH, "r") as f:
|
|
191
|
+
data = json.load(f)
|
|
192
|
+
return DatabaseConnection(
|
|
193
|
+
hostname=data.get("hostname", None),
|
|
194
|
+
port=data.get("port", None),
|
|
195
|
+
username=data.get("username", None),
|
|
196
|
+
password=data.get("password", None),
|
|
197
|
+
local_suffix=data.get("local_suffix", None),
|
|
198
|
+
)
|
|
199
|
+
except:
|
|
200
|
+
return DatabaseConnection(
|
|
201
|
+
hostname=None, port=None, username=None, password=None, local_suffix=None
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def save_db_connection(connection: DatabaseConnection) -> None:
|
|
206
|
+
os.makedirs(".dbos", exist_ok=True)
|
|
207
|
+
with open(DB_CONNECTION_PATH, "w") as f:
|
|
208
|
+
json.dump(connection, f)
|
dbos/_dbos.py
CHANGED
|
@@ -83,7 +83,7 @@ from ._context import (
|
|
|
83
83
|
)
|
|
84
84
|
from ._dbos_config import ConfigFile, load_config, set_env_vars
|
|
85
85
|
from ._error import DBOSException, DBOSNonExistentWorkflowError
|
|
86
|
-
from ._logger import add_otlp_to_all_loggers, dbos_logger
|
|
86
|
+
from ._logger import add_otlp_to_all_loggers, dbos_logger
|
|
87
87
|
from ._sys_db import SystemDatabase
|
|
88
88
|
|
|
89
89
|
# Most DBOS functions are just any callable F, so decorators / wrappers work on F
|
dbos/_dbos_config.py
CHANGED
|
@@ -6,12 +6,15 @@ from typing import Any, Dict, List, Optional, TypedDict, cast
|
|
|
6
6
|
|
|
7
7
|
import yaml
|
|
8
8
|
from jsonschema import ValidationError, validate
|
|
9
|
+
from rich import print
|
|
9
10
|
from sqlalchemy import URL
|
|
10
11
|
|
|
11
|
-
from ._db_wizard import
|
|
12
|
+
from ._db_wizard import db_wizard, load_db_connection
|
|
12
13
|
from ._error import DBOSInitializationError
|
|
13
14
|
from ._logger import config_logger, dbos_logger, init_logger
|
|
14
15
|
|
|
16
|
+
DBOS_CONFIG_PATH = "dbos-config.yaml"
|
|
17
|
+
|
|
15
18
|
|
|
16
19
|
class RuntimeConfig(TypedDict, total=False):
|
|
17
20
|
start: List[str]
|
|
@@ -23,7 +26,7 @@ class DatabaseConfig(TypedDict, total=False):
|
|
|
23
26
|
hostname: str
|
|
24
27
|
port: int
|
|
25
28
|
username: str
|
|
26
|
-
password:
|
|
29
|
+
password: str
|
|
27
30
|
connectionTimeoutMillis: Optional[int]
|
|
28
31
|
app_db_name: str
|
|
29
32
|
sys_db_name: Optional[str]
|
|
@@ -93,7 +96,7 @@ def _substitute_env_vars(content: str) -> str:
|
|
|
93
96
|
return re.sub(regex, replace_func, content)
|
|
94
97
|
|
|
95
98
|
|
|
96
|
-
def get_dbos_database_url(config_file_path: str =
|
|
99
|
+
def get_dbos_database_url(config_file_path: str = DBOS_CONFIG_PATH) -> str:
|
|
97
100
|
"""
|
|
98
101
|
Retrieve application database URL from configuration `.yaml` file.
|
|
99
102
|
|
|
@@ -119,7 +122,9 @@ def get_dbos_database_url(config_file_path: str = "dbos-config.yaml") -> str:
|
|
|
119
122
|
return db_url.render_as_string(hide_password=False)
|
|
120
123
|
|
|
121
124
|
|
|
122
|
-
def load_config(
|
|
125
|
+
def load_config(
|
|
126
|
+
config_file_path: str = DBOS_CONFIG_PATH, *, use_db_wizard: bool = True
|
|
127
|
+
) -> ConfigFile:
|
|
123
128
|
"""
|
|
124
129
|
Load the DBOS `ConfigFile` from the specified path (typically `dbos-config.yaml`).
|
|
125
130
|
|
|
@@ -151,6 +156,9 @@ def load_config(config_file_path: str = "dbos-config.yaml") -> ConfigFile:
|
|
|
151
156
|
except ValidationError as e:
|
|
152
157
|
raise DBOSInitializationError(f"Validation error: {e}")
|
|
153
158
|
|
|
159
|
+
if "database" not in data:
|
|
160
|
+
data["database"] = {}
|
|
161
|
+
|
|
154
162
|
if "name" not in data:
|
|
155
163
|
raise DBOSInitializationError(
|
|
156
164
|
f"dbos-config.yaml must specify an application name"
|
|
@@ -169,8 +177,6 @@ def load_config(config_file_path: str = "dbos-config.yaml") -> ConfigFile:
|
|
|
169
177
|
if "runtimeConfig" not in data or "start" not in data["runtimeConfig"]:
|
|
170
178
|
raise DBOSInitializationError(f"dbos-config.yaml must specify a start command")
|
|
171
179
|
|
|
172
|
-
data = cast(ConfigFile, data)
|
|
173
|
-
|
|
174
180
|
if not _is_valid_app_name(data["name"]):
|
|
175
181
|
raise DBOSInitializationError(
|
|
176
182
|
f'Invalid app name {data["name"]}. App names must be between 3 and 30 characters long and contain only lowercase letters, numbers, dashes, and underscores.'
|
|
@@ -179,10 +185,49 @@ def load_config(config_file_path: str = "dbos-config.yaml") -> ConfigFile:
|
|
|
179
185
|
if "app_db_name" not in data["database"]:
|
|
180
186
|
data["database"]["app_db_name"] = _app_name_to_db_name(data["name"])
|
|
181
187
|
|
|
188
|
+
# Load the DB connection file. Use its values for missing fields from dbos-config.yaml. Use defaults otherwise.
|
|
189
|
+
data = cast(ConfigFile, data)
|
|
190
|
+
db_connection = load_db_connection()
|
|
191
|
+
if data["database"].get("hostname"):
|
|
192
|
+
print(
|
|
193
|
+
"[bold blue]Loading database connection parameters from dbos-config.yaml[/bold blue]"
|
|
194
|
+
)
|
|
195
|
+
elif db_connection.get("hostname"):
|
|
196
|
+
print(
|
|
197
|
+
"[bold blue]Loading database connection parameters from .dbos/db_connection[/bold blue]"
|
|
198
|
+
)
|
|
199
|
+
else:
|
|
200
|
+
print(
|
|
201
|
+
"[bold blue]Using default database connection parameters (localhost)[/bold blue]"
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
data["database"]["hostname"] = (
|
|
205
|
+
data["database"].get("hostname") or db_connection.get("hostname") or "localhost"
|
|
206
|
+
)
|
|
207
|
+
data["database"]["port"] = (
|
|
208
|
+
data["database"].get("port") or db_connection.get("port") or 5432
|
|
209
|
+
)
|
|
210
|
+
data["database"]["username"] = (
|
|
211
|
+
data["database"].get("username") or db_connection.get("username") or "postgres"
|
|
212
|
+
)
|
|
213
|
+
data["database"]["password"] = (
|
|
214
|
+
data["database"].get("password")
|
|
215
|
+
or db_connection.get("password")
|
|
216
|
+
or os.environ.get("PGPASSWORD")
|
|
217
|
+
or "dbos"
|
|
218
|
+
)
|
|
219
|
+
data["database"]["local_suffix"] = (
|
|
220
|
+
data["database"].get("local_suffix")
|
|
221
|
+
or db_connection.get("local_suffix")
|
|
222
|
+
or False
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# Configure the DBOS logger
|
|
182
226
|
config_logger(data)
|
|
183
227
|
|
|
184
228
|
# Check the connectivity to the database and make sure it's properly configured
|
|
185
|
-
|
|
229
|
+
if use_db_wizard:
|
|
230
|
+
data = db_wizard(data, config_file_path)
|
|
186
231
|
|
|
187
232
|
if "local_suffix" in data["database"] and data["database"]["local_suffix"]:
|
|
188
233
|
data["database"]["app_db_name"] = f"{data['database']['app_db_name']}_local"
|
dbos/_error.py
CHANGED
|
@@ -35,6 +35,7 @@ class DBOSErrorCode(Enum):
|
|
|
35
35
|
DeadLetterQueueError = 6
|
|
36
36
|
MaxStepRetriesExceeded = 7
|
|
37
37
|
NotAuthorized = 8
|
|
38
|
+
ConflictingWorkflowError = 9
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
class DBOSWorkflowConflictIDError(DBOSException):
|
|
@@ -47,6 +48,16 @@ class DBOSWorkflowConflictIDError(DBOSException):
|
|
|
47
48
|
)
|
|
48
49
|
|
|
49
50
|
|
|
51
|
+
class DBOSConflictingWorkflowError(DBOSException):
|
|
52
|
+
"""Exception raised different workflows started with the same workflow ID."""
|
|
53
|
+
|
|
54
|
+
def __init__(self, workflow_id: str, message: Optional[str] = None):
|
|
55
|
+
super().__init__(
|
|
56
|
+
f"Conflicting workflow invocation with the same ID ({workflow_id}): {message}",
|
|
57
|
+
dbos_error_code=DBOSErrorCode.ConflictingWorkflowError.value,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
50
61
|
class DBOSRecoveryError(DBOSException):
|
|
51
62
|
"""Exception raised when a workflow recovery fails."""
|
|
52
63
|
|
dbos/_kafka.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import re
|
|
1
2
|
import threading
|
|
2
3
|
from typing import TYPE_CHECKING, Any, Callable, NoReturn
|
|
3
4
|
|
|
@@ -19,6 +20,14 @@ _kafka_queue: Queue
|
|
|
19
20
|
_in_order_kafka_queues: dict[str, Queue] = {}
|
|
20
21
|
|
|
21
22
|
|
|
23
|
+
def safe_group_name(method_name: str, topics: list[str]) -> str:
|
|
24
|
+
safe_group_id = "-".join(
|
|
25
|
+
re.sub(r"[^a-zA-Z0-9\-]", "", str(r)) for r in [method_name, *topics]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
return f"dbos-kafka-group-{safe_group_id}"[:255]
|
|
29
|
+
|
|
30
|
+
|
|
22
31
|
def _kafka_consumer_loop(
|
|
23
32
|
func: _KafkaConsumerWorkflow,
|
|
24
33
|
config: dict[str, Any],
|
|
@@ -34,6 +43,12 @@ def _kafka_consumer_loop(
|
|
|
34
43
|
if "auto.offset.reset" not in config:
|
|
35
44
|
config["auto.offset.reset"] = "earliest"
|
|
36
45
|
|
|
46
|
+
if config.get("group.id") is None:
|
|
47
|
+
config["group.id"] = safe_group_name(func.__qualname__, topics)
|
|
48
|
+
dbos_logger.warning(
|
|
49
|
+
f"Consumer group ID not found. Using generated group.id {config['group.id']}"
|
|
50
|
+
)
|
|
51
|
+
|
|
37
52
|
consumer = Consumer(config)
|
|
38
53
|
try:
|
|
39
54
|
consumer.subscribe(topics)
|
|
@@ -71,8 +86,9 @@ def _kafka_consumer_loop(
|
|
|
71
86
|
topic=cmsg.topic(),
|
|
72
87
|
value=cmsg.value(),
|
|
73
88
|
)
|
|
89
|
+
groupID = config.get("group.id")
|
|
74
90
|
with SetWorkflowID(
|
|
75
|
-
f"kafka-unique-id-{msg.topic}-{msg.partition}-{msg.offset}"
|
|
91
|
+
f"kafka-unique-id-{msg.topic}-{msg.partition}-{groupID}-{msg.offset}"
|
|
76
92
|
):
|
|
77
93
|
if in_order:
|
|
78
94
|
assert msg.topic is not None
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""workflow_queues_executor_id
|
|
2
|
+
|
|
3
|
+
Revision ID: 04ca4f231047
|
|
4
|
+
Revises: d76646551a6c
|
|
5
|
+
Create Date: 2025-01-15 15:05:08.043190
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Sequence, Union
|
|
10
|
+
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
from alembic import op
|
|
13
|
+
|
|
14
|
+
# revision identifiers, used by Alembic.
|
|
15
|
+
revision: str = "04ca4f231047"
|
|
16
|
+
down_revision: Union[str, None] = "d76646551a6c"
|
|
17
|
+
branch_labels: Union[str, Sequence[str], None] = None
|
|
18
|
+
depends_on: Union[str, Sequence[str], None] = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def upgrade() -> None:
|
|
22
|
+
op.add_column(
|
|
23
|
+
"workflow_queue",
|
|
24
|
+
sa.Column(
|
|
25
|
+
"executor_id",
|
|
26
|
+
sa.Text(),
|
|
27
|
+
nullable=True,
|
|
28
|
+
),
|
|
29
|
+
schema="dbos",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def downgrade() -> None:
|
|
34
|
+
op.drop_column("workflow_queue", "executor_id", schema="dbos")
|
dbos/_queue.py
CHANGED
|
@@ -2,6 +2,9 @@ import threading
|
|
|
2
2
|
import traceback
|
|
3
3
|
from typing import TYPE_CHECKING, Optional, TypedDict
|
|
4
4
|
|
|
5
|
+
from psycopg import errors
|
|
6
|
+
from sqlalchemy.exc import OperationalError
|
|
7
|
+
|
|
5
8
|
from ._core import P, R, execute_workflow_by_id, start_workflow
|
|
6
9
|
|
|
7
10
|
if TYPE_CHECKING:
|
|
@@ -33,9 +36,20 @@ class Queue:
|
|
|
33
36
|
name: str,
|
|
34
37
|
concurrency: Optional[int] = None,
|
|
35
38
|
limiter: Optional[QueueRateLimit] = None,
|
|
39
|
+
*, # Disable positional arguments from here on
|
|
40
|
+
worker_concurrency: Optional[int] = None,
|
|
36
41
|
) -> None:
|
|
42
|
+
if (
|
|
43
|
+
worker_concurrency is not None
|
|
44
|
+
and concurrency is not None
|
|
45
|
+
and worker_concurrency > concurrency
|
|
46
|
+
):
|
|
47
|
+
raise ValueError(
|
|
48
|
+
"worker_concurrency must be less than or equal to concurrency"
|
|
49
|
+
)
|
|
37
50
|
self.name = name
|
|
38
51
|
self.concurrency = concurrency
|
|
52
|
+
self.worker_concurrency = worker_concurrency
|
|
39
53
|
self.limiter = limiter
|
|
40
54
|
from ._dbos import _get_or_create_dbos_registry
|
|
41
55
|
|
|
@@ -60,6 +74,12 @@ def queue_thread(stop_event: threading.Event, dbos: "DBOS") -> None:
|
|
|
60
74
|
wf_ids = dbos._sys_db.start_queued_workflows(queue, dbos._executor_id)
|
|
61
75
|
for id in wf_ids:
|
|
62
76
|
execute_workflow_by_id(dbos, id)
|
|
77
|
+
except OperationalError as e:
|
|
78
|
+
# Ignore serialization error
|
|
79
|
+
if not isinstance(e.orig, errors.SerializationFailure):
|
|
80
|
+
dbos.logger.warning(
|
|
81
|
+
f"Exception encountered in queue thread: {traceback.format_exc()}"
|
|
82
|
+
)
|
|
63
83
|
except Exception:
|
|
64
84
|
dbos.logger.warning(
|
|
65
85
|
f"Exception encountered in queue thread: {traceback.format_exc()}"
|