Skip to content

Commit 579cda7

Browse files
committed
updates and improved stability
1 parent 74767b0 commit 579cda7

6 files changed

Lines changed: 47 additions & 15 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,4 @@ dmypy.json
129129
.pyre/
130130

131131
# local config
132-
config.yaml
132+
config.yaml

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.11-slim
1+
FROM python:3.13-slim
22

33
RUN apt update
44
RUN apt -y upgrade

backend/collect.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime, \
44
timezone
55
from json import loads
6+
from json.decoder import JSONDecodeError
67
from threading import Thread
78
from time import sleep
89

@@ -13,15 +14,18 @@
1314

1415
from backend.config import config
1516
from backend.connection import gitlab_session
16-
from backend.helpers import plural_or_not
17+
from backend.helpers import plural_or_not, \
18+
exit
1719

1820
# for now all requested URLs start with this suffix
1921
API_SUFFIX = '/api/v4'
2022

23+
2124
class ContainerImageCache(dict):
2225
"""
2326
dict-based class to act as cache
2427
"""
28+
2529
def __init__(self):
2630
super().__init__()
2731
# actual container repositories cache
@@ -44,7 +48,6 @@ def update_cache(self, container_images):
4448
self.container_images.update(container_images)
4549
self.create_index_by_name()
4650

47-
4851
def create_index_by_name(self):
4952
"""
5053
allow to access container repositories by their name to make them searchable
@@ -58,14 +61,18 @@ class CollectorThread(Thread):
5861
"""
5962
collects info from Gitlab at certain interval
6063
"""
64+
6165
def __init__(self):
6266
Thread.__init__(self, name='Collector')
6367

6468
def run(self):
6569
global container_images_cache
6670
global update_status
6771
while True:
68-
container_images_cache.update_cache(self.collect_container_images())
72+
try:
73+
container_images_cache.update_cache(self.collect_container_images())
74+
except JSONDecodeError as exception:
75+
print(f'Exception in collector thread: {exception}')
6976
sleep(config.update_interval)
7077

7178
def collect_container_images(self):
@@ -76,7 +83,7 @@ def collect_container_images(self):
7683
# to speedup development and avoid waiting for Gitlab response dump the latter
7784
if config.load_data:
7885
import pickle
79-
with open('container_images_dump.pickle','rb') as pickle_file:
86+
with open('container_images_dump.pickle', 'rb') as pickle_file:
8087
container_images = pickle.load(pickle_file)
8188
else:
8289
# dict for storage of container image info
@@ -120,7 +127,7 @@ def collect_container_images(self):
120127
# to speedup development and avoid waiting for Gitlab response dump the latter
121128
if config.dump_data:
122129
import pickle
123-
with open('container_images_dump.pickle','wb') as pickle_file:
130+
with open('container_images_dump.pickle', 'wb') as pickle_file:
124131
pickle.dump(container_images, pickle_file)
125132

126133
# make update progress bar unnecessary
@@ -148,6 +155,13 @@ def collect_projects() -> list:
148155
projects_total_pages = int(response.headers.get('x-total-pages'))
149156
projects_page += 1
150157
projects_list += loads(response.text)
158+
elif response.status_code == 401:
159+
# when token is unauthorized exit immediately
160+
exit(f'status_code: {response.status_code} text: {response.text}')
161+
else:
162+
print(f'status_code: {response.status_code} text: {response.text}')
163+
# try agin after a short nap
164+
sleep(20)
151165

152166
for project in projects_list:
153167
# fix None project description
@@ -236,7 +250,8 @@ def collect_project_container_images_tags_humanize(container_images: dict) -> di
236250
# add human readable tag image size
237251
tag['total_size_human_readable'] = '{:.2MiB}'.format(DataSize(tag['total_size']))
238252
# add human readable tag creation date
239-
tag['created_at_human_readable'] = dateutil_parser.parse(tag['created_at']).strftime('%Y-%m-%d %H:%M:%S')
253+
tag['created_at_human_readable'] = dateutil_parser.parse(tag['created_at']).strftime(
254+
'%Y-%m-%d %H:%M:%S')
240255

241256
# get age of a container image
242257
# relativedelta adds all other units like 'years=0' which makes it better comparable
@@ -338,4 +353,4 @@ class UpdateStatus:
338353
update_status = UpdateStatus()
339354

340355
# to be imported by others
341-
container_images_cache = ContainerImageCache()
356+
container_images_cache = ContainerImageCache()

backend/connection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from requests import Session
1+
from httpx import Client
22

33
from backend.config import config
44

55
# gitlab_session to be used to connect to GitLab
6-
gitlab_session = Session()
6+
gitlab_session = Client(follow_redirects=True)
77
gitlab_session.headers.update({'PRIVATE-TOKEN': config.api.token})
88

99
# disable TLS verification if configured
10-
if config.api.get('verify') == False:
11-
gitlab_session.verify = False
10+
if config.api.get('verify', True) == False:
11+
gitlab_session = Client(follow_redirects=True, verify=False)
12+
else:
13+
gitlab_session = Client(follow_redirects=True)
14+
15+
gitlab_session.headers.update({'PRIVATE-TOKEN': config.api.token})

backend/helpers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from os import _exit
2+
13
def plural_or_not(count: int, word: str) -> str:
24
"""
35
decide if a word has to be in plural because its appears as multiple
@@ -8,4 +10,15 @@ def plural_or_not(count: int, word: str) -> str:
810
if count > 1:
911
return f'{count} {word}s'
1012
else:
11-
return f'{count} {word}'
13+
return f'{count} {word}'
14+
15+
16+
def exit(message='', code=1):
17+
"""
18+
exit with message and code
19+
:param message:
20+
:param code:
21+
:return:
22+
"""
23+
print(message)
24+
_exit(code)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
datasize
22
flask
3+
httpx
34
markdown
45
munch
56
pyaml
67
python-dateutil
7-
requests

0 commit comments

Comments
 (0)