Skip to content

Commit 8ee7086

Browse files
authored
Add robust exception handling to NetBox tasks for timeout resilience (#1848)
- Add exception handling in _update_netbox_device_field helper function - Catch ConnectTimeout, Timeout, ConnectionError, and RequestException - Log clear error messages including NetBox URL, device name, and exception details - Continue processing remaining NetBox instances after individual failures - Preserve existing semaphore and locking behavior AI-assisted: Claude Code Signed-off-by: Christian Berendt <[email protected]>
1 parent 9cd771c commit 8ee7086

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

osism/tasks/netbox.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
import requests.exceptions
34
from celery import Celery
45
from loguru import logger
56

@@ -29,12 +30,37 @@ def _update_netbox_device_field(nb, device_name, field_name, value):
2930
"""
3031
semaphore = utils.create_netbox_semaphore(nb.base_url)
3132
with semaphore:
32-
device = nb.dcim.devices.get(name=device_name)
33-
if device:
34-
device.custom_fields.update({field_name: value})
35-
device.save()
36-
return True
37-
return False
33+
try:
34+
device = nb.dcim.devices.get(name=device_name)
35+
if device:
36+
device.custom_fields.update({field_name: value})
37+
device.save()
38+
return True
39+
return False
40+
except requests.exceptions.ConnectTimeout as e:
41+
logger.error(
42+
f"Connection timeout while updating {field_name} for device {device_name} "
43+
f"on {nb.base_url}: {e}"
44+
)
45+
return False
46+
except requests.exceptions.Timeout as e:
47+
logger.error(
48+
f"Request timeout while updating {field_name} for device {device_name} "
49+
f"on {nb.base_url}: {e}"
50+
)
51+
return False
52+
except requests.exceptions.ConnectionError as e:
53+
logger.error(
54+
f"Connection error while updating {field_name} for device {device_name} "
55+
f"on {nb.base_url}: {e}"
56+
)
57+
return False
58+
except requests.exceptions.RequestException as e:
59+
logger.error(
60+
f"Request error while updating {field_name} for device {device_name} "
61+
f"on {nb.base_url}: {e}"
62+
)
63+
return False
3864

3965

4066
def _matches_netbox_filter(nb, netbox_filter, is_primary=False):

0 commit comments

Comments
 (0)