database updater

This commit is contained in:
Broque Thomas 2025-08-24 13:11:54 -07:00
parent 2f535d8222
commit cd9a466e9e
2 changed files with 34 additions and 57 deletions

View file

@ -2243,57 +2243,29 @@ def get_version_info():
return jsonify(version_data) return jsonify(version_data)
def start_simple_background_monitor(): def _simple_monitor_task():
"""Simple background thread that calls the existing status endpoint logic.""" """The actual monitoring task that runs in the background thread."""
import time print("🔄 Simple background monitor started")
import threading while True:
try:
def simple_monitor(): with matched_context_lock:
print("🔄 Simple background monitor started") pending_count = len(matched_downloads_context)
while True: if pending_count > 0:
try: # Use app_context to safely call endpoint logic from a thread
# Check for pending matched downloads with app.app_context():
with matched_context_lock: get_download_status()
pending_count = len(matched_downloads_context)
pending_keys = list(matched_downloads_context.keys()) time.sleep(1)
except Exception as e:
# Only process and log if there are pending downloads print(f"❌ Simple monitor error: {e}")
if pending_count > 0: time.sleep(10)
print(f"🎯 Monitor: {pending_count} pending downloads: {[key.split('::')[-1] for key in pending_keys[:3]]}{'...' if len(pending_keys) > 3 else ''}")
# Just call the existing status endpoint logic directly
with app.app_context():
try:
# Import what we need
from flask import current_app
with current_app.test_request_context():
# Call the existing get_download_status function directly
result = get_download_status()
# Check how many are left after processing
with matched_context_lock:
remaining_count = len(matched_downloads_context)
if remaining_count < pending_count:
processed_count = pending_count - remaining_count
print(f"✅ Status check processed {processed_count} downloads, {remaining_count} remaining")
else:
print(f"⏳ Status check completed, {remaining_count} still pending")
except Exception as status_error:
print(f"❌ Status check error: {status_error}")
import traceback
traceback.print_exc()
time.sleep(1) # Check every 1 second for maximum responsiveness
except Exception as e:
print(f"❌ Simple monitor error: {e}")
import traceback
traceback.print_exc()
time.sleep(10)
def start_simple_background_monitor():
"""Starts the simple background monitor thread."""
monitor_thread = threading.Thread(target=_simple_monitor_task)
monitor_thread.daemon = True
monitor_thread.start()
# =============================== # ===============================
# == DATABASE UPDATER API == # == DATABASE UPDATER API ==
@ -2408,9 +2380,7 @@ def stop_database_update():
monitor_thread = threading.Thread(target=simple_monitor)
monitor_thread.daemon = True
monitor_thread.start()
# --- Main Execution --- # --- Main Execution ---

View file

@ -3367,6 +3367,8 @@ function updateDbProgressUI(state) {
// --- Event Handlers --- // --- Event Handlers ---
// --- Find and REPLACE the existing handleDbUpdateButtonClick function ---
async function handleDbUpdateButtonClick() { async function handleDbUpdateButtonClick() {
const button = document.getElementById('db-update-button'); const button = document.getElementById('db-update-button');
const currentAction = button.textContent; const currentAction = button.textContent;
@ -3376,11 +3378,14 @@ async function handleDbUpdateButtonClick() {
const isFullRefresh = refreshSelect.value === 'full'; const isFullRefresh = refreshSelect.value === 'full';
if (isFullRefresh) { if (isFullRefresh) {
// Replicates the QMessageBox confirmation from the GUI
const confirmed = confirm("⚠️ Full Refresh Warning!\n\nThis will clear and rebuild the database for the active server. It can take a long time. Are you sure you want to proceed?"); const confirmed = confirm("⚠️ Full Refresh Warning!\n\nThis will clear and rebuild the database for the active server. It can take a long time. Are you sure you want to proceed?");
if (!confirmed) return; if (!confirmed) return;
} }
try { try {
button.disabled = true;
button.textContent = 'Starting...';
const response = await fetch('/api/database/update', { const response = await fetch('/api/database/update', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
@ -3389,15 +3394,18 @@ async function handleDbUpdateButtonClick() {
if (response.ok) { if (response.ok) {
showToast('Database update started!', 'success'); showToast('Database update started!', 'success');
// Start polling immediately // Start polling immediately to get live status
stopDbUpdatePolling(); checkAndUpdateDbProgress();
dbUpdateStatusInterval = setInterval(checkAndUpdateDbProgress, 1000);
} else { } else {
const errorData = await response.json(); const errorData = await response.json();
showToast(`Error: ${errorData.error}`, 'error'); showToast(`Error: ${errorData.error}`, 'error');
button.disabled = false;
button.textContent = 'Update Database';
} }
} catch (error) { } catch (error) {
showToast('Failed to start update process.', 'error'); showToast('Failed to start update process.', 'error');
button.disabled = false;
button.textContent = 'Update Database';
} }
} else { // "Stop Update" } else { // "Stop Update"
@ -3413,4 +3421,3 @@ async function handleDbUpdateButtonClick() {
} }
} }
} }