fix: sent partial results
All checks were successful
Build Worker Base and Application Images / check-base-changes (push) Successful in 7s
Build Worker Base and Application Images / build-base (push) Has been skipped
Build Worker Base and Application Images / build-docker (push) Successful in 3m40s
Build Worker Base and Application Images / deploy-stack (push) Successful in 15s

This commit is contained in:
ziesorx 2025-10-20 17:54:50 +07:00
parent 10c54bc6e0
commit 0dd1b9f5c2
2 changed files with 155 additions and 19 deletions

View file

@ -45,12 +45,17 @@ class BranchProcessor:
self.redis_manager = None
self.db_manager = None
# Branch execution timeout (seconds)
self.branch_timeout = 30.0
# Statistics
self.stats = {
'branches_processed': 0,
'parallel_executions': 0,
'total_processing_time': 0.0,
'models_loaded': 0
'models_loaded': 0,
'branches_timed_out': 0,
'branches_failed': 0
}
logger.info("BranchProcessor initialized")
@ -279,22 +284,46 @@ class BranchProcessor:
)
future_to_branch[future] = branch
# Collect results as they complete
for future in as_completed(future_to_branch):
branch = future_to_branch[future]
branch_id = getattr(branch, 'model_id', 'unknown')
# Collect results as they complete with timeout
try:
for future in as_completed(future_to_branch, timeout=self.branch_timeout):
branch = future_to_branch[future]
branch_id = getattr(branch, 'model_id', 'unknown')
try:
result = future.result()
results[branch_id] = result
logger.info(f"[PARALLEL COMPLETE] {branch_id}: Branch completed successfully")
except Exception as e:
logger.error(f"Error in parallel branch {branch_id}: {e}")
results[branch_id] = {
'status': 'error',
'message': str(e),
'processing_time': 0.0
}
try:
# Get result with timeout to prevent indefinite hanging
result = future.result(timeout=self.branch_timeout)
results[branch_id] = result
logger.info(f"[PARALLEL COMPLETE] {branch_id}: Branch completed successfully")
except TimeoutError:
logger.error(f"[TIMEOUT] Branch {branch_id} exceeded timeout of {self.branch_timeout}s")
self.stats['branches_timed_out'] += 1
results[branch_id] = {
'status': 'timeout',
'message': f'Branch execution timeout after {self.branch_timeout}s',
'processing_time': self.branch_timeout
}
except Exception as e:
logger.error(f"[ERROR] Error in parallel branch {branch_id}: {e}", exc_info=True)
self.stats['branches_failed'] += 1
results[branch_id] = {
'status': 'error',
'message': str(e),
'processing_time': 0.0
}
except TimeoutError:
# as_completed iterator timed out - mark remaining futures as timed out
logger.error(f"[TIMEOUT] Branch execution timeout after {self.branch_timeout}s - some branches did not complete")
for future, branch in future_to_branch.items():
branch_id = getattr(branch, 'model_id', 'unknown')
if branch_id not in results:
logger.error(f"[TIMEOUT] Branch {branch_id} did not complete within timeout")
self.stats['branches_timed_out'] += 1
results[branch_id] = {
'status': 'timeout',
'message': f'Branch did not complete within {self.branch_timeout}s timeout',
'processing_time': self.branch_timeout
}
# Flatten nested branch results to top level for database access
flattened_results = {}
@ -309,6 +338,24 @@ class BranchProcessor:
flattened_results[nested_branch_id] = nested_result
logger.info(f"[FLATTEN] Added nested branch {nested_branch_id} to top-level results")
# Log summary of branch execution results
succeeded = [bid for bid, res in results.items() if res.get('status') == 'success']
failed = [bid for bid, res in results.items() if res.get('status') == 'error']
timed_out = [bid for bid, res in results.items() if res.get('status') == 'timeout']
skipped = [bid for bid, res in results.items() if res.get('status') == 'skipped']
summary_parts = []
if succeeded:
summary_parts.append(f"{len(succeeded)} succeeded: {', '.join(succeeded)}")
if failed:
summary_parts.append(f"{len(failed)} FAILED: {', '.join(failed)}")
if timed_out:
summary_parts.append(f"{len(timed_out)} TIMED OUT: {', '.join(timed_out)}")
if skipped:
summary_parts.append(f"{len(skipped)} skipped: {', '.join(skipped)}")
logger.info(f"[PARALLEL SUMMARY] Branch execution completed: {' | '.join(summary_parts) if summary_parts else 'no branches'}")
return flattened_results
async def _execute_sequential_branches(self,
@ -496,9 +543,19 @@ class BranchProcessor:
# Use .predict() method for both detection and classification models
inference_start = time.time()
detection_results = model.model.predict(input_frame, conf=min_confidence, verbose=False)
inference_time = time.time() - inference_start
logger.info(f"[INFERENCE DONE] {branch_id}: Predict completed in {inference_time:.3f}s using .predict() method")
try:
detection_results = model.model.predict(input_frame, conf=min_confidence, verbose=False)
inference_time = time.time() - inference_start
logger.info(f"[INFERENCE DONE] {branch_id}: Predict completed in {inference_time:.3f}s using .predict() method")
except Exception as inference_error:
inference_time = time.time() - inference_start
logger.error(f"[INFERENCE ERROR] {branch_id}: Model inference failed after {inference_time:.3f}s: {inference_error}", exc_info=True)
return {
'status': 'error',
'branch_id': branch_id,
'message': f'Model inference failed: {str(inference_error)}',
'processing_time': time.time() - start_time
}
# Initialize branch_detections outside the conditional
branch_detections = []