Fix error catching with socket connections
When changing to the new library, it broke the path to Error classes
This commit is contained in:
parent
5591dd3b14
commit
40a37943a0
1
.gitignore
vendored
1
.gitignore
vendored
@ -159,3 +159,4 @@ cython_debug/
|
|||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# 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.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
venv/
|
||||||
|
@ -5,7 +5,9 @@ from typing import Any
|
|||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
import requests_unixsocket
|
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_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646")
|
||||||
NOMAD_TOKEN = environ.get("NOMAD_TOKEN")
|
NOMAD_TOKEN = environ.get("NOMAD_TOKEN")
|
||||||
@ -18,7 +20,7 @@ def nomad_req(
|
|||||||
if NOMAD_TOKEN:
|
if NOMAD_TOKEN:
|
||||||
headers["X-Nomad-Token"] = NOMAD_TOKEN
|
headers["X-Nomad-Token"] = NOMAD_TOKEN
|
||||||
|
|
||||||
response = requests.request(
|
response = session.request(
|
||||||
method,
|
method,
|
||||||
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
|
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
|
||||||
params=params,
|
params=params,
|
||||||
@ -28,7 +30,7 @@ def nomad_req(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.exceptions.JSONDecodeError:
|
except JSONDecodeError:
|
||||||
return response.text
|
return response.text
|
||||||
|
|
||||||
|
|
||||||
@ -43,11 +45,14 @@ def extract_job_services(job: dict[str, Any]) -> dict[str, str]:
|
|||||||
|
|
||||||
return services
|
return services
|
||||||
|
|
||||||
|
|
||||||
exit_code = 0
|
exit_code = 0
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
description="Checks for missing services and optionally restarts their allocs.",
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
for job in nomad_req("jobs"):
|
for job in nomad_req("jobs"):
|
||||||
@ -87,7 +92,10 @@ for job in nomad_req("jobs"):
|
|||||||
restart_allocs: set[str] = set()
|
restart_allocs: set[str] = set()
|
||||||
for allocation in nomad_req("job", job_detail["ID"], "allocations"):
|
for allocation in nomad_req("job", job_detail["ID"], "allocations"):
|
||||||
allocation = cast(dict[str, Any], allocation)
|
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.add(allocation["ID"])
|
||||||
|
|
||||||
# Restart allocs associated with missing services
|
# Restart allocs associated with missing services
|
||||||
|
@ -5,7 +5,11 @@ from typing import Any
|
|||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
import requests_unixsocket
|
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")
|
NOMAD_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646")
|
||||||
@ -19,7 +23,7 @@ def nomad_req(
|
|||||||
if NOMAD_TOKEN:
|
if NOMAD_TOKEN:
|
||||||
headers["X-Nomad-Token"] = NOMAD_TOKEN
|
headers["X-Nomad-Token"] = NOMAD_TOKEN
|
||||||
|
|
||||||
response = requests.request(
|
response = session.request(
|
||||||
method,
|
method,
|
||||||
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
|
f"{NOMAD_ADDR}/v1/{'/'.join(path)}",
|
||||||
params=params,
|
params=params,
|
||||||
@ -29,7 +33,7 @@ def nomad_req(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.exceptions.JSONDecodeError:
|
except JSONDecodeError:
|
||||||
return response.text
|
return response.text
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +41,9 @@ exit_code = 0
|
|||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
description="Checks for orphaned services and optionally deletes them.",
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@ -55,7 +61,7 @@ for namespace in nomad_req("services"):
|
|||||||
try:
|
try:
|
||||||
alloc = nomad_req("allocation", alloc_id)
|
alloc = nomad_req("allocation", alloc_id)
|
||||||
continue
|
continue
|
||||||
except requests.exceptions.HTTPError as e:
|
except HTTPError as e:
|
||||||
if e.response.status_code == 404:
|
if e.response.status_code == 404:
|
||||||
alloc_found = False
|
alloc_found = False
|
||||||
message = f"alloc {alloc_id} not found for {service_name}."
|
message = f"alloc {alloc_id} not found for {service_name}."
|
||||||
|
Loading…
Reference in New Issue
Block a user