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

Added temp directory in use detector #497

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 18 additions & 7 deletions Node/Workers/FlowWorker.cs
Expand Up @@ -8,14 +8,15 @@
using FileFlows.ServerShared.Workers;
using FileFlows.Shared.Models;
using Jint.Native.Json;
using System.Collections.Concurrent;

namespace FileFlows.Node.Workers;


/// <summary>
/// A flow worker executes a flow and start a flow runner
/// </summary>
public class FlowWorker : Worker
public class FlowWorker : Worker, IWorkerThatUsesTempDirectories
{
/// <summary>
/// A unique identifier to identify the flow worker
Expand All @@ -25,7 +26,13 @@ public class FlowWorker : Worker
public readonly Guid Uid = Guid.NewGuid();

private readonly string _configKeyDefault = Guid.NewGuid().ToString();


public bool IsTempDirectoryInUse(string directory)
{
var tempDirectoryInUse = ExecutingRunners.Keys.Any(t => directory.EndsWith(t.ToString(), StringComparison.OrdinalIgnoreCase));
return tempDirectoryInUse;
}

/// <summary>
/// Gets if the config encryption key
/// </summary>
Expand Down Expand Up @@ -78,7 +85,10 @@ private bool GetConfigNoEncrypt(ProcessingNode node)
private static FlowWorker? Instance;

private readonly Mutex mutex = new Mutex();
private readonly List<Guid> ExecutingRunners = new ();
/// <summary>
/// Concurrent because we have the TempFileCleaner running in parallel
/// </summary>
private readonly ConcurrentDictionary<Guid, object?> ExecutingRunners = new ();

private const int DEFAULT_INTERVAL = 10;

Expand Down Expand Up @@ -506,7 +516,7 @@ private void AddExecutingRunner(Guid uid)
mutex.WaitOne();
try
{
ExecutingRunners.Add(uid);
ExecutingRunners.TryAdd(uid, null);
}
finally
{
Expand All @@ -525,8 +535,10 @@ private void RemoveExecutingRunner(Guid uid)
mutex.WaitOne();
try
{
if (ExecutingRunners.Contains(uid))
ExecutingRunners.Remove(uid);
if (ExecutingRunners.TryRemove(uid, out var _))
{

}
else
{
Logger.Instance?.ILog("Executing runner not in list: " + uid +" => " + string.Join(",", ExecutingRunners.Select(x => x.ToString())));
Expand Down Expand Up @@ -742,5 +754,4 @@ private async Task<bool> UpdateConfiguration(ProcessingNode node)
return true;

}

}
7 changes: 7 additions & 0 deletions ServerShared/IWorkerThatUsesTempDirectories.cs
@@ -0,0 +1,7 @@
namespace FileFlows.ServerShared
{
public interface IWorkerThatUsesTempDirectories
{
public bool IsTempDirectoryInUse(string directory);
}
}
10 changes: 8 additions & 2 deletions ServerShared/Workers/TempFileCleaner.cs
Expand Up @@ -8,14 +8,17 @@ namespace FileFlows.ServerShared.Workers;
public class TempFileCleaner:Worker
{
private string nodeAddress;

private readonly IWorkerThatUsesTempDirectories workerThatUsesTempDirectories;

/// <summary>
/// Constructs a temp file cleaner
/// <param name="nodeAddress">The name of the node</param>
/// <param name="workerThatUsesTempDirectories">The worker that uses temp directories, this is used to verify that a temp directory should not be removed while still in use</param>"
/// </summary>
public TempFileCleaner(string nodeAddress) : base(ScheduleType.Daily, 5)
public TempFileCleaner(string nodeAddress, IWorkerThatUsesTempDirectories workerThatUsesTempDirectories) : base(ScheduleType.Daily, 5)
{
this.nodeAddress = nodeAddress;
this.workerThatUsesTempDirectories = workerThatUsesTempDirectories;
Trigger();
}

Expand All @@ -37,6 +40,9 @@ protected sealed override void Execute()
Logger.Instance?.ILog("About to clean temporary directory: " + tempDir.FullName);
foreach (var dir in tempDir.GetDirectories())
{
if (workerThatUsesTempDirectories.IsTempDirectoryInUse(dir.Name))
continue;

if (dir.CreationTimeUtc < DateTime.UtcNow.AddDays(-1))
{
try
Expand Down