Skip to content

Commit

Permalink
Update StorageHealthCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyunar committed Jan 28, 2025
1 parent 2e9c4a8 commit bc57438
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions health/healthchecks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,28 @@ class FileStorageHealthCheck(BaseHealthCheck):
def check(self, **kwargs) -> dict:
try:
# Path for the healthcheck test file
test_file = "healthcheck/healthcheck_test_file.txt"
test_dir = "healthcheck/"
test_file = "healthcheck_test_file.txt"
test_file_path = f"{test_dir}{test_file}"
content = b"healthcheck"

# Ensure the "healthcheck" directory exists (for local file systems)
if not default_storage.exists("healthcheck/"):
if not default_storage.exists(test_dir):
default_storage.save(
"healthcheck/.keep", ContentFile(b"")
f"{test_dir}.keep", ContentFile(b"")
) # Create a placeholder file

# Write the test file
with default_storage.open(test_file, "wb") as f:
with default_storage.open(test_file_path, "wb") as f:
f.write(content)

# Verify the file exists
if not default_storage.exists(test_file):
dir_contents = default_storage.listdir(test_dir)
if not test_file in dir_contents[1]:
return {"status": "error", "details": "File not found after creation."}

# Clean up: Delete the test file
default_storage.delete(test_file)
default_storage.delete(test_file_path)

return {"status": "ok"}
except Exception as e:
Expand Down

0 comments on commit bc57438

Please sign in to comment.