From 61a1f30bdc4a87663f4545f7b05e52ba99a8eff4 Mon Sep 17 00:00:00 2001 From: KarmaKamikaze Date: Tue, 24 Sep 2024 16:51:28 +0200 Subject: [PATCH] Use Data directory for tasks.status for easier volume mapping --- MoxfieldPriceScraper/Dockerfile | 3 --- .../Healthcheck/Healthcheck.cs | 24 +++++++++++++------ docker-compose.yml | 1 - 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/MoxfieldPriceScraper/Dockerfile b/MoxfieldPriceScraper/Dockerfile index a1408f3..75bbc1b 100644 --- a/MoxfieldPriceScraper/Dockerfile +++ b/MoxfieldPriceScraper/Dockerfile @@ -33,9 +33,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -# Create the tasks.status file -RUN touch /app/tasks.status - COPY --from=build-env /app/out . HEALTHCHECK --interval=5m --timeout=1m --start-period=30s --retries=3 CMD ["dotnet", "MoxfieldPriceScraper.dll", "healthcheck"] diff --git a/MoxfieldPriceScraper/Healthcheck/Healthcheck.cs b/MoxfieldPriceScraper/Healthcheck/Healthcheck.cs index c59b155..3622b27 100644 --- a/MoxfieldPriceScraper/Healthcheck/Healthcheck.cs +++ b/MoxfieldPriceScraper/Healthcheck/Healthcheck.cs @@ -4,7 +4,7 @@ namespace MoxfieldPriceScraper.Healthcheck; public static class Healthcheck { - private const string StatusFilePath = "tasks.status"; + private const string StatusFile = "tasks.status"; private static readonly object FileLock = new object(); /// @@ -12,12 +12,16 @@ public static class Healthcheck /// public static void InitializeStatusFile() { + var dataDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Data"); + Directory.CreateDirectory(dataDirectory); + var statusFilePath = Path.Combine(dataDirectory, StatusFile); + lock (FileLock) { - if (!File.Exists(StatusFilePath)) + if (!File.Exists(statusFilePath)) { var initialStatus = new TaskStatus(); - File.WriteAllText(StatusFilePath, JsonConvert.SerializeObject(initialStatus, Formatting.Indented)); + File.WriteAllText(statusFilePath, JsonConvert.SerializeObject(initialStatus, Formatting.Indented)); } } } @@ -29,13 +33,16 @@ public static void InitializeStatusFile() /// The new status. public static void UpdateTaskStatus(string taskName, string status) { + var dataDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Data"); + var statusFilePath = Path.Combine(dataDirectory, StatusFile); + lock (FileLock) { - var taskStatus = JsonConvert.DeserializeObject(File.ReadAllText(StatusFilePath)); + var taskStatus = JsonConvert.DeserializeObject(File.ReadAllText(statusFilePath)); if (taskStatus != null) { taskStatus.Statuses[taskName] = status; - File.WriteAllText(StatusFilePath, JsonConvert.SerializeObject(taskStatus, Formatting.Indented)); + File.WriteAllText(statusFilePath, JsonConvert.SerializeObject(taskStatus, Formatting.Indented)); } } } @@ -46,14 +53,17 @@ public static void UpdateTaskStatus(string taskName, string status) /// True if tasks are still running, false otherwise. public static bool AreTasksRunning() { + var dataDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Data"); + var statusFilePath = Path.Combine(dataDirectory, StatusFile); + lock (FileLock) { - if (!File.Exists(StatusFilePath)) + if (!File.Exists(statusFilePath)) { return false; } - var taskStatus = JsonConvert.DeserializeObject(File.ReadAllText(StatusFilePath)); + var taskStatus = JsonConvert.DeserializeObject(File.ReadAllText(statusFilePath)); return taskStatus != null && taskStatus.Statuses.ContainsValue("running"); } } diff --git a/docker-compose.yml b/docker-compose.yml index a91fdc6..67f1932 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,6 @@ volumes: - ${PWD}/Data:/app/Data - ${PWD}/Logs:/app/Logs - - ${PWD}/tasks.status:/app/tasks.status labels: - "com.centurylinklabs.watchtower.enable=true"