Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Data directory for tasks.status for easier volume mapping #8

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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