-
Notifications
You must be signed in to change notification settings - Fork 44.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Introduce executors shared DB connection (#8340)
- Loading branch information
Showing
20 changed files
with
364 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .database import DatabaseManager | ||
from .manager import ExecutionManager | ||
from .scheduler import ExecutionScheduler | ||
|
||
__all__ = [ | ||
"DatabaseManager", | ||
"ExecutionManager", | ||
"ExecutionScheduler", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from functools import wraps | ||
from typing import Any, Callable, Concatenate, Coroutine, ParamSpec, TypeVar, cast | ||
|
||
from backend.data.credit import get_user_credit_model | ||
from backend.data.execution import ( | ||
ExecutionResult, | ||
create_graph_execution, | ||
get_execution_results, | ||
get_incomplete_executions, | ||
get_latest_execution, | ||
update_execution_status, | ||
update_graph_execution_stats, | ||
update_node_execution_stats, | ||
upsert_execution_input, | ||
upsert_execution_output, | ||
) | ||
from backend.data.graph import get_graph, get_node | ||
from backend.data.queue import RedisEventQueue | ||
from backend.util.service import AppService, expose | ||
from backend.util.settings import Config | ||
|
||
P = ParamSpec("P") | ||
R = TypeVar("R") | ||
|
||
|
||
class DatabaseManager(AppService): | ||
|
||
def __init__(self): | ||
super().__init__(port=Config().database_api_port) | ||
self.use_db = True | ||
self.use_redis = True | ||
self.event_queue = RedisEventQueue() | ||
|
||
@expose | ||
def send_execution_update(self, execution_result_dict: dict[Any, Any]): | ||
self.event_queue.put(ExecutionResult(**execution_result_dict)) | ||
|
||
@staticmethod | ||
def exposed_run_and_wait( | ||
f: Callable[P, Coroutine[None, None, R]] | ||
) -> Callable[Concatenate[object, P], R]: | ||
@expose | ||
@wraps(f) | ||
def wrapper(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
coroutine = f(*args, **kwargs) | ||
res = self.run_and_wait(coroutine) | ||
return res | ||
|
||
return wrapper | ||
|
||
# Executions | ||
create_graph_execution = exposed_run_and_wait(create_graph_execution) | ||
get_execution_results = exposed_run_and_wait(get_execution_results) | ||
get_incomplete_executions = exposed_run_and_wait(get_incomplete_executions) | ||
get_latest_execution = exposed_run_and_wait(get_latest_execution) | ||
update_execution_status = exposed_run_and_wait(update_execution_status) | ||
update_graph_execution_stats = exposed_run_and_wait(update_graph_execution_stats) | ||
update_node_execution_stats = exposed_run_and_wait(update_node_execution_stats) | ||
upsert_execution_input = exposed_run_and_wait(upsert_execution_input) | ||
upsert_execution_output = exposed_run_and_wait(upsert_execution_output) | ||
|
||
# Graphs | ||
get_node = exposed_run_and_wait(get_node) | ||
get_graph = exposed_run_and_wait(get_graph) | ||
|
||
# Credits | ||
user_credit_model = get_user_credit_model() | ||
get_or_refill_credit = cast( | ||
Callable[[Any, str], int], | ||
exposed_run_and_wait(user_credit_model.get_or_refill_credit), | ||
) | ||
spend_credits = cast( | ||
Callable[[Any, str, int, str, dict[str, str], float, float], int], | ||
exposed_run_and_wait(user_credit_model.spend_credits), | ||
) |
Oops, something went wrong.