|  | 
| 8 | 8 | 
 | 
| 9 | 9 | import os | 
| 10 | 10 | import json | 
|  | 11 | +import asyncio | 
| 11 | 12 | import traceback | 
| 12 | 13 | from aiohttp import ClientSession | 
| 13 | 14 | 
 | 
| @@ -53,54 +54,62 @@ async def get_job(session: ClientSession, retry=True) -> Optional[Dict[str, Any] | 
| 53 | 54 |             async with session.get(_job_get_url()) as response: | 
| 54 | 55 |                 if response.status == 204: | 
| 55 | 56 |                     log.debug("No content, no job to process.") | 
| 56 |  | -                    if not retry: | 
| 57 |  | -                        return None | 
|  | 57 | +                    if retry is False: | 
|  | 58 | +                        break | 
| 58 | 59 |                     continue | 
| 59 | 60 | 
 | 
| 60 | 61 |                 if response.status == 400: | 
| 61 | 62 |                     log.debug("Received 400 status, expected when FlashBoot is enabled.") | 
| 62 |  | -                    if not retry: | 
| 63 |  | -                        return None | 
|  | 63 | +                    if retry is False: | 
|  | 64 | +                        break | 
| 64 | 65 |                     continue | 
| 65 | 66 | 
 | 
| 66 | 67 |                 if response.status != 200: | 
| 67 | 68 |                     log.error(f"Failed to get job, status code: {response.status}") | 
| 68 |  | -                    if not retry: | 
| 69 |  | -                        return None | 
|  | 69 | +                    if retry is False: | 
|  | 70 | +                        break | 
| 70 | 71 |                     continue | 
| 71 | 72 | 
 | 
| 72 |  | -                next_job = await response.json() | 
| 73 |  | -                log.debug(f"Request Received | {next_job}") | 
|  | 73 | +                received_request = await response.json() | 
|  | 74 | +                log.debug("Request Received", {next_job}) | 
| 74 | 75 | 
 | 
| 75 |  | -            # Check if the job is valid | 
| 76 |  | -            job_id = next_job.get("id", None) | 
| 77 |  | -            job_input = next_job.get("input", None) | 
|  | 76 | +                # Check if the job is valid | 
|  | 77 | +                job_id = received_request.get("id", None) | 
|  | 78 | +                job_input = received_request.get("input", None) | 
| 78 | 79 | 
 | 
| 79 |  | -            if None in [job_id, job_input]: | 
| 80 |  | -                missing_fields = [] | 
| 81 |  | -                if job_id is None: | 
| 82 |  | -                    missing_fields.append("id") | 
| 83 |  | -                if job_input is None: | 
| 84 |  | -                    missing_fields.append("input") | 
|  | 80 | +                if None in [job_id, job_input]: | 
|  | 81 | +                    missing_fields = [] | 
|  | 82 | +                    if job_id is None: | 
|  | 83 | +                        missing_fields.append("id") | 
|  | 84 | +                    if job_input is None: | 
|  | 85 | +                        missing_fields.append("input") | 
| 85 | 86 | 
 | 
| 86 |  | -                log.error(f"Job has missing field(s): {', '.join(missing_fields)}.") | 
| 87 |  | -                next_job = None | 
|  | 87 | +                    log.error(f"Job has missing field(s): {', '.join(missing_fields)}.") | 
|  | 88 | +                else: | 
|  | 89 | +                    next_job = received_request | 
| 88 | 90 | 
 | 
| 89 | 91 |         except Exception as err:  # pylint: disable=broad-except | 
| 90 |  | -            log.error(f"Error while getting job: {err}") | 
|  | 92 | +            err_type = type(err).__name__ | 
|  | 93 | +            err_message = str(err) | 
|  | 94 | +            err_traceback = traceback.format_exc() | 
|  | 95 | +            log.error(f"Failed to get job, error type: {err_type}, error message: {err_message}") | 
|  | 96 | +            log.error(f"Traceback: {err_traceback}") | 
| 91 | 97 | 
 | 
| 92 | 98 |         if next_job is None: | 
| 93 | 99 |             log.debug("No job available, waiting for the next one.") | 
| 94 |  | -            if not retry: | 
| 95 |  | -                return None | 
|  | 100 | +            if retry is False: | 
|  | 101 | +                break | 
| 96 | 102 | 
 | 
| 97 |  | -    log.debug("Confirmed valid request.", next_job['id']) | 
|  | 103 | +        await asyncio.sleep(1) | 
|  | 104 | +    else: | 
|  | 105 | +        log.debug("Confirmed valid request.", next_job['id']) | 
| 98 | 106 | 
 | 
| 99 |  | -    if next_job: | 
| 100 | 107 |         job_list.add_job(next_job["id"]) | 
| 101 | 108 |         log.debug("Request ID added.", next_job['id']) | 
| 102 | 109 | 
 | 
| 103 |  | -    return next_job | 
|  | 110 | +        return next_job | 
|  | 111 | + | 
|  | 112 | +    return None | 
| 104 | 113 | 
 | 
| 105 | 114 | 
 | 
| 106 | 115 | async def run_job(handler: Callable, job: Dict[str, Any]) -> Dict[str, Any]: | 
|  | 
0 commit comments