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"))
@ -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)
@ -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<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;
}
}

View File

@ -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);
}
}
}
}

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; }
}
}