Fix error catching with socket connections
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

When changing to the new library, it broke the path to Error classes
This commit is contained in:
IamTheFij 2023-12-21 16:29:32 -08:00
parent 5591dd3b14
commit 40a37943a0
3 changed files with 25 additions and 10 deletions

1
.gitignore vendored
View File

@ -159,3 +159,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
venv/

View File

@ -5,7 +5,9 @@ from typing import Any
from typing import cast
import requests_unixsocket
requests = requests_unixsocket.Session()
from requests.exceptions import JSONDecodeError
session = requests_unixsocket.Session()
NOMAD_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646")
NOMAD_TOKEN = environ.get("NOMAD_TOKEN")
@ -18,7 +20,7 @@ def nomad_req(
if NOMAD_TOKEN:
headers["X-Nomad-Token"] = NOMAD_TOKEN
response = requests.request(
response = session.request(
method,
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
params=params,
@ -28,7 +30,7 @@ def nomad_req(
try:
return response.json()
except requests.exceptions.JSONDecodeError:
except JSONDecodeError:
return response.text
@ -43,11 +45,14 @@ def extract_job_services(job: dict[str, Any]) -> dict[str, str]:
return services
exit_code = 0
parser = ArgumentParser(
description="Checks for missing services and optionally restarts their allocs.",
)
parser.add_argument("-r", "--restart", action="store_true", help="Restart allocs for missing services")
parser.add_argument(
"-r", "--restart", action="store_true", help="Restart allocs for missing services"
)
args = parser.parse_args()
for job in nomad_req("jobs"):
@ -87,7 +92,10 @@ for job in nomad_req("jobs"):
restart_allocs: set[str] = set()
for allocation in nomad_req("job", job_detail["ID"], "allocations"):
allocation = cast(dict[str, Any], allocation)
if allocation["ClientStatus"] == "running" and allocation["TaskGroup"] in restart_groups:
if (
allocation["ClientStatus"] == "running"
and allocation["TaskGroup"] in restart_groups
):
restart_allocs.add(allocation["ID"])
# Restart allocs associated with missing services

View File

@ -5,7 +5,11 @@ from typing import Any
from typing import cast
import requests_unixsocket
requests = requests_unixsocket.Session()
from requests.exceptions import HTTPError
from requests.exceptions import JSONDecodeError
session = requests_unixsocket.Session()
NOMAD_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646")
@ -19,7 +23,7 @@ def nomad_req(
if NOMAD_TOKEN:
headers["X-Nomad-Token"] = NOMAD_TOKEN
response = requests.request(
response = session.request(
method,
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
params=params,
@ -29,7 +33,7 @@ def nomad_req(
try:
return response.json()
except requests.exceptions.JSONDecodeError:
except JSONDecodeError:
return response.text
@ -37,7 +41,9 @@ exit_code = 0
parser = ArgumentParser(
description="Checks for orphaned services and optionally deletes them.",
)
parser.add_argument("-d", "--delete", action="store_true", help="Delete orphan services")
parser.add_argument(
"-d", "--delete", action="store_true", help="Delete orphan services"
)
args = parser.parse_args()
@ -55,7 +61,7 @@ for namespace in nomad_req("services"):
try:
alloc = nomad_req("allocation", alloc_id)
continue
except requests.exceptions.HTTPError as e:
except HTTPError as e:
if e.response.status_code == 404:
alloc_found = False
message = f"alloc {alloc_id} not found for {service_name}."