Skip to content

Commit 8ea5bae

Browse files
authored
Merge pull request #449 from WikipediaLibrary/kgraessle/refactor-crons-and-close-db-connections
Match try/except block structure that we're using for linkevents_archive
2 parents e5552a0 + af06242 commit 8ea5bae

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

extlinks/aggregates/management/helpers/aggregate_archive_command.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.core import serializers
1212
from django.core.management import call_command
1313
from django.core.management.base import BaseCommand, CommandError, CommandParser
14-
from django.db import models
14+
from django.db import models, close_old_connections
1515

1616
from extlinks.common import swift
1717

@@ -153,6 +153,8 @@ def handle(self, *args, **options):
153153
elif subcommand == "upload":
154154
self.upload(container=options["container"], filenames=options["filenames"])
155155

156+
close_old_connections()
157+
156158
def dump(
157159
self,
158160
start: Optional[datetime.date] = None,
@@ -202,7 +204,7 @@ def dump(
202204
end = (datetime.date.today() - relativedelta(years=1)).replace(day=1)
203205

204206
if not container:
205-
container = os.environ.get("SWIFT_CONTAINER_AGGREGATES")
207+
container = os.environ.get("SWIFT_CONTAINER_AGGREGATES", "archive-aggregates")
206208

207209
if end:
208210
cursor = start
@@ -286,25 +288,32 @@ def upload(self, container: str, filenames: List[str]):
286288
return False
287289

288290
try:
289-
was_created = swift.ensure_container_exists(conn, container)
290-
if was_created:
291-
self.log_msg(f"Created new container: {container}")
292-
except RuntimeError as e:
293-
self.log_msg(str(e), level="error")
294-
return False
291+
# Ensure the container exists before uploading.
292+
try:
293+
was_created = swift.ensure_container_exists(conn, container)
294+
if was_created:
295+
self.log_msg(f"Created new container: {container}")
296+
except RuntimeError as e:
297+
self.log_msg(str(e), level="error")
298+
return False
295299

296-
successful, failed = swift.batch_upload_files(conn, container, filenames)
300+
successful, failed = swift.batch_upload_files(conn, container, filenames)
297301

298-
self.log_msg(
299-
"Uploaded %d/%d archives to object storage",
300-
len(successful),
301-
len(filenames),
302-
)
302+
self.log_msg(
303+
"Uploaded %d/%d archives to object storage",
304+
len(successful),
305+
len(filenames),
306+
)
303307

304-
if len(failed) > 0:
305-
raise CommandError(
306-
f"The following {failed} archives failed to upload: {','.join(failed)}"
308+
if len(failed) > 0:
309+
raise CommandError(
310+
f"The following {failed} archives failed to upload: {','.join(failed)}"
311+
)
312+
except Exception as e:
313+
self.log_msg(
314+
f"Failed to upload to Swift: {e}", level="error"
307315
)
316+
return False
308317

309318
def archive(
310319
self,

0 commit comments

Comments
 (0)