|
11 | 11 | from django.core import serializers |
12 | 12 | from django.core.management import call_command |
13 | 13 | from django.core.management.base import BaseCommand, CommandError, CommandParser |
14 | | -from django.db import models |
| 14 | +from django.db import models, close_old_connections |
15 | 15 |
|
16 | 16 | from extlinks.common import swift |
17 | 17 |
|
@@ -153,6 +153,8 @@ def handle(self, *args, **options): |
153 | 153 | elif subcommand == "upload": |
154 | 154 | self.upload(container=options["container"], filenames=options["filenames"]) |
155 | 155 |
|
| 156 | + close_old_connections() |
| 157 | + |
156 | 158 | def dump( |
157 | 159 | self, |
158 | 160 | start: Optional[datetime.date] = None, |
@@ -202,7 +204,7 @@ def dump( |
202 | 204 | end = (datetime.date.today() - relativedelta(years=1)).replace(day=1) |
203 | 205 |
|
204 | 206 | if not container: |
205 | | - container = os.environ.get("SWIFT_CONTAINER_AGGREGATES") |
| 207 | + container = os.environ.get("SWIFT_CONTAINER_AGGREGATES", "archive-aggregates") |
206 | 208 |
|
207 | 209 | if end: |
208 | 210 | cursor = start |
@@ -286,25 +288,32 @@ def upload(self, container: str, filenames: List[str]): |
286 | 288 | return False |
287 | 289 |
|
288 | 290 | 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 |
295 | 299 |
|
296 | | - successful, failed = swift.batch_upload_files(conn, container, filenames) |
| 300 | + successful, failed = swift.batch_upload_files(conn, container, filenames) |
297 | 301 |
|
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 | + ) |
303 | 307 |
|
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" |
307 | 315 | ) |
| 316 | + return False |
308 | 317 |
|
309 | 318 | def archive( |
310 | 319 | self, |
|
0 commit comments