Working default workflow (file -> celery -> redis -> worker)
This commit is contained in:
@@ -3,25 +3,26 @@ Celery worker for MyDocManager document processing tasks.
|
||||
|
||||
This module contains all Celery tasks for processing documents.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
|
||||
from celery import Celery
|
||||
|
||||
from tasks.document_processing import process_document_async
|
||||
|
||||
# Environment variables
|
||||
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||
MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
|
||||
|
||||
# Initialize Celery app
|
||||
app = Celery(
|
||||
celery_app = Celery(
|
||||
"mydocmanager_worker",
|
||||
broker=REDIS_URL,
|
||||
backend=REDIS_URL
|
||||
backend=REDIS_URL,
|
||||
)
|
||||
|
||||
# Celery configuration
|
||||
app.conf.update(
|
||||
celery_app.conf.update(
|
||||
task_serializer="json",
|
||||
accept_content=["json"],
|
||||
result_serializer="json",
|
||||
@@ -33,82 +34,10 @@ app.conf.update(
|
||||
)
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def test_task(self, message: str):
|
||||
"""
|
||||
Test task for validating worker functionality.
|
||||
|
||||
Args:
|
||||
message: Test message to process
|
||||
|
||||
Returns:
|
||||
dict: Task result with processing information
|
||||
"""
|
||||
try:
|
||||
print(f"[WORKER] Starting test task with message: {message}")
|
||||
|
||||
# Simulate some work
|
||||
for i in range(5):
|
||||
print(f"[WORKER] Processing step {i + 1}/5...")
|
||||
time.sleep(1)
|
||||
|
||||
# Update task progress
|
||||
self.update_state(
|
||||
state="PROGRESS",
|
||||
meta={
|
||||
"current": i + 1,
|
||||
"total": 5,
|
||||
"message": f"Processing step {i + 1}"
|
||||
}
|
||||
)
|
||||
|
||||
result = {
|
||||
"status": "completed",
|
||||
"message": f"Successfully processed: {message}",
|
||||
"processed_at": time.time(),
|
||||
"worker_id": self.request.id
|
||||
}
|
||||
|
||||
print(f"[WORKER] Test task completed successfully: {result}")
|
||||
return result
|
||||
|
||||
except Exception as exc:
|
||||
print(f"[WORKER] Test task failed: {str(exc)}")
|
||||
raise self.retry(exc=exc, countdown=60, max_retries=3)
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def process_document_task(self, file_path: str):
|
||||
"""
|
||||
Placeholder task for document processing.
|
||||
|
||||
Args:
|
||||
file_path: Path to the document to process
|
||||
|
||||
Returns:
|
||||
dict: Processing result
|
||||
"""
|
||||
try:
|
||||
print(f"[WORKER] Starting document processing for: {file_path}")
|
||||
|
||||
# Placeholder for document processing logic
|
||||
time.sleep(2) # Simulate processing time
|
||||
|
||||
result = {
|
||||
"status": "completed",
|
||||
"file_path": file_path,
|
||||
"processed_at": time.time(),
|
||||
"content": f"Placeholder content for {file_path}",
|
||||
"worker_id": self.request.id
|
||||
}
|
||||
|
||||
print(f"[WORKER] Document processing completed: {file_path}")
|
||||
return result
|
||||
|
||||
except Exception as exc:
|
||||
print(f"[WORKER] Document processing failed for {file_path}: {str(exc)}")
|
||||
raise self.retry(exc=exc, countdown=60, max_retries=3)
|
||||
@celery_app.task(bind=True, autoretry_for=(Exception,), retry_kwargs={'max_retries': 3, 'countdown': 60})
|
||||
def process_document(self, filepath: str):
|
||||
return asyncio.run(process_document_async(self, filepath))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.start()
|
||||
celery_app.start()
|
||||
|
||||
Reference in New Issue
Block a user