Skip to content

Commit c1f7ed3

Browse files
authored
Merge branch 'main' into fix-NFS-get-large-file
2 parents a0e0df2 + d61eec0 commit c1f7ed3

16 files changed

Lines changed: 1533 additions & 1177 deletions

File tree

nxc/database.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import sys
21
import configparser
32
import shutil
4-
from sqlalchemy import create_engine
5-
from sqlite3 import connect
3+
import sys
64
from os import mkdir
75
from os.path import exists
86
from os.path import join as path_join
7+
from pathlib import Path
8+
from sqlite3 import connect
9+
from threading import Lock
10+
11+
from sqlalchemy import create_engine, MetaData
12+
from sqlalchemy.exc import IllegalStateChangeError
13+
from sqlalchemy.orm import sessionmaker, scoped_session
914

1015
from nxc.loaders.protocolloader import ProtocolLoader
16+
from nxc.logger import nxc_logger
1117
from nxc.paths import WORKSPACE_DIR
1218

1319

@@ -103,3 +109,39 @@ def initialize_db():
103109

104110
# Even if the default workspace exists, we still need to check if every protocol has a database (in case of a new protocol)
105111
init_protocol_dbs("default")
112+
113+
114+
class BaseDB:
115+
def __init__(self, db_engine):
116+
self.db_engine = db_engine
117+
self.db_path = self.db_engine.url.database
118+
self.protocol = Path(self.db_path).stem.upper()
119+
self.metadata = MetaData()
120+
self.reflect_tables()
121+
session_factory = sessionmaker(bind=self.db_engine, expire_on_commit=True)
122+
123+
session = scoped_session(session_factory)
124+
self.sess = session()
125+
self.lock = Lock()
126+
127+
def reflect_tables(self):
128+
raise NotImplementedError("Reflect tables not implemented")
129+
130+
def shutdown_db(self):
131+
try:
132+
self.sess.close()
133+
# due to the async nature of nxc, sometimes session state is a bit messy and this will throw:
134+
# Method 'close()' can't be called here; method '_connection_for_bind()' is already in progress and
135+
# this would cause an unexpected state change to <SessionTransactionState.CLOSED: 5>
136+
except IllegalStateChangeError as e:
137+
nxc_logger.debug(f"Error while closing session db object: {e}")
138+
139+
def clear_database(self):
140+
for table in self.metadata.sorted_tables:
141+
self.db_execute(table.delete())
142+
143+
def db_execute(self, *args):
144+
self.lock.acquire()
145+
res = self.sess.execute(*args)
146+
self.lock.release()
147+
return res

0 commit comments

Comments
 (0)