mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 19:36:20 -05:00
A function can wait for a Flag to be set.
This commit is contained in:
parent
c01d62cd28
commit
655e944960
3 changed files with 50 additions and 8 deletions
18
main.py
18
main.py
|
|
@ -160,7 +160,7 @@ def main():
|
|||
@api_app.before_server_start
|
||||
@limit_concurrent(1)
|
||||
async def startup(app: Sanic):
|
||||
# Check database status first. Many function will reference flags.db_up.
|
||||
# Check database status first. Many functions will reference flags.db_up.
|
||||
flags.check_db_is_up()
|
||||
|
||||
flags.init_flags()
|
||||
|
|
@ -183,9 +183,8 @@ async def periodic_downloads(app: Sanic):
|
|||
|
||||
Limited to only one process.
|
||||
"""
|
||||
if not flags.db_up.is_set():
|
||||
logger.warning(f'Refusing to download when DB is not up.')
|
||||
return
|
||||
async with flags.db_up.wait_for():
|
||||
pass
|
||||
|
||||
if not flags.refresh_complete.is_set():
|
||||
logger.warning('Refusing to download without refresh')
|
||||
|
|
@ -206,8 +205,9 @@ async def periodic_downloads(app: Sanic):
|
|||
download_manager.disable()
|
||||
return
|
||||
|
||||
download_manager.enable()
|
||||
app.add_task(download_manager.perpetual_download())
|
||||
async with flags.wait_for_flag(flags.db_up):
|
||||
download_manager.enable()
|
||||
app.add_task(download_manager.perpetual_download())
|
||||
|
||||
|
||||
@api_app.after_server_start
|
||||
|
|
@ -218,14 +218,16 @@ async def start_workers(app: Sanic):
|
|||
download_manager.stop()
|
||||
return
|
||||
|
||||
download_manager.start_workers()
|
||||
async with flags.wait_for_flag(flags.db_up):
|
||||
download_manager.start_workers()
|
||||
|
||||
|
||||
@api_app.before_server_start
|
||||
@limit_concurrent(1)
|
||||
async def main_import_tags_config(app: Sanic):
|
||||
from wrolpi import tags
|
||||
tags.import_tags_config()
|
||||
async with flags.db_up.wait_for():
|
||||
tags.import_tags_config()
|
||||
|
||||
|
||||
@api_app.after_server_start
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
import asyncio
|
||||
import contextlib
|
||||
import multiprocessing
|
||||
import subprocess
|
||||
import threading
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import Column, Boolean, Integer
|
||||
|
|
@ -95,6 +98,12 @@ class Flag:
|
|||
except Exception as e:
|
||||
logger.critical(f'Unable to save flag! {repr(self)}', exc_info=e)
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def wait_for(self, timeout: int = 0):
|
||||
"""Wait for this Flag to be set."""
|
||||
async with wait_for_flag(self, timeout=timeout):
|
||||
yield
|
||||
|
||||
|
||||
# Set by `check_db_is_up`.
|
||||
db_up = Flag('db_up')
|
||||
|
|
@ -202,3 +211,24 @@ def init_flags():
|
|||
refresh_complete.clear()
|
||||
|
||||
FLAGS_INITIALIZED.set()
|
||||
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def wait_for_flag(flag: Flag, timeout: int = 0):
|
||||
"""Sleeps until the provided Flag is set.
|
||||
|
||||
>>> async with wait_for_flag(db_up):
|
||||
>>> do_db_operation()
|
||||
|
||||
@raise TimeoutError: Raises this when the timeout is reached.
|
||||
"""
|
||||
start = datetime.now()
|
||||
while True:
|
||||
if timeout and (elapsed := (datetime.now() - start).total_seconds()) > timeout:
|
||||
raise TimeoutError(f'Waited too long ({elapsed}s) for {flag} to be set!')
|
||||
|
||||
if flag.is_set():
|
||||
break
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
yield
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import asyncio
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
|
@ -43,3 +45,11 @@ def test_flags_with(flags_lock):
|
|||
with flags.refreshing:
|
||||
with flags.refreshing:
|
||||
raise Exception('We should not get here!')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_flag_wait_for(flags_lock):
|
||||
"""Wait for throws an error when waiting exceeds timeout."""
|
||||
with pytest.raises(TimeoutError):
|
||||
async with flags.refreshing.wait_for(timeout=1):
|
||||
await asyncio.sleep(2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue