Add connection error handling
This commit is contained in:
parent
9b81a6cc8b
commit
adb56feb7b
1 changed files with 40 additions and 22 deletions
|
|
@ -24,6 +24,7 @@ class MgmtApiHelper:
|
||||||
|
|
||||||
def get_next_job(self) -> Union[Job, None]:
|
def get_next_job(self) -> Union[Job, None]:
|
||||||
"""Gets the next job to do, if any"""
|
"""Gets the next job to do, if any"""
|
||||||
|
try:
|
||||||
resp = requests.get(
|
resp = requests.get(
|
||||||
f"{self._mgmt_host}/api/v1/mgmt/dequeue?mgmtKey={self._mgmt_key}",
|
f"{self._mgmt_host}/api/v1/mgmt/dequeue?mgmtKey={self._mgmt_key}",
|
||||||
timeout=self._mgmt_timeout,
|
timeout=self._mgmt_timeout,
|
||||||
|
|
@ -32,14 +33,23 @@ class MgmtApiHelper:
|
||||||
if resp.status_code == 404:
|
if resp.status_code == 404:
|
||||||
return None
|
return None
|
||||||
return self._dict_to_job(resp.json())
|
return self._dict_to_job(resp.json())
|
||||||
|
except requests.exceptions.ConnectionError as exc:
|
||||||
|
print("Failed to get next job:")
|
||||||
|
print(exc)
|
||||||
|
return None
|
||||||
|
|
||||||
def get_resumable_jobs(self) -> List[Job]:
|
def get_resumable_jobs(self) -> List[Job]:
|
||||||
"""Gets all active jobs - only used for resuming between restarts"""
|
"""Gets all active jobs - only used for resuming between restarts"""
|
||||||
|
try:
|
||||||
resp = requests.get(
|
resp = requests.get(
|
||||||
f"{self._mgmt_host}/api/v1/mgmt/resumable?mgmtKey={self._mgmt_key}",
|
f"{self._mgmt_host}/api/v1/mgmt/resumable?mgmtKey={self._mgmt_key}",
|
||||||
timeout=self._mgmt_timeout,
|
timeout=self._mgmt_timeout,
|
||||||
)
|
)
|
||||||
return [self._dict_to_job(x) for x in resp.json()]
|
return [self._dict_to_job(x) for x in resp.json()]
|
||||||
|
except requests.exceptions.ConnectionError as exc:
|
||||||
|
print("Failed to get resumable jobs:")
|
||||||
|
print(exc)
|
||||||
|
return []
|
||||||
|
|
||||||
def _dict_to_job(self, data: Dict) -> Job:
|
def _dict_to_job(self, data: Dict) -> Job:
|
||||||
"""Converts a dictionary (from mongo) into a Job"""
|
"""Converts a dictionary (from mongo) into a Job"""
|
||||||
|
|
@ -58,19 +68,27 @@ class MgmtApiHelper:
|
||||||
|
|
||||||
def update_job_status(self, job_id: str, status: Status) -> None:
|
def update_job_status(self, job_id: str, status: Status) -> None:
|
||||||
"""Updates the status of a job"""
|
"""Updates the status of a job"""
|
||||||
|
try:
|
||||||
requests.patch(
|
requests.patch(
|
||||||
f"{self._mgmt_host}/api/v1/mgmt/update/{job_id}?mgmtKey={self._mgmt_key}",
|
f"{self._mgmt_host}/api/v1/mgmt/update/{job_id}?mgmtKey={self._mgmt_key}",
|
||||||
timeout=self._mgmt_timeout,
|
timeout=self._mgmt_timeout,
|
||||||
data={"status": status.value},
|
data={"status": status.value},
|
||||||
)
|
)
|
||||||
|
except requests.exceptions.ConnectionError as exc:
|
||||||
|
print(f"Failed to update the status of job {job_id}:")
|
||||||
|
print(exc)
|
||||||
|
|
||||||
def append_job_log(self, job_id: str, newlog: str) -> None:
|
def append_job_log(self, job_id: str, newlog: str) -> None:
|
||||||
"""Appends to the logs for a job"""
|
"""Appends to the logs for a job"""
|
||||||
|
try:
|
||||||
requests.patch(
|
requests.patch(
|
||||||
f"{self._mgmt_host}/api/v1/mgmt/update/{job_id}?mgmtKey={self._mgmt_key}",
|
f"{self._mgmt_host}/api/v1/mgmt/update/{job_id}?mgmtKey={self._mgmt_key}",
|
||||||
timeout=self._mgmt_timeout,
|
timeout=self._mgmt_timeout,
|
||||||
data={"log": newlog},
|
data={"log": newlog},
|
||||||
)
|
)
|
||||||
|
except requests.exceptions.ConnectionError as exc:
|
||||||
|
print(f"Failed to update the status of job {job_id}:")
|
||||||
|
print(exc)
|
||||||
|
|
||||||
|
|
||||||
class MgmtApiLogger:
|
class MgmtApiLogger:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue