Skip to content

Commit

Permalink
Use Data directory for tasks.status for easier volume mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaKamikaze committed Sep 24, 2024
1 parent 58ea178 commit 61a1f30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 0 additions & 3 deletions MoxfieldPriceScraper/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
24 changes: 17 additions & 7 deletions MoxfieldPriceScraper/Healthcheck/Healthcheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ 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();

/// <summary>
/// Initializes the status file with an empty dictionary if it doesn't exist.
/// </summary>
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));
}
}
}
Expand All @@ -29,13 +33,16 @@ public static void InitializeStatusFile()
/// <param name="status">The new status.</param>
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<TaskStatus>(File.ReadAllText(StatusFilePath));
var taskStatus = JsonConvert.DeserializeObject<TaskStatus>(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));
}
}
}
Expand All @@ -46,14 +53,17 @@ public static void UpdateTaskStatus(string taskName, string status)
/// <returns>True if tasks are still running, false otherwise.</returns>
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<TaskStatus>(File.ReadAllText(StatusFilePath));
var taskStatus = JsonConvert.DeserializeObject<TaskStatus>(File.ReadAllText(statusFilePath));
return taskStatus != null && taskStatus.Statuses.ContainsValue("running");
}
}
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 61a1f30

Please sign in to comment.