From bc57438bd1f04ee072ba1c6765266310730a2b2c Mon Sep 17 00:00:00 2001 From: johnnyunar Date: Tue, 28 Jan 2025 12:33:31 +0100 Subject: [PATCH] Update StorageHealthCheck --- health/healthchecks/common.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/health/healthchecks/common.py b/health/healthchecks/common.py index c9783fb..3dbcf71 100644 --- a/health/healthchecks/common.py +++ b/health/healthchecks/common.py @@ -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: