Skip to content

Commit

Permalink
Fixed bugs, refactoring, and adding NodeHandle.land() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
snwagh committed Jun 26, 2024
1 parent 6b1b667 commit 60270aa
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 70 deletions.
82 changes: 12 additions & 70 deletions packages/syft/src/syft/node/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
# relative
from ..abstract_node import NodeSideType
from ..client.client import API_PATH
from ..orchestra import DeploymentType
from ..orchestra import NodeHandle

# from ..orchestra import DeploymentType
# from ..orchestra import NodeHandle
from ..util.constants import DEFAULT_TIMEOUT
from ..util.util import os_name
from .domain import Domain
Expand Down Expand Up @@ -65,9 +66,6 @@ def make_app(name: str, router: APIRouter) -> FastAPI:
}


process_list = []


def create_app(
name: str,
node_type: NodeType,
Expand All @@ -77,15 +75,15 @@ def create_app(
local_db: bool,
) -> FastAPI:
# Print variables for debugging
print("*" * 50)
print("Starting uvicorn app with the following settings:")
print(f"NODE_NAME: {name}")
print(f"PROCESSES: {processes}")
print(f"RESET: {reset}")
print(f"LOCAL_DB: {local_db}")
print(f"NODE_TYPE: {node_type}")
print(f"NODE_SIDE_TYPE: {node_side_type}")
print("*" * 50)
print("*" * 50, flush=True)
print("Starting uvicorn app with the following settings:", flush=True)
print(f"NODE_NAME: {name}", flush=True)
print(f"PROCESSES: {processes}", flush=True)
print(f"RESET: {reset}", flush=True)
print(f"LOCAL_DB: {local_db}", flush=True)
print(f"NODE_TYPE: {node_type}", flush=True)
print(f"NODE_SIDE_TYPE: {node_side_type}", flush=True)
print("*" * 50, flush=True)

worker_type = worker_classes[node_type]
worker = worker_type.named(
Expand Down Expand Up @@ -128,62 +126,6 @@ def app_factory() -> FastAPI:
)


def start_uvicorn_server(
name: str = "testing-node",
node_type: str = "domain",
node_side_type: str = "high",
port: int = 9081,
processes: int = 1,
local_db: bool = True,
reset: bool = False,
) -> NodeHandle:
os.environ["NODE_NAME"] = name
os.environ["NODE_TYPE"] = node_type
os.environ["NODE_SIDE_TYPE"] = node_side_type
os.environ["PORT"] = str(port)
os.environ["PROCESSES"] = str(processes)
os.environ["LOCAL_DB"] = str(local_db)
os.environ["RESET"] = str(reset)

command = ["python", "-m", "syft.node.server"]
process = subprocess.Popen(command)
process_list.append(process)
print(f"Uvicorn server running on port {port} with PID: {process.pid}")

# Return this object:
return NodeHandle(
node_type=NodeType(node_type),
deployment_type=DeploymentType.PYTHON,
name=name,
port=port,
url="http://localhost",
node_side_type=NodeSideType(node_side_type),
# shutdown=stop,
)


def stop_all_uvicorn_servers() -> None:
for process in process_list:
process.terminate()
process.wait()
process_list.clear()
print("All Uvicorn servers stopped.")


if __name__ == "__main__":
current_file_path = os.path.dirname(os.path.abspath(__file__))
reload_dirs = os.path.abspath(os.path.join(current_file_path, "../../"))

uvicorn.run(
"syft.node.server:app_factory",
host="0.0.0.0",
port=int(os.getenv("PORT", 9081)),
reload=True,
factory=True,
reload_dirs=[reload_dirs],
)


def run_uvicorn(
name: str,
node_type: Enum,
Expand Down
90 changes: 90 additions & 0 deletions packages/syft/src/syft/node/server_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# stdlib
import os
import subprocess

# third party
import uvicorn

# relative
from ..abstract_node import NodeSideType

# from .server import app_factory
from ..orchestra import DeploymentType
from ..orchestra import NodeHandle
from .node import NodeType

# List storing all reloadable servers launched
process_list = []

def start_reloadable_server(
name: str = "testing-node",
node_type: str = "domain",
node_side_type: str = "high",
port: int = 9081,
processes: int = 1,
local_db: bool = True,
reset: bool = False,
) -> NodeHandle:
os.environ["NODE_NAME"] = name
os.environ["NODE_TYPE"] = node_type
os.environ["NODE_SIDE_TYPE"] = node_side_type
os.environ["PORT"] = str(port)
os.environ["PROCESSES"] = str(processes)
os.environ["LOCAL_DB"] = str(local_db)
os.environ["RESET"] = str(reset)

command = ["python", "-m", "syft.node.server_management"]
process = subprocess.Popen(command)
process_list.append(process)
print("*" * 50, flush=True)
print(f"Uvicorn server running on port {port} with PID: {process.pid}", flush=True)
print("*" * 50, flush=True)


# Since the servers take a second to run, adding this wait so
# that notebook commands can run one after the other.
# stdlib
from time import sleep
sleep(6)

def stop() -> None:
process.terminate()
process.wait()
if process in process_list:
process_list.remove(process)
print("*" * 50, flush=True)
print(f"Uvicorn server with PID: {process.pid} stopped.", flush=True)
print("*" * 50, flush=True)


# Return this object:
return NodeHandle(
node_type=NodeType(node_type),
deployment_type=DeploymentType.PYTHON,
name=name,
port=port,
url="http://localhost",
node_side_type=NodeSideType(node_side_type),
shutdown=stop,
)

def stop_all_reloadable_servers() -> None:
for process in process_list:
process.terminate()
process.wait()
process_list.clear()
print("All Uvicorn servers stopped.")


if __name__ == "__main__":
current_file_path = os.path.dirname(os.path.abspath(__file__))
reload_dirs = os.path.abspath(os.path.join(current_file_path, "../../"))

uvicorn.run(
"syft.node.server:app_factory",
host="0.0.0.0",
port=int(os.getenv("PORT", 9081)),
reload=True,
factory=True,
reload_dirs=[reload_dirs],
)

0 comments on commit 60270aa

Please sign in to comment.