Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions omniscan_pkg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def main():
for library_id, folder_path in sorted_folders:
scanner.trigger_scan(library_id, folder_path)

asyncio.run(stats.send_discord_summary())
scanner._run_async(stats.send_discord_summary())

else:
logger.error(f"Path not found: {path}")
Expand All @@ -166,7 +166,7 @@ def main():
# Default: Scheduled Mode
logger.info(f"Will run every {BOLD}{config['RUN_INTERVAL']}{RESET} hours")

if config['RUN_ON_STARTUP']:
if config.get('RUN_ON_STARTUP'):
scanner.run_scan()

if config['START_TIME']:
Expand All @@ -181,9 +181,26 @@ def main():
else:
schedule.every(config['RUN_INTERVAL']).hours.do(scanner.run_scan)

while True:
# Graceful Shutdown Handling
import signal
stop_event = threading.Event()

def signal_handler(signum, frame):
logger.info(f"🛑 Received signal {signum}, stopping...")
stop_event.set()
# Attempt to stop watcher if running
# (Watcher runs in main thread if enabled, but here we are in main thread too?)
# Actually start_watcher blocks if watch mode is on.
# But if we are in schedule mode, we are in the loop below.

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

while not stop_event.is_set():
schedule.run_pending()
time.sleep(60)
time.sleep(1)

logger.info("👋 Omniscan shutdown complete.")

if __name__ == '__main__':
main()
12 changes: 10 additions & 2 deletions omniscan_pkg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def __init__(self, db_file='history.db'):
self._init_db()

def _init_db(self):
self.prune_counter = 0
with self.lock:
try:
conn = sqlite3.connect(self.db_file)
# Enable WAL mode for better concurrency
conn.execute('PRAGMA journal_mode=WAL;')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS stuck_files (
Expand Down Expand Up @@ -51,8 +54,13 @@ def add_event(self, event_type, details, status):
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute('INSERT INTO events (timestamp, event_type, details, status) VALUES (?, ?, ?, ?)', (timestamp, event_type, details, status))
# Prune old events (keep last 20000)
cursor.execute('DELETE FROM events WHERE id NOT IN (SELECT id FROM events ORDER BY id DESC LIMIT 20000)')

# Prune old events periodically (every 100 inserts) to reduce overhead
self.prune_counter += 1
if self.prune_counter >= 100:
cursor.execute('DELETE FROM events WHERE id NOT IN (SELECT id FROM events ORDER BY id DESC LIMIT 20000)')
self.prune_counter = 0

conn.commit()
conn.close()
except Exception as e:
Expand Down
Loading
Loading