From 2acc40d7533313fb1bafdc30ac22e8a134e70c97 Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Wed, 17 Nov 2021 06:38:36 -0500 Subject: [PATCH] fix redundant thread catching, hopefully --- Program.cs | 69 +++++++++++++++++++++++++--------------- ShowHandler.cs | 34 ++++++++++++-------- director.csproj | 2 +- schedulable/Scheduled.cs | 2 ++ 4 files changed, 67 insertions(+), 40 deletions(-) diff --git a/Program.cs b/Program.cs index 83d3f9b..5312616 100644 --- a/Program.cs +++ b/Program.cs @@ -25,6 +25,7 @@ namespace director private static readonly ConcurrentQueue workQueue = new ConcurrentQueue(); private static readonly AutoResetEvent _signal = new AutoResetEvent(false); private const int concurrentWorkers = 5; + private static readonly ConcurrentBag workerIds = new ConcurrentBag(); static void Main(string[] args) { if (!File.Exists("appsettings.json")) @@ -96,24 +97,24 @@ namespace director } } Console.WriteLine("calendars checked"); -// #if (DEBUG) -// //if(true) -// { -// Console.WriteLine("debug test"); + // #if (DEBUG) + // //if(true) + // { + // Console.WriteLine("debug test"); -// var psuedo = new schedulable.Scheduled(); -// psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30); -// // foreach(var a in scratch.agenda){ -// // Console.WriteLine(a.Occurrence._event.Summary); -// // } -// var partiallyCopiable = scratch.agenda?.First(a => a.Occurrence._event.Summary == "Good Morning, Phyrexia (in the morning)!"); -// psuedo.Occurrence = partiallyCopiable.Occurrence; -// psuedo.Occurrence.OccurrenceStart = psuedo.Showtime; -// psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime; -// workQueue.Enqueue(psuedo); -// _signal.Set(); -// } -// #endif + // var psuedo = new schedulable.Scheduled(); + // psuedo.Showtime = DateTime.Now + TimeSpan.FromMinutes(30); + // // foreach(var a in scratch.agenda){ + // // Console.WriteLine(a.Occurrence._event.Summary); + // // } + // var partiallyCopiable = scratch.agenda?.First(a => a.Occurrence._event.Summary == "Good Morning, Phyrexia (in the morning)!"); + // psuedo.Occurrence = partiallyCopiable.Occurrence; + // psuedo.Occurrence.OccurrenceStart = psuedo.Showtime; + // psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime; + // workQueue.Enqueue(psuedo); + // _signal.Set(); + // } + // #endif } private static async Task checkCalendar(string calendarUri) @@ -122,7 +123,7 @@ namespace director //it likes to throw an error saying "this is the webDAV interface, use webDAV" at my webDAV client, stopping me from using webDAV. Console.WriteLine(calendarUri); var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export"); - + lock (scratch) { var calName = iCalHoopJumping.LoadCalendar(calString); @@ -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,13 +192,21 @@ namespace director { var configurationName = locator.EventName.Replace(eventName, locator.SchedulableConfiguration); Console.WriteLine($"found match good enough, I guess. going to load {configurationName} for {eventName}"); - try + if (File.Exists(configurationName)) { - return JsonConvert.DeserializeObject(File.ReadAllText(configurationName)); + try + { + return JsonConvert.DeserializeObject(File.ReadAllText(configurationName)); + } + catch (Exception e) + { + Console.Error.WriteLine($"error btw. not sure who's not throwing one. {e.Message}"); + return null; + } } - catch (Exception e) + else { - Console.Error.WriteLine($"error btw. not sure who's not throwing one. {e.Message}"); + Console.Error.WriteLine("couldn't find that file"); return null; } } diff --git a/ShowHandler.cs b/ShowHandler.cs index 89ea879..f2c21e8 100644 --- a/ShowHandler.cs +++ b/ShowHandler.cs @@ -60,7 +60,7 @@ namespace director Directory.CreateDirectory(outputPath); 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) { checklistFilename = checklistFilename.Replace(c, '_'); @@ -81,7 +81,7 @@ namespace director { if (line.type != LineType.text) { - if(prevLine != null && prevLine.type == LineType.text) + if (prevLine != null && prevLine.type == LineType.text) { text.AppendLine(); } @@ -163,24 +163,30 @@ namespace director Console.WriteLine("actual showtime tho"); markLineDone(fileLineIndex, fileLines); break; - // case LineType.await_signal: - // //mark it if I have the signal, but if not, can't do anything but wait - // if (signalsReceived[targetItem.cmd]) - // { - // markLineDone(fileLineIndex, fileLines); - // } - // else - // { - // _signal.WaitOne(new TimeSpan(0, 2, 0)); - // } - // break; + // case LineType.await_signal: + // //mark it if I have the signal, but if not, can't do anything but wait + // if (signalsReceived[targetItem.cmd]) + // { + // markLineDone(fileLineIndex, fileLines); + // } + // else + // { + // _signal.WaitOne(new TimeSpan(0, 2, 0)); + // } + // break; } } } 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); + } } } } \ No newline at end of file diff --git a/director.csproj b/director.csproj index b2dc7d5..c32f92d 100644 --- a/director.csproj +++ b/director.csproj @@ -7,7 +7,7 @@ - + diff --git a/schedulable/Scheduled.cs b/schedulable/Scheduled.cs index 71a06b3..3afe303 100644 --- a/schedulable/Scheduled.cs +++ b/schedulable/Scheduled.cs @@ -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; } } } \ No newline at end of file