71 lines
1.8 KiB
C#
Raw Normal View History

using Logging;
namespace AutoClient.Modes.FolderStore
2024-11-26 15:14:31 +01:00
{
public class FolderWorkDispatcher
{
2024-11-26 15:57:34 +01:00
private readonly string[] files = Array.Empty<string>();
private readonly ILog log;
2024-11-26 15:57:34 +01:00
private int index = 0;
private int busyCount = 0;
2024-11-26 15:14:31 +01:00
public FolderWorkDispatcher(ILog log, string folder)
2024-11-26 15:14:31 +01:00
{
var fs = Directory.GetFiles(folder);
2024-11-26 15:57:34 +01:00
var result = new List<string>();
2024-11-26 15:14:31 +01:00
foreach (var f in fs)
{
if (!f.ToLowerInvariant().Contains(".json"))
{
var info = new FileInfo(f);
if (info.Exists && info.Length > 1024 * 1024) // larger than 1MB
{
2024-11-26 15:57:34 +01:00
result.Add(f);
2024-11-26 15:14:31 +01:00
}
}
}
2024-11-26 15:57:34 +01:00
files = result.ToArray();
this.log = log;
2024-11-26 15:14:31 +01:00
}
2024-11-26 16:22:43 +01:00
public FileIndex GetFileToCheck()
2024-11-26 15:14:31 +01:00
{
if (busyCount > 1)
{
2024-11-27 11:09:47 +01:00
log.Log("");
log.Log("Max number of busy workers reached. Waiting until contracts are started before creating any more.");
2024-11-27 11:09:47 +01:00
log.Log("");
2024-11-27 11:12:12 +01:00
Thread.Sleep(10000);
ResetIndex();
}
2024-11-26 16:22:43 +01:00
var file = new FileIndex(files[index], index);
2024-11-26 15:57:34 +01:00
index = (index + 1) % files.Length;
return file;
2024-11-26 15:14:31 +01:00
}
2024-11-26 15:57:34 +01:00
public void ResetIndex()
2024-11-26 15:14:31 +01:00
{
2024-11-26 15:57:34 +01:00
index = 0;
busyCount = 0;
}
public void WorkerIsBusy()
{
busyCount++;
2024-11-26 15:14:31 +01:00
}
}
2024-11-26 16:22:43 +01:00
public class FileIndex
{
public FileIndex(string file, int index)
{
File = file;
Index = index;
}
public string File { get; }
public int Index { get; }
}
2024-11-26 15:14:31 +01:00
}