Skip to content

Commit 9034941

Browse files
committed
Add a exceptions constant for api access retries
We have run in problems with the application when the nvd API responds properly http wise, but delivers json that cannot be decoded properly. To keep retrying these API calls until they succeed, which currently happens after ~ 1-5 tries, we need to use a tuple with the already used http error and also JSONDecodeError. As this happens multiple times, we use a constant that can be used in multiple places. We should refactor these accesses afterwards when the high prio fix is done, in order to have this functionality reusable in one place, because having the basically same stamina call in so many places calls for improvement.
1 parent 9af37ac commit 9034941

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

greenbone/scap/constants.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2025 Greenbone AG
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
from json import JSONDecodeError
5+
6+
from httpx import HTTPError
7+
8+
# Exceptions on API access to catch in order to retry these calls
9+
STAMINA_API_RETRY_EXCEPTIONS = (JSONDecodeError, HTTPError)

greenbone/scap/cpe/cli/download.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
CLIError,
3030
CLIRunner,
3131
)
32+
from greenbone.scap.constants import STAMINA_API_RETRY_EXCEPTIONS
3233
from greenbone.scap.cpe.manager import CPEManager
3334
from greenbone.scap.db import PostgresDatabase
3435
from greenbone.scap.timer import Timer
@@ -206,7 +207,7 @@ async def _producer(
206207
with Timer() as download_timer:
207208
count = 0
208209
async for attempt in stamina.retry_context(
209-
on=httpx.HTTPError,
210+
on=STAMINA_API_RETRY_EXCEPTIONS,
210211
attempts=retry_attempts,
211212
timeout=None,
212213
):
@@ -250,7 +251,7 @@ async def download(
250251
last_modified_end_date: datetime | None,
251252
) -> None:
252253
async for attempt in stamina.retry_context(
253-
on=httpx.HTTPError,
254+
on=STAMINA_API_RETRY_EXCEPTIONS,
254255
attempts=retry_attempts,
255256
timeout=None,
256257
):

greenbone/scap/cve/cli/download.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import os
77
from argparse import ArgumentParser, Namespace
88
from datetime import datetime
9+
from json import JSONDecodeError
910
from pathlib import Path
1011
from typing import Sequence
1112

1213
import httpx
1314
import shtab
1415
import stamina
16+
from httpx import HTTPError
1517
from pontos.nvd import now
1618
from pontos.nvd.api import NVDResults
1719
from pontos.nvd.cve import CVEApi
@@ -29,6 +31,7 @@
2931
CLIError,
3032
CLIRunner,
3133
)
34+
from greenbone.scap.constants import STAMINA_API_RETRY_EXCEPTIONS
3235
from greenbone.scap.cve.manager import CVEManager
3336
from greenbone.scap.db import PostgresDatabase
3437
from greenbone.scap.timer import Timer
@@ -212,7 +215,7 @@ async def _producer(
212215
with Timer() as download_timer:
213216
count = 0
214217
async for attempt in stamina.retry_context(
215-
on=httpx.HTTPError,
218+
on=STAMINA_API_RETRY_EXCEPTIONS,
216219
attempts=retry_attempts,
217220
timeout=None,
218221
):
@@ -255,7 +258,7 @@ async def download(
255258
last_modified_end_date: datetime | None,
256259
) -> None:
257260
async for attempt in stamina.retry_context(
258-
on=httpx.HTTPError,
261+
on=STAMINA_API_RETRY_EXCEPTIONS,
259262
attempts=retry_attempts,
260263
timeout=None,
261264
):

greenbone/scap/generic_cli/producer/nvd_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
DEFAULT_RETRIES,
1818
DEFAULT_VERBOSITY,
1919
)
20+
from ...constants import STAMINA_API_RETRY_EXCEPTIONS
2021

2122
from ...timer import Timer
2223
from .base import BaseScapProducer
@@ -222,7 +223,7 @@ async def fetch_initial_data(
222223
The number of expected items.
223224
"""
224225
async for attempt in stamina.retry_context(
225-
on=httpx.HTTPError,
226+
on=STAMINA_API_RETRY_EXCEPTIONS,
226227
attempts=self._retry_attempts,
227228
timeout=None,
228229
):
@@ -275,7 +276,7 @@ async def run_loop(
275276
with Timer() as download_timer:
276277
count = 0
277278
async for attempt in stamina.retry_context(
278-
on=httpx.HTTPError,
279+
on=STAMINA_API_RETRY_EXCEPTIONS,
279280
attempts=self._additional_retry_attempts,
280281
timeout=None,
281282
):

0 commit comments

Comments
 (0)