Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc #1179

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

poc #1179

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions truss/templates/server/model_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import aiofiles
import dataclasses
import enum
import importlib
Expand Down Expand Up @@ -191,6 +192,7 @@ class ModelDescriptor:
predict: MethodDescriptor
postprocess: Optional[MethodDescriptor]
truss_schema: Optional[TrussSchema]
setup_environment: Optional[MethodDescriptor]

@cached_property
def skip_input_parsing(self) -> bool:
Expand Down Expand Up @@ -250,7 +252,28 @@ def from_model(cls, model) -> "ModelDescriptor":
truss_schema=TrussSchema.from_signature(parameters, return_annotation),
)

import asyncio
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class AsyncFileSystemEventHandler(FileSystemEventHandler):
"""
An event handler that triggers asynchronous callbacks using asyncio for
file system events.
"""
def __init__(self, callback):
self.callback = callback

def on_modified(self, event):
if not event.is_directory:
asyncio.run(self.callback(event.src_path))

def on_created(self, event):
if not event.is_directory:
asyncio.run(self.callback(event.src_path))


FILEPATH = '/etc/b10_dynamic_config/environment'
class ModelWrapper:
_config: Dict
_tracer: sdk_trace.Tracer
Expand Down Expand Up @@ -280,6 +303,7 @@ def __init__(self, config: Dict, tracer: sdk_trace.Tracer):
"predict_concurrency", DEFAULT_PREDICT_CONCURRENCY
)
)
self.observer: Optional[Observer] = None

@property
def _model(self) -> Any:
Expand Down Expand Up @@ -313,6 +337,24 @@ def start_load_thread(self):
thread = Thread(target=self.load)
thread.start()


async def _handle_environment_change(self):
async with aiofiles.open(FILEPATH, 'r') as f:
data = await f.read()
await self._setup_environment_impl(data)


def setup_watcher(self):
event_handler = AsyncFileSystemEventHandler(self._handle_environment_change)
observer = Observer()
observer.schedule(event_handler, path=FILEPATH, recursive=False)
observer.start()
self.observer = observer

def on_close(self):
if self.observer:
self.observer.stop()

def load(self) -> bool:
if self.ready:
return True
Expand Down Expand Up @@ -428,6 +470,15 @@ def _load_impl(self):
gap_seconds=1.0,
)

async def _setup_environment_impl(
self,
data: dict,
):
descriptor = self.model_descriptor.setup_environment
if not descriptor:
return
...

async def preprocess(
self,
inputs: serialization.InputType,
Expand Down
4 changes: 4 additions & 0 deletions truss/templates/server/truss_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def on_startup(self):
if self._setup_json_logger:
setup_logging()
self._model.start_load_thread()
self._model.setup_watcher()

def on_close(self):
self._model.on_close()

def create_application(self):
app = FastAPI(
Expand Down
Loading