33from datetime import datetime , \
44 timezone
55from json import loads
6+ from json .decoder import JSONDecodeError
67from threading import Thread
78from time import sleep
89
1314
1415from backend .config import config
1516from 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
1921API_SUFFIX = '/api/v4'
2022
23+
2124class 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:
338353update_status = UpdateStatus ()
339354
340355# to be imported by others
341- container_images_cache = ContainerImageCache ()
356+ container_images_cache = ContainerImageCache ()
0 commit comments