AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(rnd): Split Execution Manager (#8008)
Aarushi0b919522ae5d
diff --git a/.dockerignore b/.dockerignoreindex bc751a07a0..f18c4dc2b1 100644--- a/.dockerignore+++ b/.dockerignore@@ -36,4 +36,3 @@ rnd/autogpt_builder/.env.examplernd/autogpt_builder/.env.localrnd/autogpt_server/.envrnd/autogpt_server/.venv/-diff --git a/rnd/autogpt_server/.env.example b/rnd/autogpt_server/.env.exampleindex 5df926e0ba..3a94bbcfd1 100644--- a/rnd/autogpt_server/.env.example+++ b/rnd/autogpt_server/.env.example@@ -11,6 +11,7 @@ REDIS_PASSWORD=passwordAUTH_ENABLED=falseAPP_ENV="local"+PYRO_HOST=localhostSENTRY_DSN=## ===== OPTIONAL API KEYS ===== ##diff --git a/rnd/autogpt_server/Dockerfile b/rnd/autogpt_server/Dockerfileindex e0f51b50d8..3621aee8f6 100644--- a/rnd/autogpt_server/Dockerfile+++ b/rnd/autogpt_server/Dockerfile@@ -4,7 +4,6 @@ FROM python:3.11-slim-buster as server_baseENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1-WORKDIR /appRUN apt-get update \@@ -17,7 +16,6 @@ RUN apt-get update \&& make prefix=/usr all \&& make prefix=/usr install-ENV POETRY_VERSION=1.8.3 \POETRY_HOME="/opt/poetry" \POETRY_NO_INTERACTION=1 \@@ -44,3 +42,10 @@ ENV PORT=8000ENV DATABASE_URL=""CMD ["poetry", "run", "rest"]++FROM server_base as executor++ENV PORT=8002+ENV DATABASE_URL=""++CMD ["poetry", "run", "executor"]diff --git a/rnd/autogpt_server/README.md b/rnd/autogpt_server/README.mdindex b7db35745c..a1ecdb37f8 100644--- a/rnd/autogpt_server/README.md+++ b/rnd/autogpt_server/README.md@@ -183,6 +183,13 @@ A communication layer (`service.py`) is created to decouple the communication liCurrently, the IPC is done using Pyro5 and abstracted in a way that allows a function decorated with `@expose` to be called from a different process.++By default the daemons run on the following ports:++Execution Manager Daemon: 8002+Execution Scheduler Daemon: 8003+Rest Server Daemon: 8004+## Adding a New Agent BlockTo add a new agent block, you need to create a new class that inherits from `Block` and provides the following information:diff --git a/rnd/autogpt_server/autogpt_server/app.py b/rnd/autogpt_server/autogpt_server/app.pyindex acdf4fb1e9..911d14865f 100644--- a/rnd/autogpt_server/autogpt_server/app.py+++ b/rnd/autogpt_server/autogpt_server/app.py@@ -26,10 +26,8 @@ def main(**kwargs):from autogpt_server.executor import ExecutionManager, ExecutionSchedulerfrom autogpt_server.server import AgentServer, WebsocketServer- from autogpt_server.util.service import PyroNameServerrun_processes(- PyroNameServer(),ExecutionManager(),ExecutionScheduler(),WebsocketServer(),diff --git a/rnd/autogpt_server/autogpt_server/exec.py b/rnd/autogpt_server/autogpt_server/exec.pynew file mode 100644index 0000000000..09864b7b7d--- /dev/null+++ b/rnd/autogpt_server/autogpt_server/exec.py@@ -0,0 +1,15 @@+from autogpt_server.app import run_processes+from autogpt_server.executor import ExecutionManager+++def main():+ """+ Run all the processes required for the AutoGPT-server REST API.+ """+ run_processes(+ ExecutionManager(),+ )+++if __name__ == "__main__":+ main()diff --git a/rnd/autogpt_server/autogpt_server/executor/manager.py b/rnd/autogpt_server/autogpt_server/executor/manager.pyindex 2950bdefbf..42ca2be57b 100644--- a/rnd/autogpt_server/autogpt_server/executor/manager.py+++ b/rnd/autogpt_server/autogpt_server/executor/manager.py@@ -364,7 +364,7 @@ def validate_exec(def get_agent_server_client() -> "AgentServer":from autogpt_server.server.rest_api import AgentServer- return get_service_client(AgentServer)+ return get_service_client(AgentServer, Config().agent_server_port)class Executor:@@ -648,6 +648,7 @@ class Executor:class ExecutionManager(AppService):def __init__(self):+ super().__init__(port=Config().execution_manager_port)self.use_db = Trueself.pool_size = Config().num_graph_workersself.queue = ExecutionQueue[GraphExecution]()diff --git a/rnd/autogpt_server/autogpt_server/executor/scheduler.py b/rnd/autogpt_server/autogpt_server/executor/scheduler.pyindex 69db7c5873..f367b2d75d 100644--- a/rnd/autogpt_server/autogpt_server/executor/scheduler.py+++ b/rnd/autogpt_server/autogpt_server/executor/scheduler.py@@ -9,6 +9,7 @@ from autogpt_server.data import schedule as modelfrom autogpt_server.data.block import BlockInputfrom autogpt_server.executor.manager import ExecutionManagerfrom autogpt_server.util.service import AppService, expose, get_service_client+from autogpt_server.util.settings import Configlogger = logging.getLogger(__name__)@@ -19,13 +20,15 @@ def log(msg, **kwargs):class ExecutionScheduler(AppService):def __init__(self, refresh_interval=10):+ super().__init__(port=Config().execution_scheduler_port)self.use_db = Trueself.last_check = datetime.minself.refresh_interval = refresh_interval+ self.use_redis = False@propertydef execution_manager_client(self) -> ExecutionManager:- return get_service_client(ExecutionManager)+ return get_service_client(ExecutionManager, Config().execution_manager_port)def run_service(self):scheduler = BackgroundScheduler()diff --git a/rnd/autogpt_server/autogpt_server/rest.py b/rnd/autogpt_server/autogpt_server/rest.pyindex 64066d612e..9831ae62ad 100644--- a/rnd/autogpt_server/autogpt_server/rest.py+++ b/rnd/autogpt_server/autogpt_server/rest.py@@ -1,7 +1,6 @@from autogpt_server.app import run_processes-from autogpt_server.executor import ExecutionManager, ExecutionScheduler+from autogpt_server.executor import ExecutionSchedulerfrom autogpt_server.server import AgentServer-from autogpt_server.util.service import PyroNameServerdef main():@@ -9,8 +8,6 @@ def main():Run all the processes required for the AutoGPT-server REST API."""run_processes(- PyroNameServer(),- ExecutionManager(),ExecutionScheduler(),AgentServer(),)diff --git a/rnd/autogpt_server/autogpt_server/server/rest_api.py b/rnd/autogpt_server/autogpt_server/server/rest_api.pyindex 6ba2653ab7..9d6298f221 100644--- a/rnd/autogpt_server/autogpt_server/server/rest_api.py+++ b/rnd/autogpt_server/autogpt_server/server/rest_api.py@@ -21,7 +21,7 @@ from autogpt_server.executor import ExecutionManager, ExecutionSchedulerfrom autogpt_server.server.model import CreateGraph, SetGraphActiveVersionfrom autogpt_server.util.lock import KeyedMutexfrom autogpt_server.util.service import AppService, expose, get_service_client-from autogpt_server.util.settings import Settings+from autogpt_server.util.settings import Config, Settingsfrom .utils import get_user_id@@ -34,6 +34,7 @@ class AgentServer(AppService):_test_dependency_overrides = {}def __init__(self, event_queue: AsyncEventQueue | None = None):+ super().__init__(port=Config().agent_server_port)self.event_queue = event_queue or AsyncRedisEventQueue()@asynccontextmanager@@ -239,11 +240,11 @@ class AgentServer(AppService):@propertydef execution_manager_client(self) -> ExecutionManager:- return get_service_client(ExecutionManager)+ return get_service_client(ExecutionManager, Config().execution_manager_port)@propertydef execution_scheduler_client(self) -> ExecutionScheduler:- return get_service_client(ExecutionScheduler)+ return get_service_client(ExecutionScheduler, Config().execution_scheduler_port)@classmethoddef handle_internal_http_error(cls, request: Request, exc: Exception):diff --git a/rnd/autogpt_server/autogpt_server/util/service.py b/rnd/autogpt_server/autogpt_server/util/service.pyindex 22292d6e4d..6e707b904d 100644--- a/rnd/autogpt_server/autogpt_server/util/service.py+++ b/rnd/autogpt_server/autogpt_server/util/service.py@@ -1,12 +1,13 @@import asyncioimport logging+import osimport threadingimport timefrom abc import abstractmethodfrom typing import Any, Callable, Coroutine, Type, TypeVar, cast+import Pyro5.apifrom Pyro5 import api as pyro-from Pyro5 import nameserverfrom autogpt_server.data import dbfrom autogpt_server.data.queue import AsyncEventQueue, AsyncRedisEventQueue@@ -42,25 +43,16 @@ def expose(func: C) -> C:return pyro.expose(wrapper) # type: ignore-class PyroNameServer(AppProcess):- def run(self):- nameserver.start_ns_loop(host=pyro_host, port=9090)-- @conn_retry- def _wait_for_ns(self):- pyro.locate_ns(host="localhost", port=9090)-- def health_check(self):- self._wait_for_ns()- logger.info(f"{__class__.__name__} is ready")--class AppService(AppProcess):shared_event_loop: asyncio.AbstractEventLoopevent_queue: AsyncEventQueue = AsyncRedisEventQueue()use_db: bool = Falseuse_redis: bool = False+ def __init__(self, port):+ self.port = port+ self.uri = None+@classmethod@propertydef service_name(cls) -> str:@@ -108,11 +100,10 @@ class AppService(AppProcess):@conn_retrydef __start_pyro(self):- daemon = pyro.Daemon(host=pyro_host)- ns = pyro.locate_ns(host=pyro_host, port=9090)- uri = daemon.register(self)- ns.register(self.service_name, uri)- logger.info(f"[{self.service_name}] Connected to Pyro; URI = {uri}")+ host = Config().pyro_host+ daemon = Pyro5.api.Daemon(host=host, port=self.port)+ self.uri = daemon.register(self, objectId=self.service_name)+ logger.info(f"[{self.service_name}] Connected to Pyro; URI = {self.uri}")daemon.requestLoop()def __start_async_loop(self):@@ -122,16 +113,19 @@ class AppService(AppProcess):AS = TypeVar("AS", bound=AppService)-def get_service_client(service_type: Type[AS]) -> AS:+def get_service_client(service_type: Type[AS], port: int) -> AS:service_name = service_type.service_nameclass DynamicClient:@conn_retrydef __init__(self):- ns = pyro.locate_ns()- uri = ns.lookup(service_name)- self.proxy = pyro.Proxy(uri)+ host = os.environ.get(f"{service_name.upper()}_HOST", "localhost")+ uri = f"PYRO:{service_type.service_name}@{host}:{port}"+ logger.debug(f"Connecting to service [{service_name}]. URI = {uri}")+ self.proxy = Pyro5.api.Proxy(uri)+ # Attempt to bind to ensure the connection is establishedself.proxy._pyroBind()+ logger.debug(f"Successfully connected to service [{service_name}]")def __getattr__(self, name: str) -> Callable[..., Any]:return getattr(self.proxy, name)diff --git a/rnd/autogpt_server/autogpt_server/util/settings.py b/rnd/autogpt_server/autogpt_server/util/settings.pyindex 1d117fb514..45f2dd6f47 100644--- a/rnd/autogpt_server/autogpt_server/util/settings.py+++ b/rnd/autogpt_server/autogpt_server/util/settings.py@@ -72,6 +72,21 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):extra="allow",)+ execution_manager_port: int = Field(+ default=8002,+ description="The port for execution manager daemon to run on",+ )++ execution_scheduler_port: int = Field(+ default=8003,+ description="The port for execution scheduler daemon to run on",+ )++ agent_server_port: int = Field(+ default=8004,+ description="The port for agent server daemon to run on",+ )+@classmethoddef settings_customise_sources(cls,diff --git a/rnd/autogpt_server/autogpt_server/util/test.py b/rnd/autogpt_server/autogpt_server/util/test.pyindex 2145a02dd9..41a20e7c08 100644--- a/rnd/autogpt_server/autogpt_server/util/test.py+++ b/rnd/autogpt_server/autogpt_server/util/test.py@@ -8,7 +8,6 @@ from autogpt_server.data.queue import AsyncEventQueuefrom autogpt_server.executor import ExecutionManager, ExecutionSchedulerfrom autogpt_server.server import AgentServerfrom autogpt_server.server.rest_api import get_user_id-from autogpt_server.util.service import PyroNameServerlog = print@@ -48,7 +47,6 @@ class InMemoryAsyncEventQueue(AsyncEventQueue):class SpinTestServer:def __init__(self):- self.name_server = PyroNameServer()self.exec_manager = ExecutionManager()self.in_memory_queue = InMemoryAsyncEventQueue()self.agent_server = AgentServer(event_queue=self.in_memory_queue)@@ -59,7 +57,6 @@ class SpinTestServer:return "3e53486c-cf57-477e-ba2a-cb02dc828e1a"async def __aenter__(self):- self.name_server.__enter__()self.setup_dependency_overrides()self.agent_server.__enter__()self.exec_manager.__enter__()@@ -76,7 +73,6 @@ class SpinTestServer:self.scheduler.__exit__(exc_type, exc_val, exc_tb)self.exec_manager.__exit__(exc_type, exc_val, exc_tb)self.agent_server.__exit__(exc_type, exc_val, exc_tb)- self.name_server.__exit__(exc_type, exc_val, exc_tb)def setup_dependency_overrides(self):# Override get_user_id for testingdiff --git a/rnd/autogpt_server/pyproject.toml b/rnd/autogpt_server/pyproject.tomlindex 0922154c7c..fe2b62554b 100644--- a/rnd/autogpt_server/pyproject.toml+++ b/rnd/autogpt_server/pyproject.toml@@ -66,6 +66,7 @@ build-backend = "poetry.core.masonry.api"app = "autogpt_server.app:main"rest = "autogpt_server.rest:main"ws = "autogpt_server.ws:main"+executor = "autogpt_server.exec:main"cli = "autogpt_server.cli:main"format = "linter:format"lint = "linter:lint"diff --git a/rnd/autogpt_server/test/executor/test_scheduler.py b/rnd/autogpt_server/test/executor/test_scheduler.pyindex e37e0e1738..065800fcd3 100644--- a/rnd/autogpt_server/test/executor/test_scheduler.py+++ b/rnd/autogpt_server/test/executor/test_scheduler.py@@ -4,6 +4,7 @@ from autogpt_server.data import db, graphfrom autogpt_server.executor import ExecutionSchedulerfrom autogpt_server.usecases.sample import create_test_graph, create_test_userfrom autogpt_server.util.service import get_service_client+from autogpt_server.util.settings import Configfrom autogpt_server.util.test import SpinTestServer@@ -13,7 +14,9 @@ async def test_agent_schedule(server: SpinTestServer):test_user = await create_test_user()test_graph = await graph.create_graph(create_test_graph(), user_id=test_user.id)- scheduler = get_service_client(ExecutionScheduler)+ scheduler = get_service_client(+ ExecutionScheduler, Config().execution_scheduler_port+ )schedules = scheduler.get_execution_schedules(test_graph.id, test_user.id)assert len(schedules) == 0diff --git a/rnd/autogpt_server/test/util/test_service.py b/rnd/autogpt_server/test/util/test_service.pyindex a75ef5e0dc..5a7db88256 100644--- a/rnd/autogpt_server/test/util/test_service.py+++ b/rnd/autogpt_server/test/util/test_service.py@@ -5,6 +5,7 @@ from autogpt_server.util.service import AppService, expose, get_service_clientclass TestService(AppService):def __init__(self):+ super().__init__(port=8005)self.use_redis = Falsedef run_service(self):@@ -29,7 +30,7 @@ class TestService(AppService):@pytest.mark.asyncio(scope="session")async def test_service_creation(server):with TestService():- client = get_service_client(TestService)+ client = get_service_client(TestService, 8005)assert client.add(5, 3) == 8assert client.subtract(10, 4) == 6assert client.fun_with_async(5, 3) == 8diff --git a/rnd/docker-compose.yml b/rnd/docker-compose.ymlindex c53d771597..72c94665f5 100644--- a/rnd/docker-compose.yml+++ b/rnd/docker-compose.yml@@ -28,6 +28,7 @@ services:build:context: ../dockerfile: rnd/autogpt_server/Dockerfile+ target: serverdevelop:watch:- path: ./@@ -44,11 +45,42 @@ services:- REDIS_PORT=6379- REDIS_PASSWORD=password- AUTH_ENABLED=false+ - PYRO_HOST=0.0.0.0+ - EXECUTIONMANAGER_HOST=executor+ - EXECUTIONSCHEDULER_HOST=execution_schedulerports:- "8000:8000"networks:- app-network+ executor:+ build:+ context: ../+ dockerfile: rnd/autogpt_server/Dockerfile+ target: executor+ develop:+ watch:+ - path: ./+ target: rnd/autogpt_server/+ action: rebuild+ depends_on:+ redis:+ condition: service_started+ postgres:+ condition: service_healthy+ environment:+ - DATABASE_URL=postgresql://agpt_user:pass123@postgres:5432/agpt_local+ - REDIS_HOST=redis+ - REDIS_PORT=6379+ - REDIS_PASSWORD=password+ - AUTH_ENABLED=false+ - PYRO_HOST=0.0.0.0+ - AGENTSERVER_HOST=rest_server+ ports:+ - "8002:8002"+ networks:+ - app-network+ws_server:build:context: ../@@ -67,6 +99,7 @@ services:- REDIS_PORT=6379- REDIS_PASSWORD=password- AUTH_ENABLED=false+ - PYRO_HOST=0.0.0.0ports:- "8001:8001"networks: