-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
349 lines (300 loc) · 13.6 KB
/
main.py
File metadata and controls
349 lines (300 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# main.py
import os
import signal
import atexit
import sys
from pathlib import Path
from config import config
from app import create_app
from db.db_manager import DatabaseManager
from processing.file_watcher import FileWatcher
from processing.pdf_processor import PDFProcessor
from processing.vision_api_client import VisionAPIClient
from processing.ocr_queue_manager import OCRQueueManager
from processing.html_processor import HTMLProcessor
from processing.document_source_manager import DocumentSourceManager
from processing.boox_pdf_source import BooxPDFSource
from processing.saber_note_source import SaberNoteSource
from processing.archiver import DocumentArchiver
# Validate and print configuration
config.print_config()
validation_errors = config.validate()
if validation_errors:
print("\n⚠️ Configuration warnings:")
for error in validation_errors:
print(f" - {error}")
print()
# Ensure required directories exist
config.ensure_directories()
# Create application components using centralized config
db = DatabaseManager(config.DATABASE_PATH)
pdf_processor = PDFProcessor()
html_processor = HTMLProcessor()
# Initialize Vision API client (OpenAI-compatible endpoint)
ocr_processor = VisionAPIClient(
api_base=config.INFERENCE_API_BASE,
api_key=config.INFERENCE_API_KEY,
model=config.INFERENCE_MODEL,
max_tokens=config.INFERENCE_MAX_TOKENS,
timeout=config.INFERENCE_TIMEOUT,
)
# Initialize document archiver
archiver = DocumentArchiver(
archive_root=Path(config.ARCHIVE_FOLDER),
preserve_structure=config.ARCHIVE_PRESERVE_STRUCTURE,
enabled=config.ARCHIVE_ENABLED
)
# Initialize OCR queue with archiver
ocr_queue = OCRQueueManager(db, ocr_processor, archiver=archiver)
# Initialize document source manager
doc_source_manager = DocumentSourceManager(polling_interval=config.FILE_WATCHER_POLLING_INTERVAL)
# Register Boox PDF source if enabled
if config.BOOX_ENABLED:
print(f"Initializing Boox PDF source: {config.BOOX_FOLDER}")
boox_source = BooxPDFSource(
watch_directory=Path(config.BOOX_FOLDER),
enabled=True
)
doc_source_manager.register_source(boox_source)
else:
print("Boox PDF source is disabled")
# Register Saber note source if enabled
if config.SABER_ENABLED:
if not config.SABER_PASSWORD:
print("⚠️ WARNING: Saber is enabled but SABER_PASSWORD is not set. Disabling Saber source.")
else:
print(f"Initializing Saber note source: {config.SABER_FOLDER}")
try:
saber_source = SaberNoteSource(
watch_directory=Path(config.SABER_FOLDER),
encryption_password=config.SABER_PASSWORD,
enabled=True
)
doc_source_manager.register_source(saber_source)
except Exception as e:
print(f"⚠️ WARNING: Failed to initialize Saber source: {e}")
print(" Saber support will be disabled")
import traceback
print(f" Traceback: {traceback.format_exc()}")
else:
print("Saber note source is disabled")
# Keep legacy file_watcher for backward compatibility (used by app/__init__.py)
# This will be removed once app is updated to use doc_source_manager
file_watcher = FileWatcher(config.UPLOAD_FOLDER, polling_interval=config.FILE_WATCHER_POLLING_INTERVAL)
# Define callback for processed documents from document source manager
def process_document_from_source(processed_doc):
"""
Handle a processed document from any document source.
Args:
processed_doc: ProcessedDocument object from a document source
"""
print(f"Processing document from {processed_doc.source_type}: {processed_doc.title}")
# Check if already in database
with db.get_connection() as conn:
cursor = conn.cursor()
# For Saber notes, use encrypted filename as unique identifier
# For PDFs, use the file path
if processed_doc.source_type == 'saber_note':
identifier = processed_doc.encrypted_filename
cursor.execute("""
SELECT id, processing_status FROM pdf_documents
WHERE filename = ? OR relative_path LIKE ?
""", (identifier, f"%{identifier}%"))
else:
cursor.execute("""
SELECT id, processing_status FROM pdf_documents
WHERE relative_path = ? OR filename = ?
""", (processed_doc.original_path, Path(processed_doc.original_path).name))
existing = cursor.fetchone()
# Skip if already completed or explicitly skipped
if existing:
if existing['processing_status'] == 'completed':
print(f"Document already processed: {processed_doc.title}")
return
elif existing['processing_status'] == 'skipped':
print(f"Document is skipped, not processing: {processed_doc.title}")
return
# Add or reset document in database
if existing:
doc_id = existing['id']
print(f"Resetting existing document: {doc_id}")
db.reset_document_status_by_id(doc_id)
else:
# Add to database
# For Saber notes, use the first rendered page image as the "file" for database purposes
# For PDFs, use the original file
if processed_doc.source_type == 'saber_note' and processed_doc.page_images:
primary_path = processed_doc.page_images[0] # Use first rendered image
else:
primary_path = Path(processed_doc.original_path)
doc_id = db.add_document(primary_path)
if not doc_id:
print(f"Failed to add document to database: {processed_doc.title}")
return
# Process based on source type
if processed_doc.source_type == 'saber_note':
# For Saber notes, queue each rendered page image for OCR
# Note: Saber rendered images are temporary, don't archive them
print(f"Queueing {len(processed_doc.page_images)} Saber pages for OCR")
for page_image in processed_doc.page_images:
ocr_queue.add_to_queue(doc_id, page_image, base_watch_dir=Path(config.SABER_FOLDER))
elif processed_doc.source_type == 'boox_pdf':
# For PDFs, use existing PDF processor
filepath = Path(processed_doc.original_path)
pdf_processor.process_document(filepath, doc_id, db)
ocr_queue.add_to_queue(doc_id, filepath, base_watch_dir=Path(config.BOOX_FOLDER))
else:
print(f"Unknown source type: {processed_doc.source_type}")
# Set the callback on the document source manager
doc_source_manager.set_process_callback(process_document_from_source)
# Define callbacks for file watching (legacy - for backward compatibility)
def process_new_file(relative_path):
"""Process a newly detected file."""
filepath = Path(os.path.join(config.UPLOAD_FOLDER, relative_path))
print(f"Processing new file: {filepath}")
# Check if this file is already in the database
with db.get_connection() as conn:
cursor = conn.cursor()
# Try to find by relative path or filename
filename = filepath.name
cursor.execute("""
SELECT id, processing_status FROM pdf_documents
WHERE relative_path = ? OR filename = ?
""", (str(filepath.absolute()), filename))
existing = cursor.fetchone()
# If already in database and completed or skipped, skip processing
if existing:
if existing['processing_status'] == 'completed':
print(f"File already processed: {filepath}")
return
elif existing['processing_status'] == 'skipped':
print(f"File is skipped, not processing: {filepath}")
return
# If in database but not completed or skipped, reset and process
if existing:
doc_id = existing['id']
print(f"Resetting existing document: {doc_id}")
db.reset_document_status_by_id(doc_id)
else:
# Add to database if not found
doc_id = db.add_document(filepath)
if not doc_id:
print(f"Failed to add document to database: {filepath}")
return
# Choose processor based on file extension
file_extension = filepath.suffix.lower()
# Process based on file type
if file_extension in ['.html', '.htm']:
print(f"Processing HTML file: {filepath}")
# Use HTML processor
html_processor.process_document(filepath, doc_id, db)
print(f"HTML processing complete for {filepath}")
else:
# Default PDF processing
print(f"Processing PDF file: {filepath}")
pdf_processor.process_document(filepath, doc_id, db)
# Only queue PDFs for OCR
ocr_queue.add_to_queue(doc_id, filepath, base_watch_dir=Path(config.UPLOAD_FOLDER))
def handle_removed_file(relative_path):
"""Handle a file that's been removed."""
filepath = Path(os.path.join(config.UPLOAD_FOLDER, relative_path))
print(f"Marking removed file: {filepath}")
db.mark_document_removed(filepath)
# File watcher callbacks are now registered in app/__init__.py via _init_file_watcher()
# to avoid duplicate processing
def cleanup():
"""Clean up resources before shutdown."""
print("Shutting down application...")
ocr_queue.stop_processing()
doc_source_manager.stop_all()
file_watcher.stop() # Legacy watcher
db.close()
print("Application shutdown complete.")
def signal_handler(signum, _):
"""Handle shutdown signals gracefully."""
print(f"\nSignal received: {signum}")
cleanup()
sys.exit(0)
# Register cleanup handlers
atexit.register(cleanup)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Create the Flask application
# Note: Still passing legacy file_watcher for backward compatibility
# TODO: Update create_app signature to accept doc_source_manager
app = create_app(db, file_watcher, pdf_processor, ocr_processor, ocr_queue, html_processor)
# Store doc_source_manager on app for access in routes if needed
app.doc_source_manager = doc_source_manager
# Start background services immediately (works for both gunicorn and direct execution)
# These services are thread-based and won't interfere with gunicorn workers
def _startup_initialization():
"""
Perform startup initialization in background thread to avoid blocking gunicorn.
This includes API health check, file watching, and initial document scanning.
"""
print("[STARTUP] ▶ Starting OCR queue processing...")
ocr_queue.start_processing()
print("[STARTUP] ✓ OCR queue ready")
# Check inference API health
print(f"[STARTUP] ▶ Checking inference API at {config.INFERENCE_API_BASE}...")
health = ocr_processor.health_check()
if health.get('healthy'):
models = health.get('models', [])
print(f"[STARTUP] ✓ Inference API is healthy")
if models:
print(f"[STARTUP] Available models: {', '.join(models[:5])}")
else:
error = health.get('error', 'Unknown error')
print(f"[STARTUP] ⚠ WARNING: Inference API not reachable: {error}")
print(f"[STARTUP] OCR processing will fail until the API is available")
print("[STARTUP] ▶ Starting document source watchers...")
# Start the new modular document source manager
doc_source_manager.start_all(trigger_initial_scan=True)
# Also start legacy file watcher for backward compatibility (without initial scan to avoid duplicates)
file_watcher.start(trigger_initial_scan=False)
print("[STARTUP] ✓ File watchers ready")
# Perform initial scan to queue any unprocessed documents from database
print("[STARTUP] ▶ Scanning for unprocessed documents in database...")
with db.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT id, relative_path, processing_status
FROM pdf_documents
WHERE (processing_status = 'pending' OR processing_status IS NULL OR processing_status = 'failed')
AND processing_status != 'skipped'
AND relative_path NOT LIKE '%.html'
AND relative_path NOT LIKE '%.htm'
ORDER BY file_modified_at DESC
""")
unprocessed = cursor.fetchall()
if unprocessed:
print(f"[STARTUP] Found {len(unprocessed)} unprocessed documents, queueing for processing...")
for doc in unprocessed:
doc_id = doc['id']
doc_path = doc['relative_path']
filepath = Path(doc_path) if os.path.isabs(doc_path) else Path(config.UPLOAD_FOLDER) / doc_path
if filepath.exists():
print(f"[STARTUP] Queueing doc {doc_id}: {filepath.name}")
# Process PDF immediately
pdf_processor.process_document(filepath, doc_id, db)
# Queue for OCR (use UPLOAD_FOLDER as base for legacy documents)
ocr_queue.add_to_queue(doc_id, filepath, base_watch_dir=Path(config.UPLOAD_FOLDER))
else:
print(f"[STARTUP] Skipping doc {doc_id}: file not found")
print(f"[STARTUP] ✓ Initial scan complete, {len(unprocessed)} documents queued")
else:
print("[STARTUP] ✓ No unprocessed documents found")
# Start initialization in background thread to avoid blocking gunicorn worker
import threading
print("[STARTUP] Starting background initialization...")
init_thread = threading.Thread(target=_startup_initialization, daemon=True, name="StartupInit")
init_thread.start()
print("[STARTUP] Web server is ready to accept connections")
if __name__ == '__main__':
# This block only runs when executing directly (not under gunicorn)
app.run(
host=config.FLASK_HOST,
port=config.FLASK_PORT,
debug=config.FLASK_DEBUG,
use_reloader=False
)