Refactor fleet module and add cleanup script
- Update Celery configuration in celery.py - Modify base settings for improved performance - Enhance task management in fleet tasks.py - Revise fleet views.py for better data handling - Add cleanup_tmp_archives.sh script for temporary file management - Adjust docker-compose.prod.yml for consistency - Optimize fleetDashboardStore.js with reduced code complexity
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
# backend/modules/fleet/tasks.py
|
||||
from celery import shared_task
|
||||
from core.celery import ResourceAwareTask
|
||||
from django.core.mail import send_mail
|
||||
from django.core.mail import EmailMessage
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.core.files import File
|
||||
from django.core.files.base import ContentFile
|
||||
import logging
|
||||
from io import BytesIO
|
||||
@@ -835,13 +837,12 @@ def process_work_order_invoice_ocr(invoice_id):
|
||||
return {"status": "ok", "invoice_id": str(invoice.pk)}
|
||||
|
||||
|
||||
@shared_task
|
||||
@shared_task(base=ResourceAwareTask)
|
||||
def build_monthly_archive_cached_task(generated_archive_id):
|
||||
from .models import GeneratedFleetArchive
|
||||
from .services import NotificationService
|
||||
from .views import (
|
||||
_build_monthly_service_tasks_archive_content,
|
||||
_build_monthly_work_orders_archive_content,
|
||||
_build_monthly_service_tasks_archive_to_temp_file,
|
||||
_build_monthly_work_orders_archive_to_temp_file,
|
||||
_notify_monthly_archive_request,
|
||||
)
|
||||
|
||||
@@ -854,25 +855,27 @@ def build_monthly_archive_cached_task(generated_archive_id):
|
||||
if generated is None:
|
||||
return {"status": "failed", "error": "Generated archive record not found"}
|
||||
|
||||
tmp_archive_path = None
|
||||
try:
|
||||
if generated.archive_type == 'service_tasks':
|
||||
archive_content = _build_monthly_service_tasks_archive_content(
|
||||
tmp_archive_path = _build_monthly_service_tasks_archive_to_temp_file(
|
||||
user=generated.requested_by,
|
||||
year=generated.year,
|
||||
month=generated.month,
|
||||
)
|
||||
else:
|
||||
archive_content = _build_monthly_work_orders_archive_content(
|
||||
tmp_archive_path = _build_monthly_work_orders_archive_to_temp_file(
|
||||
user=generated.requested_by,
|
||||
year=generated.year,
|
||||
month=generated.month,
|
||||
)
|
||||
|
||||
if not archive_content:
|
||||
if tmp_archive_path is None or tmp_archive_path.stat().st_size <= 0:
|
||||
raise ValueError('ZIP arhiva je prazna.')
|
||||
|
||||
filename = generated.filename or f"{generated.archive_type}-{generated.year}-{generated.month}.zip"
|
||||
generated.file.save(filename, ContentFile(archive_content), save=False)
|
||||
with tmp_archive_path.open('rb') as temp_handle:
|
||||
generated.file.save(filename, File(temp_handle), save=False)
|
||||
generated.status = 'ready'
|
||||
generated.error_message = ''
|
||||
generated.save(update_fields=['file', 'status', 'error_message', 'updated_at'])
|
||||
@@ -902,6 +905,9 @@ def build_monthly_archive_cached_task(generated_archive_id):
|
||||
)
|
||||
logger.exception("Greška kod build_monthly_archive_cached_task: %s", exc)
|
||||
return {"status": "failed", "error": str(exc)}
|
||||
finally:
|
||||
if tmp_archive_path is not None:
|
||||
tmp_archive_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
@shared_task
|
||||
@@ -924,6 +930,14 @@ def cleanup_expired_generated_archives_task():
|
||||
return {"deleted": deleted}
|
||||
|
||||
|
||||
@shared_task
|
||||
def cleanup_stale_tmp_archives_task():
|
||||
from .views import _cleanup_stale_tmp_archives
|
||||
|
||||
deleted = _cleanup_stale_tmp_archives()
|
||||
return {"deleted": deleted}
|
||||
|
||||
|
||||
@shared_task
|
||||
def cleanup_expired_generated_pdfs_task():
|
||||
now = timezone.now()
|
||||
|
||||
Reference in New Issue
Block a user