@@ -80,6 +80,7 @@ def __init__(self, library, path, layer):
80
80
self .App .TaskService .schedule (self ._periodic_pull (None ))
81
81
self .App .PubSub .subscribe ("Application.tick/60!" , self ._periodic_pull )
82
82
83
+
83
84
async def _periodic_pull (self , event_name ):
84
85
"""
85
86
Changes in remote repository are being pulled every minute.
@@ -103,58 +104,67 @@ async def _periodic_pull(self, event_name):
103
104
104
105
url = random .choice (self .URLs )
105
106
106
- async with aiohttp .ClientSession () as session :
107
- async with session .get (url , headers = headers ) as response :
108
-
109
- if response .status == 200 : # The request indicates a new version that we don't have yet
110
-
111
- etag_incoming = response .headers .get ('ETag' )
112
-
113
- # Download new version
114
- newtarfname = os .path .join (self .RootPath , "new.tar.xz" )
115
- with open (newtarfname , 'wb' ) as ftmp :
116
- while True :
117
- chunk = await response .content .read (16 * 1024 )
118
- if not chunk :
119
- break
120
- ftmp .write (chunk )
121
-
122
- # Extract the contents to the temporary directory
123
- temp_extract_dir = os .path .join (
124
- self .RootPath ,
125
- "new"
126
- )
127
-
128
- # Remove temp_extract_dir if it exists (from the last, failed run)
129
- if os .path .exists (temp_extract_dir ):
130
- shutil .rmtree (temp_extract_dir )
131
-
132
- # Extract the archive into the temp_extract_dir
133
- with tarfile .open (newtarfname , mode = 'r:xz' ) as tar :
134
- tar .extractall (temp_extract_dir )
135
-
136
- # Synchronize the temp_extract_dir into the library
137
- synchronize_dirs (self .RepoPath , temp_extract_dir )
138
- await self ._set_ready ()
139
-
140
- if etag_incoming is not None :
141
- with open (etag_fname , 'w' ) as f :
142
- f .write (etag_incoming )
143
-
144
- # Remove temp_extract_dir
145
- if os .path .exists (temp_extract_dir ):
146
- shutil .rmtree (temp_extract_dir )
147
-
148
- # Remove newtarfname
149
- if os .path .exists (newtarfname ):
150
- os .remove (newtarfname )
151
-
152
- elif response .status == 304 :
153
- # The repository has not changed ...
154
- await self ._set_ready ()
155
-
156
- else :
157
- L .exception ("Failed to download the library." , struct_data = {"url" : url , 'status' : response .status })
107
+ try :
108
+ async with aiohttp .ClientSession () as session :
109
+ async with session .get (url , headers = headers ) as response :
110
+
111
+ if response .status == 200 : # The request indicates a new version that we don't have yet
112
+
113
+ etag_incoming = response .headers .get ('ETag' )
114
+
115
+ # Download new version
116
+ newtarfname = os .path .join (self .RootPath , "new.tar.xz" )
117
+ with open (newtarfname , 'wb' ) as ftmp :
118
+ while True :
119
+ chunk = await response .content .read (16 * 1024 )
120
+ if not chunk :
121
+ break
122
+ ftmp .write (chunk )
123
+
124
+ # Extract the contents to the temporary directory
125
+ temp_extract_dir = os .path .join (
126
+ self .RootPath ,
127
+ "new"
128
+ )
129
+
130
+ # Remove temp_extract_dir if it exists (from the last, failed run)
131
+ if os .path .exists (temp_extract_dir ):
132
+ shutil .rmtree (temp_extract_dir )
133
+
134
+ # Extract the archive into the temp_extract_dir
135
+ with tarfile .open (newtarfname , mode = 'r:xz' ) as tar :
136
+ tar .extractall (temp_extract_dir )
137
+
138
+ # Synchronize the temp_extract_dir into the library
139
+ synchronize_dirs (self .RepoPath , temp_extract_dir )
140
+ if not self .IsReady :
141
+ await self ._set_ready ()
142
+
143
+ if etag_incoming is not None :
144
+ with open (etag_fname , 'w' ) as f :
145
+ f .write (etag_incoming )
146
+
147
+ # Remove temp_extract_dir
148
+ if os .path .exists (temp_extract_dir ):
149
+ shutil .rmtree (temp_extract_dir )
150
+
151
+ # Remove newtarfname
152
+ if os .path .exists (newtarfname ):
153
+ os .remove (newtarfname )
154
+
155
+ elif response .status == 304 :
156
+ # The repository has not changed ...
157
+ if not self .IsReady :
158
+ await self ._set_ready ()
159
+
160
+ else :
161
+ L .error ("Failed to download the library." , struct_data = {"url" : url , 'status' : response .status })
162
+
163
+ except aiohttp .ClientError as e :
164
+ L .error ("Failed to download the library." , struct_data = {"url" : url , 'exception' : e })
165
+
166
+ except asyncio .TimeoutError as e :
167
+ L .error ("Failed to download the library." , struct_data = {"url" : url , 'exception' : e })
158
168
159
169
160
170
async def subscribe (self , path ):
0 commit comments