@@ -28,6 +28,7 @@ class BaseYStore(ABC):
28
28
metadata_callback : Callable [[], Awaitable [bytes ] | bytes ] | None = None
29
29
version = 2
30
30
_started : Event | None = None
31
+ _stopped : Event | None = None
31
32
_task_group : TaskGroup | None = None
32
33
__start_lock : Lock | None = None
33
34
@@ -50,6 +51,12 @@ def started(self) -> Event:
50
51
self ._started = Event ()
51
52
return self ._started
52
53
54
+ @property
55
+ def stopped (self ) -> Event :
56
+ if self ._stopped is None :
57
+ self ._stopped = Event ()
58
+ return self ._stopped
59
+
53
60
@property
54
61
def _start_lock (self ) -> Lock :
55
62
if self .__start_lock is None :
@@ -96,12 +103,14 @@ async def start(
96
103
async with create_task_group () as self ._task_group :
97
104
task_status .started ()
98
105
self .started .set ()
106
+ await self .stopped .wait ()
99
107
100
108
async def stop (self ) -> None :
101
109
"""Stop the store."""
102
110
if self ._task_group is None :
103
111
raise RuntimeError ("YStore not running" )
104
112
113
+ self .stopped .set ()
105
114
self ._task_group .cancel_scope .cancel ()
106
115
self ._task_group = None
107
116
@@ -309,7 +318,7 @@ class MySQLiteYStore(SQLiteYStore):
309
318
document_ttl : int | None = None
310
319
path : str
311
320
lock : Lock
312
- db_initialized : Event
321
+ db_initialized : Event | None
313
322
_db : Connection
314
323
315
324
def __init__ (
@@ -329,6 +338,7 @@ def __init__(
329
338
self .metadata_callback = metadata_callback
330
339
self .log = log or getLogger (__name__ )
331
340
self .lock = Lock ()
341
+ self .db_initialized = None
332
342
333
343
async def start (
334
344
self ,
@@ -356,10 +366,11 @@ async def start(
356
366
self ._task_group .start_soon (self ._init_db )
357
367
task_status .started ()
358
368
self .started .set ()
369
+ await self .stopped .wait ()
359
370
360
371
async def stop (self ) -> None :
361
372
"""Stop the store."""
362
- if hasattr ( self , " db_initialized" ) and self .db_initialized .is_set ():
373
+ if self . db_initialized is not None and self .db_initialized .is_set ():
363
374
await self ._db .close ()
364
375
await super ().stop ()
365
376
@@ -405,6 +416,7 @@ async def _init_db(self):
405
416
await db .commit ()
406
417
await db .close ()
407
418
self ._db = await connect (self .db_path )
419
+ assert self .db_initialized is not None
408
420
self .db_initialized .set ()
409
421
410
422
async def read (self ) -> AsyncIterator [tuple [bytes , bytes , float ]]:
@@ -413,8 +425,8 @@ async def read(self) -> AsyncIterator[tuple[bytes, bytes, float]]:
413
425
Returns:
414
426
A tuple of (update, metadata, timestamp) for each update.
415
427
"""
416
- if not hasattr ( self , " db_initialized" ) :
417
- raise RuntimeError ("ystore is not started" )
428
+ if self . db_initialized is None :
429
+ raise RuntimeError ("YStore not started" )
418
430
await self .db_initialized .wait ()
419
431
try :
420
432
async with self .lock :
@@ -438,8 +450,8 @@ async def write(self, data: bytes) -> None:
438
450
Arguments:
439
451
data: The update to store.
440
452
"""
441
- if not hasattr ( self , " db_initialized" ) :
442
- raise RuntimeError ("ystore is not started" )
453
+ if self . db_initialized is None :
454
+ raise RuntimeError ("YStore not started" )
443
455
await self .db_initialized .wait ()
444
456
async with self .lock :
445
457
# first, determine time elapsed since last update
0 commit comments