29
29
from apiserver .data .api .classifications import insert_classification
30
30
from apiserver .data .source import KeyState
31
31
from apiserver import data
32
- from apiserver .data import Source
32
+ from apiserver .data import Source , get_kv
33
33
from apiserver .data .admin import drop_recreate_database
34
34
35
35
@@ -39,18 +39,9 @@ async def startup(dsrc: Source, config: Config, recreate: bool = False) -> None:
39
39
attempting this, it uses a simple lock mechanism by letting the first process set a value in the KV.
40
40
"""
41
41
42
- try :
43
- # Store startup (tests connection)
44
- await dsrc .store .startup ()
45
- except StoreObjectError as e :
46
- raise AppError (
47
- ErrorKeys .STARTUP ,
48
- "<magenta>Failed to start store! Did you start the databases?</magenta>" ,
49
- "startup_store_failure" ,
50
- ) from e
51
-
52
42
# Checks lock: returns True if it is the first lock since at least 25 seconds (lock expire time)
53
43
is_first_process = await wait_for_lock_is_first (dsrc )
44
+
54
45
logger .debug (f"Unlocked startup, first={ is_first_process } " )
55
46
# Only recreates if it is also the first lock since at least 25 seconds (lock expire time)
56
47
logger .debug (f"Startup with recreate={ recreate and is_first_process } " )
@@ -71,6 +62,17 @@ async def startup(dsrc: Source, config: Config, recreate: bool = False) -> None:
71
62
logger .warning ("Initial population..." )
72
63
await initial_population (dsrc , config )
73
64
65
+ # We do this after to ensure the main database exists
66
+ try :
67
+ # Store startup (tests connection)
68
+ await dsrc .store .startup ()
69
+ except StoreObjectError as e :
70
+ raise AppError (
71
+ ErrorKeys .STARTUP ,
72
+ "<magenta>Failed to start store! Did you start the databases?</magenta>" ,
73
+ "startup_store_failure" ,
74
+ ) from e
75
+
74
76
# Load keys
75
77
logger .debug ("Loading keys." )
76
78
key_state = await load_keys (dsrc , config )
@@ -88,8 +90,8 @@ async def wait_for_lock_is_first(dsrc: Source) -> bool:
88
90
true if it is the first lock since at least 25 seconds (lock expire time)."""
89
91
# We sleep for a shor time to increase the distribution in startup times, hopefully reducing race conditions
90
92
await sleep (random () + 0.1 )
91
- # was_locked = await data.trs.startup.startup_is_locked(dsrc)
92
- was_locked = None
93
+ was_locked = await data .trs .startup .startup_is_locked (dsrc )
94
+
93
95
if was_locked is None :
94
96
lock_msg = "First process."
95
97
return_val = True
0 commit comments