Skip to content

Commit

Permalink
Merge pull request #59 from rwb27/persist-settings-to-disk
Browse files Browse the repository at this point in the history
Attempt to save settings on exit for all Things
  • Loading branch information
rwb27 authored Dec 13, 2023
2 parents e1b6ebf + aee61db commit 3e3ce52
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/labthings_fastapi/thing_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import logging
from typing import Optional, Sequence, TypeVar
import os.path
from fastapi import FastAPI, Request
Expand Down Expand Up @@ -113,9 +114,21 @@ async def lifespan(self, app: FastAPI):
for thing in self.things.values():
await stack.enter_async_context(thing)
yield
for thing in self.things.values():
for name, thing in self.things.items():
# Remove the blocking portal - the event loop is about to stop.
thing._labthings_blocking_portal = None
try:
if thing._labthings_thing_settings:
thing._labthings_thing_settings.write_to_file()
except PermissionError:
logging.warning(
f"Could not write {name} settings to disk: permission error."
)
except FileNotFoundError:
logging.warning(
f"Could not write {name} settings, folder not found"
)

self.blocking_portal = None

def add_things_view_to_app(self):
Expand Down

0 comments on commit 3e3ce52

Please sign in to comment.