Skip to content

Commit 3ccd599

Browse files
committed
renamed assembly artifact -> build artifact, added a new comp_order decorator to give start and stop orderer more information, comp_order.worker starts worker components before and stops them after all other components, renamed component -> comp_type, removed factory (for now), default is singleton, moved build_app() call to init of server, not run method, added comp_order.worker to base worker class, version bump -> 1.2.0-beta.8
1 parent 366b692 commit 3ccd599

File tree

11 files changed

+77
-35
lines changed

11 files changed

+77
-35
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "koi-net"
7-
version = "1.2.0b7"
7+
version = "1.2.0b8"
88
description = "Implementation of KOI-net protocol in Python"
99
authors = [
1010
{name = "Luke Miller", email = "[email protected]"}

src/koi_net/build/artifact.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import inspect
22
from collections import deque
33
from typing import TYPE_CHECKING, Any
4-
from pydantic import BaseModel
5-
6-
if TYPE_CHECKING:
7-
from .assembler import NodeAssembler
84

95
from .consts import (
6+
COMP_ORDER_OVERRIDE,
107
COMP_TYPE_OVERRIDE,
118
START_FUNC_NAME,
129
START_ORDER_OVERRIDE,
1310
STOP_FUNC_NAME,
1411
STOP_ORDER_OVERRIDE,
12+
CompOrder,
1513
CompType
1614
)
1715

16+
if TYPE_CHECKING:
17+
from .assembler import NodeAssembler
18+
1819

19-
class AssemblyArtifact:
20+
class BuildArtifact:
2021
assembler: "NodeAssembler"
2122
comp_dict: dict[str, Any]
2223
dep_graph: dict[str, list[str]]
@@ -130,10 +131,24 @@ def build_start_order(self):
130131
Checks if components define a start function in init order. Can
131132
be overridden by setting start order override in the `NodeAssembler`.
132133
"""
133-
self.start_order = getattr(self.assembler, START_ORDER_OVERRIDE, None) or [
134-
comp_name for comp_name in self.init_order
135-
if getattr(self.comp_dict[comp_name], START_FUNC_NAME, None)
136-
]
134+
135+
self.start_order = getattr(self.assembler, START_ORDER_OVERRIDE, None)
136+
137+
if self.start_order:
138+
return
139+
140+
workers = []
141+
start_order = []
142+
for comp_name in self.init_order:
143+
comp = self.comp_dict[comp_name]
144+
if getattr(comp, START_FUNC_NAME, None):
145+
if getattr(comp, COMP_ORDER_OVERRIDE, None) == CompOrder.WORKER:
146+
workers.append(comp_name)
147+
else:
148+
start_order.append(comp_name)
149+
150+
# order workers first
151+
self.start_order = workers + start_order
137152

138153
print("\nstart order")
139154
[print(f"{i}: {comp_name}") for i, comp_name in enumerate(self.start_order)]
@@ -144,11 +159,26 @@ def build_stop_order(self):
144159
Checks if components define a stop function in init order. Can
145160
be overridden by setting stop order override in the `NodeAssembler`.
146161
"""
147-
self.stop_order = getattr(self.assembler, STOP_ORDER_OVERRIDE, None) or [
148-
comp_name for comp_name in self.init_order
149-
if getattr(self.comp_dict[comp_name], STOP_FUNC_NAME, None)
150-
]
151-
162+
self.stop_order = getattr(self.assembler, STOP_ORDER_OVERRIDE, None)
163+
164+
if self.stop_order:
165+
return
166+
167+
workers = []
168+
stop_order = []
169+
for comp_name in self.init_order:
170+
comp = self.comp_dict[comp_name]
171+
if getattr(comp, STOP_FUNC_NAME, None):
172+
if getattr(comp, COMP_ORDER_OVERRIDE, None) == CompOrder.WORKER:
173+
workers.append(comp_name)
174+
else:
175+
stop_order.append(comp_name)
176+
177+
# order workers first (last)
178+
self.stop_order = workers + stop_order
179+
# reverse order from start order
180+
self.stop_order.reverse()
181+
152182
print("\nstop order")
153183
[print(f"{i}: {comp_name}") for i, comp_name in enumerate(self.stop_order)]
154184

src/koi_net/build/assembler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
import structlog
55

6-
from .artifact import AssemblyArtifact, CompType
6+
from .artifact import BuildArtifact, CompType
77
from .container import NodeContainer
88

99
log = structlog.stdlib.get_logger()
1010

1111

1212
class NodeAssembler:
13-
_artifact: AssemblyArtifact = None
13+
_artifact: BuildArtifact = None
1414

1515
# optional order overrides:
1616
_start_order: list[str]
@@ -22,15 +22,15 @@ def __new__(cls) -> Self | NodeContainer:
2222

2323
# builds assembly artifact if it doesn't exist
2424
if not cls._artifact:
25-
cls._artifact = AssemblyArtifact(cls)
25+
cls._artifact = BuildArtifact(cls)
2626
cls._artifact.build()
2727

2828
components = cls._build_components(cls._artifact)
2929

3030
return NodeContainer(cls._artifact, **components)
3131

3232
@staticmethod
33-
def _build_components(artifact: AssemblyArtifact):
33+
def _build_components(artifact: BuildArtifact):
3434
"""Returns assembled components as a dict."""
3535

3636
print("\nbuilding components")

src/koi_net/build/comp_order.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from koi_net.build.consts import COMP_ORDER_OVERRIDE, CompOrder
2+
3+
4+
def worker(cls):
5+
setattr(cls, COMP_ORDER_OVERRIDE, CompOrder.WORKER)
6+
return cls

src/koi_net/build/comp_type.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .consts import COMP_TYPE_OVERRIDE, CompType
2+
3+
4+
def object(cls):
5+
"""Sets a component's type to `CompType.OBJECT`."""
6+
setattr(cls, COMP_TYPE_OVERRIDE, CompType.OBJECT)
7+
return cls

src/koi_net/build/component.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/koi_net/build/consts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
START_FUNC_NAME = "start"
55
STOP_FUNC_NAME = "stop"
6+
67
START_ORDER_OVERRIDE = "_start_order"
78
STOP_ORDER_OVERRIDE = "_stop_order"
9+
810
COMP_TYPE_OVERRIDE = "_comp_type"
11+
COMP_ORDER_OVERRIDE = "_comp_order"
912

1013
class CompType(StrEnum):
1114
SINGLETON = "SINGLETON"
12-
FACTORY = "FACTORY"
1315
OBJECT = "OBJECT"
16+
17+
class CompOrder(StrEnum):
18+
WORKER = "WORKER"

src/koi_net/build/container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import structlog
22

33
from ..entrypoints.base import EntryPoint
4-
from .artifact import AssemblyArtifact
4+
from .artifact import BuildArtifact
55
from .consts import START_FUNC_NAME, STOP_FUNC_NAME
66

77
log = structlog.stdlib.get_logger()
88

99

1010
class NodeContainer:
1111
"""Dummy 'shape' for node containers built by assembler."""
12-
_artifact: AssemblyArtifact
12+
_artifact: BuildArtifact
1313

1414
entrypoint: EntryPoint
1515

src/koi_net/config/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from rid_lib.types import KoiNetNode
66
import structlog
77

8-
from ..build import component
8+
from ..build import comp_type
99
from ..protocol.secure import PrivateKey
1010
from ..protocol.node import NodeProfile
1111

@@ -68,7 +68,7 @@ def __getattribute__(self, name):
6868

6969
# marking this component as static, classes are implicitly treated as
7070
# factories, but this needs to be passed as is
71-
@component.object
71+
@comp_type.object
7272
class NodeConfig(BaseModel):
7373
"""Base node config class, intended to be extended."""
7474

src/koi_net/entrypoints/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def __init__(
2828
self.config = config
2929
self.response_handler = response_handler
3030

31+
self.build_app()
32+
3133
def build_endpoints(self, router: APIRouter):
3234
"""Builds endpoints for API router."""
3335
for path, models in API_MODEL_MAP.items():
@@ -75,8 +77,6 @@ def protocol_error_handler(self, request, exc: ProtocolError):
7577
def run(self):
7678
"""Starts FastAPI server and event handler."""
7779

78-
self.build_app()
79-
8080
uvicorn.run(
8181
app=self.app,
8282
host=self.config.server.host,

0 commit comments

Comments
 (0)