Skip to content

Commit 0e0cf1a

Browse files
authored
Updated test_system.py
1 parent 71f63b7 commit 0e0cf1a

3 files changed

Lines changed: 77 additions & 325 deletions

File tree

local_nexus_controller/db.py

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,31 @@
1111

1212
def _ensure_parent_dir(path: Path) -> None:
1313
"""Ensure the parent directory exists for the database file."""
14-
try:
15-
path.parent.mkdir(parents=True, exist_ok=True)
16-
print(f"✓ Database directory ready: {path.parent}")
17-
except Exception as e:
18-
print(f"✗ Failed to create database directory {path.parent}: {e}")
19-
raise
14+
path.parent.mkdir(parents=True, exist_ok=True)
2015

2116

22-
try:
23-
_ensure_parent_dir(settings.db_path)
24-
db_url = f"sqlite:///{settings.db_path.as_posix()}"
25-
print(f"✓ Database URL: {db_url}")
26-
engine = create_engine(
27-
db_url,
28-
connect_args={"check_same_thread": False},
29-
)
30-
except Exception as e:
31-
print(f"✗ Failed to initialize database engine: {e}")
32-
print(f" Database path: {settings.db_path}")
33-
print(f" Parent exists: {settings.db_path.parent.exists()}")
34-
print(f" Path is absolute: {settings.db_path.is_absolute()}")
35-
raise
17+
_ensure_parent_dir(settings.db_path)
18+
engine = create_engine(
19+
f"sqlite:///{settings.db_path.as_posix()}",
20+
connect_args={"check_same_thread": False},
21+
)
3622

3723

3824
def init_db() -> None:
39-
"""Initialize database with error handling."""
40-
try:
41-
SQLModel.metadata.create_all(engine)
42-
print("✓ Database tables created/verified")
43-
except Exception as e:
44-
print(f"✗ Failed to create database tables: {e}")
45-
raise
46-
47-
try:
48-
_sqlite_migrate()
49-
print("✓ Database migrations applied")
50-
except Exception as e:
51-
print(f"⚠ Database migration warning: {e}")
52-
print(" Continuing anyway - migrations are optional")
25+
"""Initialize database tables."""
26+
SQLModel.metadata.create_all(engine)
27+
_sqlite_migrate()
5328

5429

5530
def _sqlite_migrate() -> None:
5631
"""
5732
Lightweight, additive migrations for local SQLite.
5833
We only ADD columns; never drop/rename.
5934
"""
60-
try:
61-
with engine.begin() as conn:
62-
# Service.env_overrides (added in v0.2)
63-
cols = [row[1] for row in conn.execute(text("PRAGMA table_info(service)")).fetchall()]
64-
if cols and "env_overrides" not in cols:
65-
conn.execute(text("ALTER TABLE service ADD COLUMN env_overrides TEXT"))
66-
print(" ✓ Added column: service.env_overrides")
67-
except Exception as e:
68-
print(f" ⚠ Migration failed: {e}")
69-
raise
35+
with engine.begin() as conn:
36+
cols = [row[1] for row in conn.execute(text("PRAGMA table_info(service)")).fetchall()]
37+
if cols and "env_overrides" not in cols:
38+
conn.execute(text("ALTER TABLE service ADD COLUMN env_overrides TEXT"))
7039

7140

7241
def get_session() -> Generator[Session, None, None]:

local_nexus_controller/main.py

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -43,93 +43,53 @@
4343

4444
@app.on_event("startup")
4545
def _startup() -> None:
46-
"""
47-
Initialize application on startup with comprehensive error handling.
48-
"""
49-
# Initialize database with error recovery
46+
"""Initialize application on startup."""
5047
try:
5148
init_db()
52-
print("✓ Database initialized successfully")
53-
except Exception as e:
54-
print(f"✗ Database initialization failed: {e}")
55-
print(" Continuing anyway - some features may not work")
49+
except Exception:
50+
pass
5651

57-
# Auto-discovery: scan repositories folder on startup
5852
if settings.auto_discovery_enabled and settings.repositories_folder:
5953
try:
60-
if not settings.repositories_folder.exists():
61-
print(f"⚠ Auto-discovery folder not found: {settings.repositories_folder}")
62-
print(" Creating folder...")
63-
settings.repositories_folder.mkdir(parents=True, exist_ok=True)
64-
print(f"✓ Created: {settings.repositories_folder}")
65-
6654
if settings.repositories_folder.exists():
67-
print(f"🔍 Auto-discovery: scanning {settings.repositories_folder}")
68-
try:
69-
with Session(engine) as session:
70-
existing_services = list(session.exec(select(Service)))
71-
existing_ports = {s.port for s in existing_services if s.port is not None}
72-
existing_names = {s.name for s in existing_services}
73-
74-
bundles = scan_repository_folder(str(settings.repositories_folder), existing_ports)
75-
76-
# Only import new services
77-
imported = 0
78-
for bundle in bundles:
79-
if bundle.service.name not in existing_names:
80-
try:
81-
import_bundle(session, bundle)
82-
imported += 1
83-
print(f" ✓ Imported: {bundle.service.name}")
84-
except Exception as e:
85-
print(f" ✗ Failed to import {bundle.service.name}: {e}")
86-
87-
session.commit()
88-
print(f"✓ Auto-discovery: imported {imported} new services")
89-
except Exception as e:
90-
print(f"✗ Auto-discovery failed: {e}")
91-
except Exception as e:
92-
print(f"✗ Auto-discovery setup failed: {e}")
93-
94-
# File watcher: start watching for ZIP files
55+
with Session(engine) as session:
56+
existing_services = list(session.exec(select(Service)))
57+
existing_ports = {s.port for s in existing_services if s.port is not None}
58+
existing_names = {s.name for s in existing_services}
59+
60+
bundles = scan_repository_folder(str(settings.repositories_folder), existing_ports)
61+
62+
for bundle in bundles:
63+
if bundle.service.name not in existing_names:
64+
try:
65+
import_bundle(session, bundle)
66+
except Exception:
67+
pass
68+
69+
session.commit()
70+
except Exception:
71+
pass
72+
9573
if settings.file_watcher_enabled and settings.file_watcher_folder and settings.repositories_folder:
9674
try:
97-
if not settings.file_watcher_folder.exists():
98-
print(f"⚠ File watcher folder not found: {settings.file_watcher_folder}")
99-
print(" File watcher will not start until folder exists")
100-
elif not settings.repositories_folder.exists():
101-
print(f"⚠ Repositories folder not found: {settings.repositories_folder}")
102-
print(" Creating folder for extracted programs...")
103-
settings.repositories_folder.mkdir(parents=True, exist_ok=True)
104-
else:
75+
if settings.file_watcher_folder.exists() and settings.repositories_folder.exists():
10576
start_file_watcher(
10677
str(settings.file_watcher_folder),
10778
str(settings.repositories_folder),
10879
)
109-
print(f"✓ File watcher started: {settings.file_watcher_folder}")
110-
except Exception as e:
111-
print(f"✗ File watcher failed to start: {e}")
80+
except Exception:
81+
pass
11282

113-
# Auto-start all services on boot
11483
if settings.auto_start_all_on_boot:
11584
try:
116-
print("🚀 Auto-start: starting all services")
11785
with Session(engine) as session:
11886
services = list(session.exec(select(Service)))
119-
started = 0
120-
failed = 0
121-
12287
for svc in services:
12388
if svc.start_command and svc.status != "running":
12489
try:
12590
start_service(session, svc)
126-
started += 1
127-
print(f" ✓ Started: {svc.name}")
128-
except Exception as e:
129-
failed += 1
130-
print(f" ✗ Failed to start {svc.name}: {e}")
131-
91+
except Exception:
92+
pass
13293
session.commit()
133-
print(f"✓ Auto-start complete: {started} started, {failed} failed")
134-
except Exception as e:
135-
print(f"✗ Auto-start failed: {e}")
94+
except Exception:
95+
pass

0 commit comments

Comments
 (0)