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 ConcurrentQueue<schedulable.Scheduled> workQueue = new ConcurrentQueue<schedulable.Scheduled>();
private static readonly AutoResetEvent _signal = new AutoResetEvent(false); private static readonly AutoResetEvent _signal = new AutoResetEvent(false);
private const int concurrentWorkers = 5; private const int concurrentWorkers = 5;
private static readonly ConcurrentBag<Guid> workerIds = new ConcurrentBag<Guid>();
static void Main(string[] args) static void Main(string[] args)
{ {
if (!File.Exists("appsettings.json")) if (!File.Exists("appsettings.json"))
@ -96,24 +97,24 @@ namespace director
} }
} }
Console.WriteLine("calendars checked"); Console.WriteLine("calendars checked");
// #if (DEBUG) // #if (DEBUG)
// //if(true) // //if(true)
// { // {
// Console.WriteLine("debug test"); // Console.WriteLine("debug test");
// var psuedo = new schedulable.Scheduled(); // var psuedo = new schedulable.Scheduled();
// psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30); // psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30);
// // foreach(var a in scratch.agenda){ // // foreach(var a in scratch.agenda){
// // Console.WriteLine(a.Occurrence._event.Summary); // // Console.WriteLine(a.Occurrence._event.Summary);
// // } // // }
// var partiallyCopiable = scratch.agenda?.First(a => a.Occurrence._event.Summary == "Good Morning, Phyrexia (in the morning)!"); // var partiallyCopiable = scratch.agenda?.First(a => a.Occurrence._event.Summary == "Good Morning, Phyrexia (in the morning)!");
// psuedo.Occurrence = partiallyCopiable.Occurrence; // psuedo.Occurrence = partiallyCopiable.Occurrence;
// psuedo.Occurrence.OccurrenceStart = psuedo.Showtime; // psuedo.Occurrence.OccurrenceStart = psuedo.Showtime;
// psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime; // psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime;
// workQueue.Enqueue(psuedo); // workQueue.Enqueue(psuedo);
// _signal.Set(); // _signal.Set();
// } // }
// #endif // #endif
} }
private static async Task checkCalendar(string calendarUri) private static async Task checkCalendar(string calendarUri)
@ -147,6 +148,8 @@ namespace director
} }
private static void threadwork() private static void threadwork()
{ {
var threadId = Guid.NewGuid();
workerIds.Add(threadId);
schedulable.Scheduled todo = null; schedulable.Scheduled todo = null;
while (true) while (true)
{ {
@ -155,16 +158,24 @@ namespace director
if (!workQueue.TryDequeue(out todo)) { continue; } if (!workQueue.TryDequeue(out todo)) { continue; }
Console.WriteLine($"threadwork consumes!"); Console.WriteLine($"threadwork consumes!");
Console.WriteLine(JsonConvert.SerializeObject(todo)); 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); todo.Configuration = findConfig(todo.Occurrence.CalendarSourceName, todo.Occurrence._event.Summary);
if (todo.Configuration == null) if (todo.Configuration == null)
{ {
Console.WriteLine("configuration not found, skipping :("); Console.WriteLine("configuration not found, doing nothing");
continue; continue;
} }
Console.WriteLine("configuration found"); 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}); //tf.ProduceMessage(new silver_messages.directorial.execute_command(){command = "directors_datasync", timeout = TimeSpan.Zero});
}); });
handler.StartHandling(); handler.StartHandling();
@ -181,6 +192,8 @@ namespace director
{ {
var configurationName = locator.EventName.Replace(eventName, locator.SchedulableConfiguration); var configurationName = locator.EventName.Replace(eventName, locator.SchedulableConfiguration);
Console.WriteLine($"found match good enough, I guess. going to load {configurationName} for {eventName}"); Console.WriteLine($"found match good enough, I guess. going to load {configurationName} for {eventName}");
if (File.Exists(configurationName))
{
try try
{ {
return JsonConvert.DeserializeObject<schedulable.Schedulable>(File.ReadAllText(configurationName)); return JsonConvert.DeserializeObject<schedulable.Schedulable>(File.ReadAllText(configurationName));
@ -191,6 +204,12 @@ namespace director
return null; 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); //HumanCommunication.Instance.forwardToDiscord($"couldn't find suitable configuration for {eventName} within {CalendarSourceName}", HumanCommunication.LogLevel.Info);

View File

@ -60,7 +60,7 @@ namespace director
Directory.CreateDirectory(outputPath); Directory.CreateDirectory(outputPath);
checklistFilename = $"checklist ({todo.Occurrence._event.Summary}).md"; checklistFilename = $"checklist ({todo.Occurrence._event.Summary}).md";
var invalidFilenames = System.IO.Path.GetInvalidFileNameChars().Concat(new char[] {'(', ')', ':'}); var invalidFilenames = System.IO.Path.GetInvalidFileNameChars().Concat(new char[] { '(', ')', ':' });
foreach (char c in invalidFilenames) foreach (char c in invalidFilenames)
{ {
checklistFilename = checklistFilename.Replace(c, '_'); checklistFilename = checklistFilename.Replace(c, '_');
@ -81,7 +81,7 @@ namespace director
{ {
if (line.type != LineType.text) if (line.type != LineType.text)
{ {
if(prevLine != null && prevLine.type == LineType.text) if (prevLine != null && prevLine.type == LineType.text)
{ {
text.AppendLine(); text.AppendLine();
} }
@ -180,7 +180,13 @@ namespace director
private void ChecklistComplete() private void ChecklistComplete()
{ {
Console.WriteLine("\"another job well executed\" - lotus"); 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> </PropertyGroup>
<ItemGroup> <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" /> <PackageReference Include="Ical.Net" Version="4.2.0" />
</ItemGroup> </ItemGroup>

View File

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