3
3
import os
4
4
import sqlite3
5
5
from enum import Enum
6
+ from typing import Literal
6
7
7
8
from devservices .constants import DEVSERVICES_LOCAL_DIR
8
9
from devservices .constants import STATE_DB_FILE
9
10
10
11
12
+ class ServiceRuntime (Enum ):
13
+ LOCAL = "local"
14
+ CONTAINERIZED = "containerized"
15
+
16
+
11
17
class StateTables (Enum ):
12
18
STARTED_SERVICES = "started_services"
13
19
STARTING_SERVICES = "starting_services"
20
+ SERVICE_RUNTIME = "service_runtime"
14
21
15
22
16
23
class State :
@@ -30,7 +37,7 @@ def __new__(cls) -> State:
30
37
31
38
def initialize_database (self ) -> None :
32
39
cursor = self .conn .cursor ()
33
- # Formatted strings here and throughout the fileshould be extremely low risk given these are constants
40
+ # Formatted strings here and throughout the file should be extremely low risk given these are constants
34
41
cursor .execute (
35
42
f"""
36
43
CREATE TABLE IF NOT EXISTS { StateTables .STARTED_SERVICES .value } (
@@ -50,10 +57,26 @@ def initialize_database(self) -> None:
50
57
)
51
58
"""
52
59
)
60
+
61
+ cursor .execute (
62
+ f"""
63
+ CREATE TABLE IF NOT EXISTS { StateTables .SERVICE_RUNTIME .value } (
64
+ service_name TEXT PRIMARY KEY,
65
+ runtime TEXT
66
+ timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
67
+ )
68
+ """
69
+ )
53
70
self .conn .commit ()
54
71
55
72
def update_service_entry (
56
- self , service_name : str , mode : str , table : StateTables
73
+ self ,
74
+ service_name : str ,
75
+ mode : str ,
76
+ table : (
77
+ Literal [StateTables .STARTED_SERVICES ]
78
+ | Literal [StateTables .STARTING_SERVICES ]
79
+ ),
57
80
) -> None :
58
81
cursor = self .conn .cursor ()
59
82
service_entries = self .get_service_entries (table )
@@ -96,7 +119,12 @@ def get_service_entries(self, table: StateTables) -> list[str]:
96
119
return [row [0 ] for row in cursor .fetchall ()]
97
120
98
121
def get_active_modes_for_service (
99
- self , service_name : str , table : StateTables
122
+ self ,
123
+ service_name : str ,
124
+ table : (
125
+ Literal [StateTables .STARTED_SERVICES ]
126
+ | Literal [StateTables .STARTING_SERVICES ]
127
+ ),
100
128
) -> list [str ]:
101
129
cursor = self .conn .cursor ()
102
130
cursor .execute (
@@ -110,6 +138,41 @@ def get_active_modes_for_service(
110
138
return []
111
139
return str (result [0 ]).split ("," )
112
140
141
+ def get_service_runtime (self , service_name : str ) -> ServiceRuntime :
142
+ cursor = self .conn .cursor ()
143
+ cursor .execute (
144
+ f"""
145
+ SELECT runtime FROM { StateTables .SERVICE_RUNTIME .value } WHERE service_name = ?
146
+ """ ,
147
+ (service_name ,),
148
+ )
149
+ result = cursor .fetchone ()
150
+ if result is None :
151
+ return ServiceRuntime .CONTAINERIZED
152
+ return ServiceRuntime (result [0 ])
153
+
154
+ def update_service_runtime (
155
+ self , service_name : str , runtime : ServiceRuntime
156
+ ) -> None :
157
+ cursor = self .conn .cursor ()
158
+ cursor .execute (
159
+ f"""
160
+ INSERT OR REPLACE INTO { StateTables .SERVICE_RUNTIME .value } (service_name, runtime) VALUES (?, ?)
161
+ """ ,
162
+ (service_name , runtime .value ),
163
+ )
164
+ self .conn .commit ()
165
+
166
+ def get_services_by_runtime (self , runtime : ServiceRuntime ) -> list [str ]:
167
+ cursor = self .conn .cursor ()
168
+ cursor .execute (
169
+ f"""
170
+ SELECT service_name FROM { StateTables .SERVICE_RUNTIME .value } WHERE runtime = ?
171
+ """ ,
172
+ (runtime .value ,),
173
+ )
174
+ return [row [0 ] for row in cursor .fetchall ()]
175
+
113
176
def clear_state (self ) -> None :
114
177
cursor = self .conn .cursor ()
115
178
cursor .execute (
@@ -122,4 +185,9 @@ def clear_state(self) -> None:
122
185
DELETE FROM { StateTables .STARTING_SERVICES .value }
123
186
"""
124
187
)
188
+ cursor .execute (
189
+ f"""
190
+ DELETE FROM { StateTables .SERVICE_RUNTIME .value }
191
+ """
192
+ )
125
193
self .conn .commit ()
0 commit comments