dbos 0.21.0a5__tar.gz → 0.22.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.

Files changed (91) hide show
  1. {dbos-0.21.0a5 → dbos-0.22.0}/PKG-INFO +1 -1
  2. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_admin_server.py +21 -0
  3. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_cloudutils/cloudutils.py +4 -2
  4. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_cloudutils/databases.py +4 -0
  5. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_context.py +0 -1
  6. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_core.py +43 -85
  7. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_db_wizard.py +14 -2
  8. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_dbos.py +58 -9
  9. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_error.py +11 -0
  10. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_logger.py +2 -1
  11. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_queue.py +3 -1
  12. dbos-0.22.0/dbos/_recovery.py +69 -0
  13. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_registrations.py +12 -7
  14. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_sys_db.py +120 -24
  15. {dbos-0.21.0a5 → dbos-0.22.0}/pyproject.toml +1 -1
  16. {dbos-0.21.0a5 → dbos-0.22.0}/tests/conftest.py +18 -1
  17. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_admin_server.py +22 -1
  18. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_classdecorators.py +427 -8
  19. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_config.py +23 -0
  20. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_dbos.py +175 -15
  21. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_failures.py +1 -1
  22. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_queue.py +247 -44
  23. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_spans.py +4 -1
  24. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_workflow_cmds.py +2 -2
  25. dbos-0.21.0a5/dbos/_recovery.py +0 -50
  26. {dbos-0.21.0a5 → dbos-0.22.0}/LICENSE +0 -0
  27. {dbos-0.21.0a5 → dbos-0.22.0}/README.md +0 -0
  28. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/__init__.py +0 -0
  29. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_app_db.py +0 -0
  30. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_classproperty.py +0 -0
  31. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_cloudutils/authentication.py +0 -0
  32. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_croniter.py +0 -0
  33. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_dbos_config.py +0 -0
  34. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_fastapi.py +0 -0
  35. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_flask.py +0 -0
  36. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_kafka.py +0 -0
  37. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_kafka_message.py +0 -0
  38. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/env.py +0 -0
  39. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/script.py.mako +0 -0
  40. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/04ca4f231047_workflow_queues_executor_id.py +0 -0
  41. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/50f3227f0b4b_fix_job_queue.py +0 -0
  42. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/5c361fc04708_added_system_tables.py +0 -0
  43. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/a3b18ad34abe_added_triggers.py +0 -0
  44. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/d76646551a6b_job_queue_limiter.py +0 -0
  45. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/d76646551a6c_workflow_queue.py +0 -0
  46. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_migrations/versions/eab0cc1d9a14_job_queue.py +0 -0
  47. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_outcome.py +0 -0
  48. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_request.py +0 -0
  49. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_roles.py +0 -0
  50. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_scheduler.py +0 -0
  51. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_schemas/__init__.py +0 -0
  52. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_schemas/application_database.py +0 -0
  53. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_schemas/system_database.py +0 -0
  54. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_serialization.py +0 -0
  55. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/README.md +0 -0
  56. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/__package/__init__.py +0 -0
  57. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/__package/main.py +0 -0
  58. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/__package/schema.py +0 -0
  59. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/alembic.ini +0 -0
  60. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/dbos-config.yaml.dbos +0 -0
  61. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/migrations/env.py.dbos +0 -0
  62. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/migrations/script.py.mako +0 -0
  63. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/migrations/versions/2024_07_31_180642_init.py +0 -0
  64. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_templates/dbos-db-starter/start_postgres_docker.py +0 -0
  65. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_tracer.py +0 -0
  66. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/_workflow_commands.py +0 -0
  67. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/cli/_github_init.py +0 -0
  68. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/cli/_template_init.py +0 -0
  69. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/cli/cli.py +0 -0
  70. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/dbos-config.schema.json +0 -0
  71. {dbos-0.21.0a5 → dbos-0.22.0}/dbos/py.typed +0 -0
  72. {dbos-0.21.0a5 → dbos-0.22.0}/tests/__init__.py +0 -0
  73. {dbos-0.21.0a5 → dbos-0.22.0}/tests/atexit_no_ctor.py +0 -0
  74. {dbos-0.21.0a5 → dbos-0.22.0}/tests/atexit_no_launch.py +0 -0
  75. {dbos-0.21.0a5 → dbos-0.22.0}/tests/classdefs.py +0 -0
  76. {dbos-0.21.0a5 → dbos-0.22.0}/tests/more_classdefs.py +0 -0
  77. {dbos-0.21.0a5 → dbos-0.22.0}/tests/queuedworkflow.py +0 -0
  78. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_async.py +0 -0
  79. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_concurrency.py +0 -0
  80. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_croniter.py +0 -0
  81. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_fastapi.py +0 -0
  82. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_fastapi_roles.py +0 -0
  83. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_flask.py +0 -0
  84. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_kafka.py +0 -0
  85. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_outcome.py +0 -0
  86. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_package.py +0 -0
  87. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_scheduler.py +0 -0
  88. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_schema_migration.py +0 -0
  89. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_singleton.py +0 -0
  90. {dbos-0.21.0a5 → dbos-0.22.0}/tests/test_sqlalchemy.py +0 -0
  91. {dbos-0.21.0a5 → dbos-0.22.0}/version/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbos
3
- Version: 0.21.0a5
3
+ Version: 0.22.0
4
4
  Summary: Ultra-lightweight durable execution in Python
5
5
  Author-Email: "DBOS, Inc." <contact@dbos.dev>
6
6
  License: MIT
@@ -16,6 +16,7 @@ if TYPE_CHECKING:
16
16
  _health_check_path = "/dbos-healthz"
17
17
  _workflow_recovery_path = "/dbos-workflow-recovery"
18
18
  _deactivate_path = "/deactivate"
19
+ _workflow_queues_metadata_path = "/dbos-workflow-queues-metadata"
19
20
  # /workflows/:workflow_id/cancel
20
21
  # /workflows/:workflow_id/resume
21
22
  # /workflows/:workflow_id/restart
@@ -64,6 +65,26 @@ class AdminRequestHandler(BaseHTTPRequestHandler):
64
65
  self.send_response(200)
65
66
  self._end_headers()
66
67
  self.wfile.write("deactivated".encode("utf-8"))
68
+ elif self.path == _workflow_queues_metadata_path:
69
+ queue_metadata_array = []
70
+ from ._dbos import _get_or_create_dbos_registry
71
+
72
+ registry = _get_or_create_dbos_registry()
73
+ for queue in registry.queue_info_map.values():
74
+ queue_metadata = {
75
+ "name": queue.name,
76
+ "concurrency": queue.concurrency,
77
+ "workerConcurrency": queue.worker_concurrency,
78
+ "rateLimit": queue.limiter,
79
+ }
80
+ # Remove keys with None values
81
+ queue_metadata = {
82
+ k: v for k, v in queue_metadata.items() if v is not None
83
+ }
84
+ queue_metadata_array.append(queue_metadata)
85
+ self.send_response(200)
86
+ self._end_headers()
87
+ self.wfile.write(json.dumps(queue_metadata_array).encode("utf-8"))
67
88
  else:
68
89
  self.send_response(404)
69
90
  self._end_headers()
@@ -29,9 +29,11 @@ class DBOSCloudCredentials:
29
29
  @dataclass
30
30
  class UserProfile:
31
31
  Name: str
32
- Email: str
33
32
  Organization: str
34
- SubscriptionPlan: str
33
+
34
+ def __init__(self, **kwargs: Any) -> None:
35
+ self.Name = kwargs.get("Name", "")
36
+ self.Organization = kwargs.get("Organization", "")
35
37
 
36
38
 
37
39
  class AppLanguages(Enum):
@@ -23,6 +23,10 @@ class UserDBCredentials:
23
23
  RoleName: str
24
24
  Password: str
25
25
 
26
+ def __init__(self, **kwargs: Any) -> None:
27
+ self.RoleName = kwargs.get("RoleName", "")
28
+ self.Password = kwargs.get("Password", "")
29
+
26
30
 
27
31
  @dataclass
28
32
  class UserDBInstance:
@@ -49,7 +49,6 @@ class TracedAttributes(TypedDict, total=False):
49
49
  class DBOSContext:
50
50
  def __init__(self) -> None:
51
51
  self.executor_id = os.environ.get("DBOS__VMID", "local")
52
- self.app_version = os.environ.get("DBOS__APPVERSION", "")
53
52
  self.app_id = os.environ.get("DBOS__APPID", "")
54
53
 
55
54
  self.logger = dbos_logger
@@ -63,6 +63,7 @@ from ._registrations import (
63
63
  get_or_create_func_info,
64
64
  get_temp_workflow_type,
65
65
  set_dbos_func_name,
66
+ set_func_info,
66
67
  set_temp_workflow_type,
67
68
  )
68
69
  from ._roles import check_required_roles
@@ -162,7 +163,7 @@ def _init_workflow(
162
163
  "output": None,
163
164
  "error": None,
164
165
  "app_id": ctx.app_id,
165
- "app_version": ctx.app_version,
166
+ "app_version": dbos.app_version,
166
167
  "executor_id": ctx.executor_id,
167
168
  "request": (
168
169
  _serialization.serialize(ctx.request) if ctx.request is not None else None
@@ -286,6 +287,7 @@ def execute_workflow_by_id(
286
287
  ctx.request = (
287
288
  _serialization.deserialize(request) if request is not None else None
288
289
  )
290
+ # If this function belongs to a configured class, add that class instance as its first argument
289
291
  if status["config_name"] is not None:
290
292
  config_name = status["config_name"]
291
293
  class_name = status["class_name"]
@@ -295,28 +297,9 @@ def execute_workflow_by_id(
295
297
  workflow_id,
296
298
  f"Cannot execute workflow because instance '{iname}' is not registered",
297
299
  )
298
-
299
- if startNew:
300
- return start_workflow(
301
- dbos,
302
- wf_func,
303
- status["queue_name"],
304
- True,
305
- dbos._registry.instance_info_map[iname],
306
- *inputs["args"],
307
- **inputs["kwargs"],
308
- )
309
- else:
310
- with SetWorkflowID(workflow_id):
311
- return start_workflow(
312
- dbos,
313
- wf_func,
314
- status["queue_name"],
315
- True,
316
- dbos._registry.instance_info_map[iname],
317
- *inputs["args"],
318
- **inputs["kwargs"],
319
- )
300
+ class_instance = dbos._registry.instance_info_map[iname]
301
+ inputs["args"] = (class_instance,) + inputs["args"]
302
+ # If this function is a class method, add that class object as its first argument
320
303
  elif status["class_name"] is not None:
321
304
  class_name = status["class_name"]
322
305
  if class_name not in dbos._registry.class_info_map:
@@ -324,30 +307,20 @@ def execute_workflow_by_id(
324
307
  workflow_id,
325
308
  f"Cannot execute workflow because class '{class_name}' is not registered",
326
309
  )
310
+ class_object = dbos._registry.class_info_map[class_name]
311
+ inputs["args"] = (class_object,) + inputs["args"]
327
312
 
328
- if startNew:
329
- return start_workflow(
330
- dbos,
331
- wf_func,
332
- status["queue_name"],
333
- True,
334
- dbos._registry.class_info_map[class_name],
335
- *inputs["args"],
336
- **inputs["kwargs"],
337
- )
338
- else:
339
- with SetWorkflowID(workflow_id):
340
- return start_workflow(
341
- dbos,
342
- wf_func,
343
- status["queue_name"],
344
- True,
345
- dbos._registry.class_info_map[class_name],
346
- *inputs["args"],
347
- **inputs["kwargs"],
348
- )
313
+ if startNew:
314
+ return start_workflow(
315
+ dbos,
316
+ wf_func,
317
+ status["queue_name"],
318
+ True,
319
+ *inputs["args"],
320
+ **inputs["kwargs"],
321
+ )
349
322
  else:
350
- if startNew:
323
+ with SetWorkflowID(workflow_id):
351
324
  return start_workflow(
352
325
  dbos,
353
326
  wf_func,
@@ -356,16 +329,6 @@ def execute_workflow_by_id(
356
329
  *inputs["args"],
357
330
  **inputs["kwargs"],
358
331
  )
359
- else:
360
- with SetWorkflowID(workflow_id):
361
- return start_workflow(
362
- dbos,
363
- wf_func,
364
- status["queue_name"],
365
- True,
366
- *inputs["args"],
367
- **inputs["kwargs"],
368
- )
369
332
 
370
333
 
371
334
  @overload
@@ -398,9 +361,12 @@ def start_workflow(
398
361
  *args: P.args,
399
362
  **kwargs: P.kwargs,
400
363
  ) -> "WorkflowHandle[R]":
364
+ # If the function has a class, add the class object as its first argument
401
365
  fself: Optional[object] = None
402
366
  if hasattr(func, "__self__"):
403
367
  fself = func.__self__
368
+ if fself is not None:
369
+ args = (fself,) + args # type: ignore
404
370
 
405
371
  fi = get_func_info(func)
406
372
  if fi is None:
@@ -436,17 +402,13 @@ def start_workflow(
436
402
  new_wf_ctx.id_assigned_for_next_workflow = new_wf_ctx.assign_workflow_id()
437
403
  new_wf_id = new_wf_ctx.id_assigned_for_next_workflow
438
404
 
439
- gin_args: Tuple[Any, ...] = args
440
- if fself is not None:
441
- gin_args = (fself,)
442
-
443
405
  status = _init_workflow(
444
406
  dbos,
445
407
  new_wf_ctx,
446
408
  inputs=inputs,
447
409
  wf_name=get_dbos_func_name(func),
448
- class_name=get_dbos_class_name(fi, func, gin_args),
449
- config_name=get_config_name(fi, func, gin_args),
410
+ class_name=get_dbos_class_name(fi, func, args),
411
+ config_name=get_config_name(fi, func, args),
450
412
  temp_wf_type=get_temp_workflow_type(func),
451
413
  queue=queue_name,
452
414
  max_recovery_attempts=fi.max_recovery_attempts,
@@ -464,27 +426,15 @@ def start_workflow(
464
426
  )
465
427
  return WorkflowHandlePolling(new_wf_id, dbos)
466
428
 
467
- if fself is not None:
468
- future = dbos._executor.submit(
469
- cast(Callable[..., R], _execute_workflow_wthread),
470
- dbos,
471
- status,
472
- func,
473
- new_wf_ctx,
474
- fself,
475
- *args,
476
- **kwargs,
477
- )
478
- else:
479
- future = dbos._executor.submit(
480
- cast(Callable[..., R], _execute_workflow_wthread),
481
- dbos,
482
- status,
483
- func,
484
- new_wf_ctx,
485
- *args,
486
- **kwargs,
487
- )
429
+ future = dbos._executor.submit(
430
+ cast(Callable[..., R], _execute_workflow_wthread),
431
+ dbos,
432
+ status,
433
+ func,
434
+ new_wf_ctx,
435
+ *args,
436
+ **kwargs,
437
+ )
488
438
  return WorkflowHandleFuture(new_wf_id, future, dbos)
489
439
 
490
440
 
@@ -516,6 +466,8 @@ def workflow_wrapper(
516
466
 
517
467
  @wraps(func)
518
468
  def wrapper(*args: Any, **kwargs: Any) -> R:
469
+ fi = get_func_info(func)
470
+ assert fi is not None
519
471
  if dbosreg.dbos is None:
520
472
  raise DBOSException(
521
473
  f"Function {func.__name__} invoked before DBOS initialized"
@@ -572,7 +524,7 @@ def decorate_workflow(
572
524
  ) -> Callable[[Callable[P, R]], Callable[P, R]]:
573
525
  def _workflow_decorator(func: Callable[P, R]) -> Callable[P, R]:
574
526
  wrapped_func = workflow_wrapper(reg, func, max_recovery_attempts)
575
- reg.register_wf_function(func.__qualname__, wrapped_func)
527
+ reg.register_wf_function(func.__qualname__, wrapped_func, "workflow")
576
528
  return wrapped_func
577
529
 
578
530
  return _workflow_decorator
@@ -724,8 +676,12 @@ def decorate_transaction(
724
676
  wrapped_wf = workflow_wrapper(dbosreg, temp_wf)
725
677
  set_dbos_func_name(temp_wf, "<temp>." + func.__qualname__)
726
678
  set_temp_workflow_type(temp_wf, "transaction")
727
- dbosreg.register_wf_function(get_dbos_func_name(temp_wf), wrapped_wf)
679
+ dbosreg.register_wf_function(
680
+ get_dbos_func_name(temp_wf), wrapped_wf, "transaction"
681
+ )
728
682
  wrapper.__orig_func = temp_wf # type: ignore
683
+ set_func_info(wrapped_wf, get_or_create_func_info(func))
684
+ set_func_info(temp_wf, get_or_create_func_info(func))
729
685
 
730
686
  return cast(F, wrapper)
731
687
 
@@ -873,8 +829,10 @@ def decorate_step(
873
829
  wrapped_wf = workflow_wrapper(dbosreg, temp_wf)
874
830
  set_dbos_func_name(temp_wf, "<temp>." + func.__qualname__)
875
831
  set_temp_workflow_type(temp_wf, "step")
876
- dbosreg.register_wf_function(get_dbos_func_name(temp_wf), wrapped_wf)
832
+ dbosreg.register_wf_function(get_dbos_func_name(temp_wf), wrapped_wf, "step")
877
833
  wrapper.__orig_func = temp_wf # type: ignore
834
+ set_func_info(wrapped_wf, get_or_create_func_info(func))
835
+ set_func_info(temp_wf, get_or_create_func_info(func))
878
836
 
879
837
  return cast(Callable[P, R], wrapper)
880
838
 
@@ -1,7 +1,7 @@
1
1
  import json
2
2
  import os
3
3
  import time
4
- from typing import TYPE_CHECKING, Optional, TypedDict
4
+ from typing import TYPE_CHECKING, Optional, TypedDict, cast
5
5
 
6
6
  import docker # type: ignore
7
7
  import typer
@@ -45,8 +45,20 @@ def db_wizard(config: "ConfigFile", config_file_path: str) -> "ConfigFile":
45
45
  f"Could not connect to Postgres: password authentication failed: {db_connection_error}"
46
46
  )
47
47
  db_config = config["database"]
48
+
49
+ # Read the config file and check if the database hostname/port/username are set. If so, skip the wizard.
50
+ with open(config_file_path, "r") as file:
51
+ content = file.read()
52
+ local_config = yaml.safe_load(content)
53
+ if "database" not in local_config:
54
+ local_config["database"] = {}
55
+ local_config = cast("ConfigFile", local_config)
56
+
48
57
  if (
49
- db_config["hostname"] != "localhost"
58
+ local_config["database"].get("hostname")
59
+ or local_config["database"].get("port")
60
+ or local_config["database"].get("username")
61
+ or db_config["hostname"] != "localhost"
50
62
  or db_config["port"] != 5432
51
63
  or db_config["username"] != "postgres"
52
64
  ):
@@ -2,6 +2,8 @@ from __future__ import annotations
2
2
 
3
3
  import asyncio
4
4
  import atexit
5
+ import hashlib
6
+ import inspect
5
7
  import json
6
8
  import os
7
9
  import sys
@@ -56,7 +58,7 @@ from ._registrations import (
56
58
  )
57
59
  from ._roles import default_required_roles, required_roles
58
60
  from ._scheduler import ScheduledWorkflow, scheduled
59
- from ._sys_db import WorkflowStatusString, reset_system_database
61
+ from ._sys_db import reset_system_database
60
62
  from ._tracer import dbos_tracer
61
63
 
62
64
  if TYPE_CHECKING:
@@ -83,7 +85,11 @@ from ._context import (
83
85
  get_local_dbos_context,
84
86
  )
85
87
  from ._dbos_config import ConfigFile, load_config, set_env_vars
86
- from ._error import DBOSException, DBOSNonExistentWorkflowError
88
+ from ._error import (
89
+ DBOSConflictingRegistrationError,
90
+ DBOSException,
91
+ DBOSNonExistentWorkflowError,
92
+ )
87
93
  from ._logger import add_otlp_to_all_loggers, dbos_logger
88
94
  from ._sys_db import SystemDatabase
89
95
 
@@ -142,6 +148,7 @@ RegisteredJob = Tuple[
142
148
  class DBOSRegistry:
143
149
  def __init__(self) -> None:
144
150
  self.workflow_info_map: dict[str, Workflow[..., Any]] = {}
151
+ self.function_type_map: dict[str, str] = {}
145
152
  self.class_info_map: dict[str, type] = {}
146
153
  self.instance_info_map: dict[str, object] = {}
147
154
  self.queue_info_map: dict[str, Queue] = {}
@@ -149,7 +156,11 @@ class DBOSRegistry:
149
156
  self.dbos: Optional[DBOS] = None
150
157
  self.config: Optional[ConfigFile] = None
151
158
 
152
- def register_wf_function(self, name: str, wrapped_func: F) -> None:
159
+ def register_wf_function(self, name: str, wrapped_func: F, functype: str) -> None:
160
+ if name in self.function_type_map:
161
+ if self.function_type_map[name] != functype:
162
+ raise DBOSConflictingRegistrationError(name)
163
+ self.function_type_map[name] = functype
153
164
  self.workflow_info_map[name] = wrapped_func
154
165
 
155
166
  def register_class(self, cls: type, ci: DBOSClassInfo) -> None:
@@ -186,6 +197,22 @@ class DBOSRegistry:
186
197
  else:
187
198
  self.instance_info_map[fn] = inst
188
199
 
200
+ def compute_app_version(self) -> str:
201
+ """
202
+ An application's version is computed from a hash of the source of its workflows.
203
+ This is guaranteed to be stable given identical source code because it uses an MD5 hash
204
+ and because it iterates through the workflows in sorted order.
205
+ This way, if the app's workflows are updated (which would break recovery), its version changes.
206
+ App version can be manually set through the DBOS__APPVERSION environment variable.
207
+ """
208
+ hasher = hashlib.md5()
209
+ sources = sorted(
210
+ [inspect.getsource(wf) for wf in self.workflow_info_map.values()]
211
+ )
212
+ for source in sources:
213
+ hasher.update(source.encode("utf-8"))
214
+ return hasher.hexdigest()
215
+
189
216
 
190
217
  class DBOS:
191
218
  """
@@ -283,6 +310,7 @@ class DBOS:
283
310
  self._executor_field: Optional[ThreadPoolExecutor] = None
284
311
  self._background_threads: List[threading.Thread] = []
285
312
  self._executor_id: str = os.environ.get("DBOS__VMID", "local")
313
+ self.app_version: str = os.environ.get("DBOS__APPVERSION", "")
286
314
 
287
315
  # If using FastAPI, set up middleware and lifecycle events
288
316
  if self.fastapi is not None:
@@ -305,7 +333,7 @@ class DBOS:
305
333
  temp_send_wf = workflow_wrapper(self._registry, send_temp_workflow)
306
334
  set_dbos_func_name(send_temp_workflow, TEMP_SEND_WF_NAME)
307
335
  set_temp_workflow_type(send_temp_workflow, "send")
308
- self._registry.register_wf_function(TEMP_SEND_WF_NAME, temp_send_wf)
336
+ self._registry.register_wf_function(TEMP_SEND_WF_NAME, temp_send_wf, "send")
309
337
 
310
338
  for handler in dbos_logger.handlers:
311
339
  handler.flush()
@@ -351,6 +379,10 @@ class DBOS:
351
379
  dbos_logger.warning(f"DBOS was already launched")
352
380
  return
353
381
  self._launched = True
382
+ if self.app_version == "":
383
+ self.app_version = self._registry.compute_app_version()
384
+ dbos_logger.info(f"Application version: {self.app_version}")
385
+ dbos_tracer.app_version = self.app_version
354
386
  self._executor_field = ThreadPoolExecutor(max_workers=64)
355
387
  self._sys_db_field = SystemDatabase(self.config)
356
388
  self._app_db_field = ApplicationDatabase(self.config)
@@ -359,9 +391,19 @@ class DBOS:
359
391
  admin_port = 3001
360
392
  self._admin_server_field = AdminServer(dbos=self, port=admin_port)
361
393
 
362
- if not os.environ.get("DBOS__VMID"):
363
- workflow_ids = self._sys_db.get_pending_workflows("local")
364
- self._executor.submit(startup_recovery_thread, self, workflow_ids)
394
+ workflow_ids = self._sys_db.get_pending_workflows(
395
+ self._executor_id, self.app_version
396
+ )
397
+ if (len(workflow_ids)) > 0:
398
+ self.logger.info(
399
+ f"Recovering {len(workflow_ids)} workflows from application version {self.app_version}"
400
+ )
401
+ else:
402
+ self.logger.info(
403
+ f"No workflows to recover from application version {self.app_version}"
404
+ )
405
+
406
+ self._executor.submit(startup_recovery_thread, self, workflow_ids)
365
407
 
366
408
  # Listen to notifications
367
409
  notification_listener_thread = threading.Thread(
@@ -398,13 +440,13 @@ class DBOS:
398
440
  self._background_threads.append(poller_thread)
399
441
  self._registry.pollers = []
400
442
 
401
- dbos_logger.info("DBOS launched")
443
+ dbos_logger.info("DBOS launched!")
402
444
 
403
445
  # Flush handlers and add OTLP to all loggers if enabled
404
446
  # to enable their export in DBOS Cloud
405
447
  for handler in dbos_logger.handlers:
406
448
  handler.flush()
407
- add_otlp_to_all_loggers()
449
+ add_otlp_to_all_loggers(self.app_version)
408
450
  except Exception:
409
451
  dbos_logger.error(f"DBOS failed to launch: {traceback.format_exc()}")
410
452
  raise
@@ -613,6 +655,7 @@ class DBOS:
613
655
  workflow_id=workflow_id,
614
656
  status=stat["status"],
615
657
  name=stat["name"],
658
+ executor_id=stat["executor_id"],
616
659
  recovery_attempts=stat["recovery_attempts"],
617
660
  class_name=stat["class_name"],
618
661
  config_name=stat["config_name"],
@@ -909,6 +952,7 @@ class WorkflowStatus:
909
952
  workflow_id(str): The ID of the workflow execution
910
953
  status(str): The status of the execution, from `WorkflowStatusString`
911
954
  name(str): The workflow function name
955
+ executor_id(str): The ID of the executor running the workflow
912
956
  class_name(str): For member functions, the name of the class containing the workflow function
913
957
  config_name(str): For instance member functions, the name of the class instance for the execution
914
958
  queue_name(str): For workflows that are or were queued, the queue name
@@ -922,6 +966,7 @@ class WorkflowStatus:
922
966
  workflow_id: str
923
967
  status: str
924
968
  name: str
969
+ executor_id: Optional[str]
925
970
  class_name: Optional[str]
926
971
  config_name: Optional[str]
927
972
  queue_name: Optional[str]
@@ -994,6 +1039,10 @@ def _dbos_exit_hook() -> None:
994
1039
  )
995
1040
  return
996
1041
  if not _dbos_global_instance._launched:
1042
+ if _dbos_global_instance.fastapi is not None:
1043
+ # FastAPI lifespan middleware will call launch/destroy, so we can ignore this.
1044
+ # This is likely to happen during fastapi dev runs, where the reloader loads the module multiple times.
1045
+ return
997
1046
  print("DBOS exiting; DBOS exists but launch() was not called")
998
1047
  dbos_logger.warning("DBOS exiting; DBOS exists but launch() was not called")
999
1048
  return
@@ -36,6 +36,7 @@ class DBOSErrorCode(Enum):
36
36
  MaxStepRetriesExceeded = 7
37
37
  NotAuthorized = 8
38
38
  ConflictingWorkflowError = 9
39
+ ConflictingRegistrationError = 25
39
40
 
40
41
 
41
42
  class DBOSWorkflowConflictIDError(DBOSException):
@@ -127,3 +128,13 @@ class DBOSMaxStepRetriesExceeded(DBOSException):
127
128
  "Step reached maximum retries.",
128
129
  dbos_error_code=DBOSErrorCode.MaxStepRetriesExceeded.value,
129
130
  )
131
+
132
+
133
+ class DBOSConflictingRegistrationError(DBOSException):
134
+ """Exception raised when conflicting decorators are applied to the same function."""
135
+
136
+ def __init__(self, name: str) -> None:
137
+ super().__init__(
138
+ f"Operation (Name: {name}) is already registered with a conflicting function type",
139
+ dbos_error_code=DBOSErrorCode.ConflictingRegistrationError.value,
140
+ )
@@ -86,8 +86,9 @@ def config_logger(config: "ConfigFile") -> None:
86
86
  dbos_logger.addFilter(_otlp_transformer)
87
87
 
88
88
 
89
- def add_otlp_to_all_loggers() -> None:
89
+ def add_otlp_to_all_loggers(app_version: str) -> None:
90
90
  if _otlp_handler is not None and _otlp_transformer is not None:
91
+ _otlp_transformer.app_version = app_version
91
92
  root = logging.root
92
93
 
93
94
  root.addHandler(_otlp_handler)
@@ -76,7 +76,9 @@ def queue_thread(stop_event: threading.Event, dbos: "DBOS") -> None:
76
76
  execute_workflow_by_id(dbos, id)
77
77
  except OperationalError as e:
78
78
  # Ignore serialization error
79
- if not isinstance(e.orig, errors.SerializationFailure):
79
+ if not isinstance(
80
+ e.orig, (errors.SerializationFailure, errors.LockNotAvailable)
81
+ ):
80
82
  dbos.logger.warning(
81
83
  f"Exception encountered in queue thread: {traceback.format_exc()}"
82
84
  )
@@ -0,0 +1,69 @@
1
+ import os
2
+ import threading
3
+ import time
4
+ import traceback
5
+ from typing import TYPE_CHECKING, Any, List
6
+
7
+ from ._core import execute_workflow_by_id
8
+ from ._error import DBOSWorkflowFunctionNotFoundError
9
+ from ._sys_db import GetPendingWorkflowsOutput
10
+
11
+ if TYPE_CHECKING:
12
+ from ._dbos import DBOS, WorkflowHandle
13
+
14
+
15
+ def startup_recovery_thread(
16
+ dbos: "DBOS", pending_workflows: List[GetPendingWorkflowsOutput]
17
+ ) -> None:
18
+ """Attempt to recover local pending workflows on startup using a background thread."""
19
+ stop_event = threading.Event()
20
+ dbos.stop_events.append(stop_event)
21
+ while not stop_event.is_set() and len(pending_workflows) > 0:
22
+ try:
23
+ for pending_workflow in list(pending_workflows):
24
+ if (
25
+ pending_workflow.queue_name
26
+ and pending_workflow.queue_name != "_dbos_internal_queue"
27
+ ):
28
+ dbos._sys_db.clear_queue_assignment(pending_workflow.workflow_uuid)
29
+ continue
30
+ execute_workflow_by_id(dbos, pending_workflow.workflow_uuid)
31
+ pending_workflows.remove(pending_workflow)
32
+ except DBOSWorkflowFunctionNotFoundError:
33
+ time.sleep(1)
34
+ except Exception as e:
35
+ dbos.logger.error(
36
+ f"Exception encountered when recovering workflows: {traceback.format_exc()}"
37
+ )
38
+ raise e
39
+
40
+
41
+ def recover_pending_workflows(
42
+ dbos: "DBOS", executor_ids: List[str] = ["local"]
43
+ ) -> List["WorkflowHandle[Any]"]:
44
+ workflow_handles: List["WorkflowHandle[Any]"] = []
45
+ for executor_id in executor_ids:
46
+ dbos.logger.debug(f"Recovering pending workflows for executor: {executor_id}")
47
+ pending_workflows = dbos._sys_db.get_pending_workflows(
48
+ executor_id, dbos.app_version
49
+ )
50
+ for pending_workflow in pending_workflows:
51
+ if (
52
+ pending_workflow.queue_name
53
+ and pending_workflow.queue_name != "_dbos_internal_queue"
54
+ ):
55
+ try:
56
+ dbos._sys_db.clear_queue_assignment(pending_workflow.workflow_uuid)
57
+ workflow_handles.append(
58
+ dbos.retrieve_workflow(pending_workflow.workflow_uuid)
59
+ )
60
+ except Exception as e:
61
+ dbos.logger.error(e)
62
+ else:
63
+ workflow_handles.append(
64
+ execute_workflow_by_id(dbos, pending_workflow.workflow_uuid)
65
+ )
66
+ dbos.logger.info(
67
+ f"Recovering {len(pending_workflows)} workflows from version {dbos.app_version}"
68
+ )
69
+ return workflow_handles
@@ -1,4 +1,5 @@
1
1
  import inspect
2
+ from dataclasses import dataclass
2
3
  from enum import Enum
3
4
  from types import FunctionType
4
5
  from typing import Any, Callable, List, Literal, Optional, Tuple, Type, cast
@@ -31,9 +32,9 @@ def set_temp_workflow_type(f: Any, name: TempWorkflowType) -> None:
31
32
  setattr(f, "dbos_temp_workflow_type", name)
32
33
 
33
34
 
35
+ @dataclass
34
36
  class DBOSClassInfo:
35
- def __init__(self) -> None:
36
- self.def_required_roles: Optional[List[str]] = None
37
+ def_required_roles: Optional[List[str]] = None
37
38
 
38
39
 
39
40
  class DBOSFuncType(Enum):
@@ -44,12 +45,12 @@ class DBOSFuncType(Enum):
44
45
  Instance = 4
45
46
 
46
47
 
48
+ @dataclass
47
49
  class DBOSFuncInfo:
48
- def __init__(self) -> None:
49
- self.class_info: Optional[DBOSClassInfo] = None
50
- self.func_type: DBOSFuncType = DBOSFuncType.Unknown
51
- self.required_roles: Optional[List[str]] = None
52
- self.max_recovery_attempts = DEFAULT_MAX_RECOVERY_ATTEMPTS
50
+ class_info: Optional[DBOSClassInfo] = None
51
+ func_type: DBOSFuncType = DBOSFuncType.Unknown
52
+ required_roles: Optional[List[str]] = None
53
+ max_recovery_attempts: int = DEFAULT_MAX_RECOVERY_ATTEMPTS
53
54
 
54
55
 
55
56
  def get_or_create_class_info(cls: Type[Any]) -> DBOSClassInfo:
@@ -110,6 +111,10 @@ def get_or_create_func_info(func: Callable[..., Any]) -> DBOSFuncInfo:
110
111
  return fi
111
112
 
112
113
 
114
+ def set_func_info(func: Callable[..., Any], fi: DBOSFuncInfo) -> None:
115
+ setattr(func, "dbos_func_decorator_info", fi)
116
+
117
+
113
118
  def get_class_info(cls: Type[Any]) -> Optional[DBOSClassInfo]:
114
119
  if hasattr(cls, "dbos_class_decorator_info"):
115
120
  ci: DBOSClassInfo = getattr(cls, "dbos_class_decorator_info")