Update app3.py
Browse files
app3.py
CHANGED
|
@@ -265,6 +265,7 @@ class TranslationOrchestrator:
|
|
| 265 |
|
| 266 |
self.handle_worker_failure(worker_id, request_id, "Timeout waiting for completion")
|
| 267 |
|
|
|
|
| 268 |
def handle_job_completion(self, worker_id: str, request_id: str, worker_response: Dict):
|
| 269 |
worker = self.workers[worker_id]
|
| 270 |
|
|
@@ -282,13 +283,14 @@ class TranslationOrchestrator:
|
|
| 282 |
completion_data = {
|
| 283 |
'request_id': request_id,
|
| 284 |
'status': 'completed',
|
| 285 |
-
'translated_text': worker_response.get('translated_text'),
|
| 286 |
'processing_time': processing_time,
|
| 287 |
'character_count': worker_response.get('character_count', len(job_data.get('text', ''))),
|
| 288 |
'translation_length': worker_response.get('translation_length', 0),
|
| 289 |
'from_cache': worker_response.get('from_cache', False),
|
| 290 |
'worker_name': worker.config.name,
|
| 291 |
-
'completed_at': datetime.now().isoformat()
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
self.completed_jobs[request_id] = completion_data
|
|
@@ -298,7 +300,12 @@ class TranslationOrchestrator:
|
|
| 298 |
|
| 299 |
notification_url = job_data.get('notification_url')
|
| 300 |
if notification_url:
|
|
|
|
|
|
|
| 301 |
self.notify_wordpress(notification_url, completion_data)
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
def handle_worker_failure(self, worker_id: str, request_id: str, error_message: str):
|
| 304 |
worker = self.workers[worker_id]
|
|
@@ -339,6 +346,8 @@ class TranslationOrchestrator:
|
|
| 339 |
|
| 340 |
def notify_wordpress(self, notification_url: str, data: Dict):
|
| 341 |
try:
|
|
|
|
|
|
|
| 342 |
response = requests.post(
|
| 343 |
notification_url,
|
| 344 |
json=data,
|
|
@@ -347,12 +356,19 @@ class TranslationOrchestrator:
|
|
| 347 |
)
|
| 348 |
|
| 349 |
if response.status_code == 200:
|
| 350 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
else:
|
| 352 |
-
print(f"
|
|
|
|
| 353 |
|
| 354 |
except Exception as e:
|
| 355 |
-
print(f"
|
|
|
|
| 356 |
|
| 357 |
# Initialize FastAPI app
|
| 358 |
app = FastAPI(
|
|
@@ -476,6 +492,53 @@ async def check_status(request_id: str):
|
|
| 476 |
"message": "Translation request not found"
|
| 477 |
}
|
| 478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
@app.get("/api/workers")
|
| 480 |
async def list_workers():
|
| 481 |
workers_info = []
|
|
|
|
| 265 |
|
| 266 |
self.handle_worker_failure(worker_id, request_id, "Timeout waiting for completion")
|
| 267 |
|
| 268 |
+
|
| 269 |
def handle_job_completion(self, worker_id: str, request_id: str, worker_response: Dict):
|
| 270 |
worker = self.workers[worker_id]
|
| 271 |
|
|
|
|
| 283 |
completion_data = {
|
| 284 |
'request_id': request_id,
|
| 285 |
'status': 'completed',
|
| 286 |
+
'translated_text': worker_response.get('translated_text', ''),
|
| 287 |
'processing_time': processing_time,
|
| 288 |
'character_count': worker_response.get('character_count', len(job_data.get('text', ''))),
|
| 289 |
'translation_length': worker_response.get('translation_length', 0),
|
| 290 |
'from_cache': worker_response.get('from_cache', False),
|
| 291 |
'worker_name': worker.config.name,
|
| 292 |
+
'completed_at': datetime.now().isoformat(),
|
| 293 |
+
'success': True # اضافه کردن این خط
|
| 294 |
}
|
| 295 |
|
| 296 |
self.completed_jobs[request_id] = completion_data
|
|
|
|
| 300 |
|
| 301 |
notification_url = job_data.get('notification_url')
|
| 302 |
if notification_url:
|
| 303 |
+
# اضافه کردن لاگ برای دیباگ
|
| 304 |
+
print(f"📤 Sending notification to WordPress: {notification_url}")
|
| 305 |
self.notify_wordpress(notification_url, completion_data)
|
| 306 |
+
else:
|
| 307 |
+
print(f"⚠️ No notification URL for {request_id}")
|
| 308 |
+
|
| 309 |
|
| 310 |
def handle_worker_failure(self, worker_id: str, request_id: str, error_message: str):
|
| 311 |
worker = self.workers[worker_id]
|
|
|
|
| 346 |
|
| 347 |
def notify_wordpress(self, notification_url: str, data: Dict):
|
| 348 |
try:
|
| 349 |
+
print(f"🔔 Notifying WordPress for request {data['request_id']}")
|
| 350 |
+
|
| 351 |
response = requests.post(
|
| 352 |
notification_url,
|
| 353 |
json=data,
|
|
|
|
| 356 |
)
|
| 357 |
|
| 358 |
if response.status_code == 200:
|
| 359 |
+
print(f"✅ WordPress notified successfully for {data['request_id']}")
|
| 360 |
+
try:
|
| 361 |
+
response_data = response.json()
|
| 362 |
+
print(f"📝 WordPress response: {response_data}")
|
| 363 |
+
except:
|
| 364 |
+
print(f"📝 WordPress response: {response.text}")
|
| 365 |
else:
|
| 366 |
+
print(f"❌ WordPress notification failed: HTTP {response.status_code}")
|
| 367 |
+
print(f"📄 Response: {response.text}")
|
| 368 |
|
| 369 |
except Exception as e:
|
| 370 |
+
print(f"💥 WordPress notification error: {str(e)}")
|
| 371 |
+
|
| 372 |
|
| 373 |
# Initialize FastAPI app
|
| 374 |
app = FastAPI(
|
|
|
|
| 492 |
"message": "Translation request not found"
|
| 493 |
}
|
| 494 |
|
| 495 |
+
# اضافه کردن endpoint جدید برای بررسی وضعیت
|
| 496 |
+
@app.post("/api/get-translation-result")
|
| 497 |
+
async def get_translation_result(request: dict):
|
| 498 |
+
request_id = request.get('request_id')
|
| 499 |
+
|
| 500 |
+
if not request_id:
|
| 501 |
+
raise HTTPException(status_code=400, detail="Request ID is required")
|
| 502 |
+
|
| 503 |
+
# بررسی در jobs تکمیل شده
|
| 504 |
+
if request_id in orchestrator.completed_jobs:
|
| 505 |
+
result = orchestrator.completed_jobs[request_id]
|
| 506 |
+
return {
|
| 507 |
+
"success": True,
|
| 508 |
+
"status": "completed",
|
| 509 |
+
**result
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
# بررسی در jobs فعال
|
| 513 |
+
if request_id in orchestrator.active_jobs:
|
| 514 |
+
job_info = orchestrator.active_jobs[request_id]
|
| 515 |
+
elapsed_time = time.time() - job_info['start_time']
|
| 516 |
+
|
| 517 |
+
return {
|
| 518 |
+
"success": True,
|
| 519 |
+
"status": "processing",
|
| 520 |
+
"request_id": request_id,
|
| 521 |
+
"elapsed_time": elapsed_time,
|
| 522 |
+
"message": "Translation in progress"
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
# بررسی در صف
|
| 526 |
+
for job in orchestrator.job_queue:
|
| 527 |
+
if job['request_id'] == request_id:
|
| 528 |
+
return {
|
| 529 |
+
"success": True,
|
| 530 |
+
"status": "queued",
|
| 531 |
+
"request_id": request_id,
|
| 532 |
+
"queue_position": list(orchestrator.job_queue).index(job) + 1,
|
| 533 |
+
"message": "Translation queued"
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
return {
|
| 537 |
+
"success": False,
|
| 538 |
+
"status": "not_found",
|
| 539 |
+
"message": "Translation request not found"
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
@app.get("/api/workers")
|
| 543 |
async def list_workers():
|
| 544 |
workers_info = []
|