Skip to content

Commit c916492

Browse files
author
Benjamin Bach
committed
Raise DownloadCancelled when download has been cancelled and stop the outer loop
1 parent 89281be commit c916492

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

kalite/i18n/base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,6 @@ def get_content_pack_url(lang):
401401
def download_content_pack(fobj, lang, callback=None):
402402
"""Given a file object where the content pack lang will be stored, return a
403403
zipfile object pointing to the content pack.
404-
405-
If minimal is set to True, append the "-minimal" flag when downloading the
406-
contentpack.
407-
408404
"""
409405
url = get_content_pack_url(lang)
410406

kalite/packages/bundled/fle_utils/internet/download.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77

88
from requests.packages.urllib3.util.retry import Retry
9-
from requests.exceptions import HTTPError
9+
from kalite.updates.settings import DOWNLOAD_SOCKET_TIMEOUT
1010

1111

12-
socket.setdefaulttimeout(20)
12+
socket.setdefaulttimeout(DOWNLOAD_SOCKET_TIMEOUT)
1313

1414

1515
logger = logging.getLogger(__name__)
@@ -58,16 +58,21 @@ def download_file(url, dst=None, callback=None, fp=None, max_retries=5):
5858
# Assuming the KA Lite version is included in user agent because of an
5959
# intention to create stats on learningequality.org
6060
from kalite.version import user_agent
61+
62+
# Notice that we deliberate aren't using the ``timeout`` kwarg here, we
63+
# will allow the stream to hang forever when a connection is disrupted
64+
# but a download has already started. This is to not have to write "resume"
65+
# logic on top of our retry logic.
6166
response = s.get(
6267
url,
6368
allow_redirects=True,
6469
stream=True,
70+
# timeout=DOWNLOAD_SOCKET_TIMEOUT,
6571
headers={"user-agent": user_agent()}
6672
)
6773

6874
response.raise_for_status()
6975

70-
7176
# Don't do this things until passed the raise_for_status() point
7277
# If not called with a file pointer or destination, create a new temporary
7378
# file
@@ -95,6 +100,7 @@ def download_file(url, dst=None, callback=None, fp=None, max_retries=5):
95100
fp.seek(0)
96101

97102
# Verify file existence
103+
dst = fp.name or None
98104
if dst:
99105
if os.path.isfile(dst):
100106
size_on_disk = os.path.getsize(dst)

kalite/updates/management/commands/videodownload.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,16 @@ def handle(self, *args, **options):
152152
failcnt=DOWNLOAD_MAX_RETRIES,
153153
retries=retries,
154154
)
155-
self.update_stage(
156-
stage_name=video.get("youtube_id"),
157-
stage_percent=0.,
158-
notes=msg
159-
)
155+
try:
156+
self.update_stage(
157+
stage_name=video.get("youtube_id"),
158+
stage_percent=0.,
159+
notes=msg
160+
)
161+
except AssertionError:
162+
# Raised by update_stage when the video
163+
# download job has ended
164+
raise DownloadCancelled()
160165
logger.info(msg)
161166
time.sleep(30)
162167
continue
@@ -171,9 +176,9 @@ def handle(self, *args, **options):
171176
annotate_content_models_by_youtube_id(youtube_ids=[video.get("youtube_id")], language=video.get("language"))
172177

173178
except DownloadCancelled:
174-
# Cancellation event
175179
video_queue.clear()
176180
failed_youtube_ids.append(video.get("youtube_id"))
181+
break
177182

178183
except (HTTPError, Exception) as e:
179184
# Rather than getting stuck on one video,

kalite/updates/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
#: backoff rate defined as:
1818
#: {backoff factor} * (2 ^ ({number of total retries} - 1))
1919
DOWNLOAD_MAX_RETRIES = getattr(settings, "KALITE_DOWNLOAD_RETRIES", 5)
20+
21+
DOWNLOAD_SOCKET_TIMEOUT = getattr(settings, "KALITE_DOWNLOAD_TIMEOUT", 20)

kalite/updates/static/js/updates/bundle_modules/update_videos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ $(function() {
204204
if(server_is_online){
205205
base.updatesStart("videodownload", 5000, video_callbacks);
206206
} else {
207+
base.updatesStart("videodownload", 10000, video_callbacks);
207208
messages.show_message("error", gettext("Could not connect to the central server; videos cannot be downloaded at this time."));
208209
}
209210
});

kalite/updates/templates/updates/update_videos.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h4 class="videos">
7373

7474
<div>
7575
<!-- note: id must match the "updates" app process name -->
76-
<div id="videodownload-progressbar">
76+
<div id="videodownload-progressbar" style="margin-top: 20px">
7777
{% include "updates/progress-bar.html" %}
7878
</div>
7979

0 commit comments

Comments
 (0)