Skip to content

Commit f7fdb6b

Browse files
committed
add cleanup for thread-local storage in import tasks
1 parent a5c42e6 commit f7fdb6b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/integrations/tasks.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@shared_task(name="Import from Trakt")
1111
def import_trakt(username, user):
1212
"""Celery task for importing anime and manga data from Trakt."""
13+
try:
1314
# Set the request.user on the thread for the history record middleware to use
1415
HistoricalRecords.thread.request = type("FakeRequest", (), {"user": user})
1516

@@ -20,20 +21,27 @@ def import_trakt(username, user):
2021
num_ratings_imported,
2122
warning_message,
2223
) = trakt.importer(username, user)
24+
2325
info_message = (
2426
f"Imported {num_tv_imported} TV shows, "
2527
f"{num_movie_imported} movies, "
2628
f"{num_watchlist_imported} watchlist items, "
2729
f"and {num_ratings_imported} ratings."
2830
)
31+
2932
if warning_message:
3033
return f"{info_message} {ERROR_TITLE} {warning_message}"
3134
return info_message
35+
finally:
36+
# Clean up thread-local storage
37+
if hasattr(HistoricalRecords.thread, "request"):
38+
delattr(HistoricalRecords.thread, "request")
3239

3340

3441
@shared_task(name="Import from SIMKL")
3542
def import_simkl(token, user):
3643
"""Celery task for importing anime and manga data from SIMKL."""
44+
try:
3745
# Set the request.user on the thread for the history record middleware to use
3846
HistoricalRecords.thread.request = type("FakeRequest", (), {"user": user})
3947

@@ -49,9 +57,14 @@ def import_simkl(token, user):
4957
f"{num_movie_imported} movies, "
5058
f"and {num_anime_imported} anime."
5159
)
60+
5261
if warning_message:
5362
return f"{info_message} {ERROR_TITLE} {warning_message}"
5463
return info_message
64+
finally:
65+
# Clean up thread-local storage
66+
if hasattr(HistoricalRecords.thread, "request"):
67+
delattr(HistoricalRecords.thread, "request")
5568

5669

5770
@shared_task(name="Import from MyAnimeList")
@@ -70,19 +83,23 @@ def import_mal(username, user):
7083
@shared_task(name="Import from TMDB")
7184
def import_tmdb(file, user, status):
7285
"""Celery task for importing TMDB tv shows and movies."""
86+
try:
7387
# Set the request.user on the thread for the history record middleware to use
7488
HistoricalRecords.thread.request = type("FakeRequest", (), {"user": user})
7589

76-
try:
7790
num_tv_imported, num_movie_imported = tmdb.importer(file, user, status)
7891
except UnicodeDecodeError as error:
7992
msg = "Invalid file format. Please upload a CSV file."
8093
raise ValueError(msg) from error
8194
except KeyError as error:
8295
msg = "Error parsing TMDB CSV file."
8396
raise ValueError(msg) from error
84-
97+
else:
8598
return f"Imported {num_tv_imported} TV shows and {num_movie_imported} movies."
99+
finally:
100+
# Clean up thread-local storage
101+
if hasattr(HistoricalRecords.thread, "request"):
102+
delattr(HistoricalRecords.thread, "request")
86103

87104

88105
@shared_task(name="Import from AniList")

0 commit comments

Comments
 (0)