Change local job monitoring to poll database instead of TCP

- Use get_queue_by_job_id to check job status
- More reliable than TCP polling for local jobs
parent 08a9a99e
......@@ -628,49 +628,55 @@ class ClusterMaster:
return None
async def _monitor_job_result(self, job_id: str, process_type: str) -> None:
"""Monitor for job result from local backend."""
"""Monitor for job result by polling database."""
try:
from .comm import SocketCommunicator
from .config import get_backend_web_port
from .database import get_queue_by_job_id
import json
# Poll for result
for _ in range(300): # Poll for up to 5 minutes (300 * 1s)
try:
backend_comm = SocketCommunicator(host='localhost', port=get_backend_web_port(), comm_type='tcp')
backend_comm.connect()
# Send get_result request
result_request = Message(
msg_type='get_result',
msg_id=f'poll_{job_id}',
data={'request_id': job_id}
)
backend_comm.send_message(result_request)
# Try to receive response
response = backend_comm.receive_message()
if response and response.msg_type in ['analyze_response', 'train_response']:
result_data = response.data
print(f"Received result for job {job_id}")
# Handle result
await self._handle_job_result({
'job_id': job_id,
'result': result_data
})
# Clean up
if job_id in self.pending_jobs:
del self.pending_jobs[job_id]
return
elif response and response.msg_type == 'result_pending':
# Result not ready yet, wait and try again
await asyncio.sleep(1)
continue
queue_entry = get_queue_by_job_id(job_id)
if queue_entry:
status = queue_entry['status']
if status == 'completed':
result = json.loads(queue_entry['result']) if queue_entry['result'] else {}
print(f"Received result for job {job_id}")
# Handle result
await self._handle_job_result({
'job_id': job_id,
'result': result
})
# Clean up
if job_id in self.pending_jobs:
del self.pending_jobs[job_id]
return
elif status == 'failed':
error = queue_entry.get('error', 'Unknown error')
result = {'status': 'failed', 'error': error}
print(f"Job {job_id} failed: {error}")
# Handle failed result
await self._handle_job_result({
'job_id': job_id,
'result': result
})
# Clean up
if job_id in self.pending_jobs:
del self.pending_jobs[job_id]
return
elif status == 'processing':
# Still processing, wait and try again
await asyncio.sleep(1)
continue
except Exception as e:
print(f"Error polling for job {job_id} result: {e}")
print(f"Error polling for job {job_id} status: {e}")
await asyncio.sleep(1)
# Timeout - job took too long
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment