fix redundant thread catching, hopefully

This commit is contained in:
Adam R. Grey 2021-11-17 06:38:36 -05:00
parent 99acb25bca
commit 2acc40d753
4 changed files with 67 additions and 40 deletions

View File

@ -25,6 +25,7 @@ namespace director
private static readonly ConcurrentQueue<schedulable.Scheduled> workQueue = new ConcurrentQueue<schedulable.Scheduled>();
private static readonly AutoResetEvent _signal = new AutoResetEvent(false);
private const int concurrentWorkers = 5;
private static readonly ConcurrentBag<Guid> workerIds = new ConcurrentBag<Guid>();
static void Main(string[] args)
{
if (!File.Exists("appsettings.json"))
@ -147,6 +148,8 @@ namespace director
}
private static void threadwork()
{
var threadId = Guid.NewGuid();
workerIds.Add(threadId);
schedulable.Scheduled todo = null;
while (true)
{
@ -155,16 +158,24 @@ namespace director
if (!workQueue.TryDequeue(out todo)) { continue; }
Console.WriteLine($"threadwork consumes!");
Console.WriteLine(JsonConvert.SerializeObject(todo));
if (todo.HandledBy != null && workerIds.Contains(todo.HandledBy.Value))
{
Console.WriteLine($"{todo.HandledBy} already got this.");
continue;
}
todo.HandledBy = threadId;
Console.WriteLine($"signing it out, {todo.HandledBy}");
todo.Configuration = findConfig(todo.Occurrence.CalendarSourceName, todo.Occurrence._event.Summary);
if (todo.Configuration == null)
{
Console.WriteLine("configuration not found, skipping :(");
Console.WriteLine("configuration not found, doing nothing");
continue;
}
Console.WriteLine("configuration found");
var handler = new ShowHandler(todo, directorConf.workingDirectory + todo.Showtime.ToString("_yyyy-MM-dd"), () => {
var handler = new ShowHandler(todo, directorConf.workingDirectory + todo.Showtime.ToString("_yyyy-MM-dd"), () =>
{
//tf.ProduceMessage(new silver_messages.directorial.execute_command(){command = "directors_datasync", timeout = TimeSpan.Zero});
});
handler.StartHandling();
@ -181,6 +192,8 @@ namespace director
{
var configurationName = locator.EventName.Replace(eventName, locator.SchedulableConfiguration);
Console.WriteLine($"found match good enough, I guess. going to load {configurationName} for {eventName}");
if (File.Exists(configurationName))
{
try
{
return JsonConvert.DeserializeObject<schedulable.Schedulable>(File.ReadAllText(configurationName));
@ -191,6 +204,12 @@ namespace director
return null;
}
}
else
{
Console.Error.WriteLine("couldn't find that file");
return null;
}
}
}
}
//HumanCommunication.Instance.forwardToDiscord($"couldn't find suitable configuration for {eventName} within {CalendarSourceName}", HumanCommunication.LogLevel.Info);

View File

@ -180,7 +180,13 @@ namespace director
private void ChecklistComplete()
{
Console.WriteLine("\"another job well executed\" - lotus");
//TODO: delete folder. if necessary. Maybe.
todo.Done = true;
var parentDirectory = Path.GetDirectoryName(checklistFilename);
File.Delete(checklistFilename);
if ((Directory.GetFiles(parentDirectory)?.Count() ?? 0) > 0)
{
Directory.Delete(parentDirectory);
}
}
}
}

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="silvermeddlists.franz" Version="0.0.5" />
<PackageReference Include="silvermeddlists.franz" Version="0.0.7" />
<PackageReference Include="Ical.Net" Version="4.2.0" />
</ItemGroup>

View File

@ -8,5 +8,7 @@ namespace schedulable
public iCalHoopJumping.CalendarOccurrence Occurrence { get; set; }
public DateTime Showtime { get; set; }
public Schedulable Configuration { get; set; }
public Guid? HandledBy { get; set; }
public bool Done { get; set; }
}
}