-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
61 lines (52 loc) · 1.86 KB
/
tasks.py
File metadata and controls
61 lines (52 loc) · 1.86 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
import logging
from celery import shared_task
from django.utils import timezone
from apps.documents.models import Document
from apps.documents.services import (
analyze_document,
deliver_webhook,
download_file_from_s3,
extract_text,
)
logger = logging.getLogger(__name__)
@shared_task(
bind=True,
max_retries=3,
default_retry_delay=60,
name="documents.process_document",
)
def process_document(self, document_id: str):
try:
doc = Document.objects.get(id=document_id)
except Document.DoesNotExist:
logger.error(f"Document {document_id} not found — aborting task.")
return
doc.status = Document.Status.PROCESSING
doc.task_id = self.request.id or doc.task_id or ""
doc.save(update_fields=["status", "task_id", "updated_at"])
try:
file_bytes = download_file_from_s3(doc.s3_key)
raw_text = extract_text(file_bytes, doc.mime_type)
result = analyze_document(raw_text)
doc.raw_text = raw_text
doc.summary = result["summary"]
doc.extracted_data = result["extracted_data"]
doc.status = Document.Status.DONE
doc.processed_at = timezone.now()
doc.error_message = ""
doc.save(update_fields=[
"raw_text", "summary", "extracted_data",
"status", "processed_at", "error_message", "updated_at"
])
delivered = deliver_webhook(doc)
if delivered:
doc.webhook_delivered = True
doc.save(update_fields=["webhook_delivered"])
except Exception as exc:
logger.exception(f"[{document_id}] Processing failed: {exc}")
try:
raise self.retry(exc=exc)
except self.MaxRetriesExceededError:
doc.status = Document.Status.FAILED
doc.error_message = str(exc)
doc.save(update_fields=["status", "error_message", "updated_at"])