Revize 957aac06
Přidáno uživatelem Jakub Šilhavý před asi 3 roky(ů)
client/api_client.py | ||
---|---|---|
5 | 5 |
from diskcache import Deque |
6 | 6 |
from requests import HTTPError, ConnectionError |
7 | 7 |
|
8 |
from config_manager import server_url, server_port, server_endpoint, cache_dir, cache_retry_period_seconds, cache_max_retries, cache_max_entries |
|
8 |
from config_manager import server_url, server_port, server_endpoint, cache_dir, \ |
|
9 |
cache_retry_period_seconds, cache_max_retries, cache_max_entries |
|
9 | 10 |
|
10 | 11 |
|
11 | 12 |
_cache = Deque(directory=cache_dir) |
... | ... | |
19 | 20 |
logging.info(f"response text: {response.text}") |
20 | 21 |
except ConnectionError: |
21 | 22 |
logging.warning(f"sending payload = {payload} to {_uri} failed") |
23 |
_cache_failed_payload(payload) |
|
22 | 24 |
except HTTPError as error: |
23 | 25 |
logging.error(f"HTTP Error ({_uri}) payload = {payload}, {error}") |
26 |
_cache_failed_payload(payload) |
|
27 |
|
|
28 |
|
|
29 |
def _cache_failed_payload(payload: dict): |
|
30 |
if len(_cache) >= cache_max_entries: |
|
31 |
oldest_payload = _cache.pop() |
|
32 |
logging.warning(f"cache is full - discarding payload = {oldest_payload}") |
|
33 |
|
|
34 |
logging.info(f"adding payload = {payload} into cache") |
|
35 |
_cache.append(payload) |
|
24 | 36 |
|
25 | 37 |
|
26 | 38 |
def api_client_run(): |
27 | 39 |
while True: |
40 |
retries = min(cache_max_retries, len(_cache)) |
|
41 |
logging.info(f"emptying the cache ({retries} records)") |
|
42 |
for _ in range(0, retries): |
|
43 |
payload = _cache.pop() |
|
44 |
send_data(payload) |
|
28 | 45 |
sleep(cache_retry_period_seconds) |
Také k dispozici: Unified diff
re #9359 Implemented storing/retrieving payloads into/from a diskcache