Skip to content

Commit

Permalink
fix delete_project_files_from_s3
Browse files Browse the repository at this point in the history
  • Loading branch information
habibasseiss committed Dec 21, 2024
1 parent a0a66c2 commit e927ef6
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,29 @@ class Project:
# Set up event listener for Project deletion
@event.listens_for(Project, 'before_delete')
def delete_project_files_from_s3(mapper, connection, target: Project):
# We need to run the async function in a new event loop
for file in target.files:
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(delete_file_from_s3(file.path))
except Exception as e:
# Log any other unexpected errors
logger.error(
f'Failed to delete file {file.path} from S3: {str(e)}'
)
finally:
loop.close()
# Create a coroutine that deletes all files
async def delete_all_files():
for file in target.files:
try:
await delete_file_from_s3(file.path)
except Exception as e:
# Log any other unexpected errors
logger.error(
f'Failed to delete file {file.path} from S3: {str(e)}'
)

try:
# Try to get the current event loop
loop = asyncio.get_event_loop()

# If we're already in an event loop, run the coroutine directly
if loop.is_running():
loop.create_task(delete_all_files())
else:
# If we're not in an event loop, run it to completion
loop.run_until_complete(delete_all_files())
except Exception as e:
logger.error(f'Failed to delete files from S3: {str(e)}')


@table_registry.mapped_as_dataclass
Expand Down

0 comments on commit e927ef6

Please sign in to comment.