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)
@ -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. //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); Console.WriteLine(calendarUri);
var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export"); var calString = await httpClient.GetStringAsync(directorConf.webdav_uri + calendarUri + "?export");
lock (scratch) lock (scratch)
{ {
var calName = iCalHoopJumping.LoadCalendar(calString); var calName = iCalHoopJumping.LoadCalendar(calString);
@ -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,13 +192,21 @@ 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}");
try if (File.Exists(configurationName))
{ {
return JsonConvert.DeserializeObject<schedulable.Schedulable>(File.ReadAllText(configurationName)); try
{
return JsonConvert.DeserializeObject<schedulable.Schedulable>(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; return null;
} }
} }

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();
} }
@ -163,24 +163,30 @@ namespace director
Console.WriteLine("actual showtime tho"); Console.WriteLine("actual showtime tho");
markLineDone(fileLineIndex, fileLines); markLineDone(fileLineIndex, fileLines);
break; break;
// case LineType.await_signal: // case LineType.await_signal:
// //mark it if I have the signal, but if not, can't do anything but wait // //mark it if I have the signal, but if not, can't do anything but wait
// if (signalsReceived[targetItem.cmd]) // if (signalsReceived[targetItem.cmd])
// { // {
// markLineDone(fileLineIndex, fileLines); // markLineDone(fileLineIndex, fileLines);
// } // }
// else // else
// { // {
// _signal.WaitOne(new TimeSpan(0, 2, 0)); // _signal.WaitOne(new TimeSpan(0, 2, 0));
// } // }
// break; // break;
} }
} }
} }
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; }
} }
} }