Add file processing notifications feature
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -156,6 +156,10 @@ class Settings(BaseSettings):
|
||||
default=False,
|
||||
description="Send notifications when application shuts down"
|
||||
)
|
||||
notify_on_file_processed: bool = Field(
|
||||
default=True,
|
||||
description="Send notifications when files are successfully processed"
|
||||
)
|
||||
|
||||
@validator('notification_urls', pre=True)
|
||||
def parse_notification_urls(cls, v):
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
from app.config import settings
|
||||
from app.tasks.retry_config import BaseTaskWithRetry
|
||||
# Import the shared Celery instance
|
||||
from app.celery_app import celery
|
||||
|
||||
# 1) Import the aggregator task
|
||||
from app.tasks.send_to_all import send_to_all_destinations
|
||||
from app.tasks.send_to_all import send_to_all_destinations, get_configured_services_from_validator
|
||||
|
||||
# Import notification utility
|
||||
from app.utils.notification import notify_file_processed
|
||||
|
||||
|
||||
@celery.task(base=BaseTaskWithRetry)
|
||||
@@ -14,12 +18,42 @@ def finalize_document_storage(original_file: str, processed_file: str, metadata:
|
||||
"""
|
||||
Final storage step after embedding metadata.
|
||||
We will now call 'send_to_all_destinations' to push the final PDF to Dropbox/Nextcloud/Paperless.
|
||||
After uploading, send a notification about the processed file.
|
||||
"""
|
||||
print(f"[INFO] Finalizing document storage for {processed_file}")
|
||||
|
||||
# Determine which destinations are configured
|
||||
configured_destinations = []
|
||||
try:
|
||||
configured_services = get_configured_services_from_validator()
|
||||
# Get list of service names that are configured
|
||||
for service_name, is_configured in configured_services.items():
|
||||
if is_configured:
|
||||
# Format service names for display
|
||||
display_name = service_name.replace('_', ' ').title()
|
||||
configured_destinations.append(display_name)
|
||||
except Exception as e:
|
||||
print(f"[WARNING] Could not determine configured destinations: {e}")
|
||||
configured_destinations = ["configured destinations"]
|
||||
|
||||
# 2) Enqueue uploads to all destinations (Dropbox, Nextcloud, Paperless)
|
||||
send_to_all_destinations.delay(processed_file)
|
||||
|
||||
# 3) Send notification about successful file processing
|
||||
try:
|
||||
# Get file information
|
||||
file_size = os.path.getsize(processed_file) if os.path.exists(processed_file) else 0
|
||||
filename = os.path.basename(processed_file)
|
||||
|
||||
notify_file_processed(
|
||||
filename=filename,
|
||||
file_size=file_size,
|
||||
metadata=metadata,
|
||||
destinations=configured_destinations
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[WARNING] Failed to send file processed notification: {e}")
|
||||
|
||||
return {
|
||||
"status": "Completed",
|
||||
"file": processed_file
|
||||
|
||||
@@ -183,3 +183,38 @@ def notify_shutdown() -> bool:
|
||||
notification_type="info",
|
||||
tags=["system", "shutdown"]
|
||||
)
|
||||
|
||||
def notify_file_processed(filename: str, file_size: int, metadata: dict, destinations: list) -> bool:
|
||||
"""Send a notification that a file has been successfully processed"""
|
||||
if not settings.notify_on_file_processed:
|
||||
return False
|
||||
|
||||
# Format file size for display
|
||||
size_mb = file_size / (1024 * 1024)
|
||||
size_str = f"{size_mb:.2f} MB" if size_mb >= 1 else f"{file_size / 1024:.2f} KB"
|
||||
|
||||
# Extract key metadata fields
|
||||
doc_type = metadata.get('document_type', 'Unknown')
|
||||
tags = metadata.get('tags', [])
|
||||
tags_str = ', '.join(tags) if tags else 'None'
|
||||
|
||||
# Format destinations
|
||||
destinations_str = ', '.join(destinations) if destinations else 'None configured'
|
||||
|
||||
title = f"File Processed: {filename}"
|
||||
message = f"""
|
||||
File: {filename}
|
||||
Size: {size_str}
|
||||
Document Type: {doc_type}
|
||||
Tags: {tags_str}
|
||||
Destinations: {destinations_str}
|
||||
|
||||
The file has been successfully processed and uploaded to all configured destinations.
|
||||
"""
|
||||
|
||||
return send_notification(
|
||||
title=title,
|
||||
message=message.strip(),
|
||||
notification_type="success",
|
||||
tags=["document", "processed", "success"]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user