Skip to content

Commit

Permalink
FastAPI startup events
Browse files Browse the repository at this point in the history
This was added a few months ago but seems to have since been clobbered. Testing again.
  • Loading branch information
wtgee committed May 24, 2024
1 parent 9a0dadc commit 76270db
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/panoptes/pocs/utils/service/power.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from enum import auto
from typing import Union

Expand All @@ -20,19 +21,24 @@ class RelayCommand(BaseModel):
command: RelayAction


app = FastAPI()
power_board: PowerBoard
conf = get_config('environment.power', {})
conf: dict


@app.on_event('startup')
async def startup():
@asynccontextmanager
def lifespan(app: FastAPI):
global conf
global power_board
power_board = PowerBoard(**get_config('environment.power', {}))
print(f'Power board setup: {power_board}')
conf = get_config('environment.power', {})
power_board = PowerBoard(**conf)
power_board.logger.info(f'Power board setup: {power_board}')
yield
power_board.logger.info('Shutting down power board')


app = FastAPI()


@app.on_event('startup')
@repeat_every(seconds=conf.get('record_interval', 60), wait_first=True)
def record_readings():
"""Record the current readings in the db."""
Expand Down

0 comments on commit 76270db

Please sign in to comment.