poc check for responding agents seems to work, need to test telefranzing them

This commit is contained in:
Adam R. Grey 2021-09-03 03:38:21 -04:00
parent 47de14dca9
commit ac3b998da9
7 changed files with 153 additions and 48 deletions

59
HumanCommunication.cs Normal file
View File

@ -0,0 +1,59 @@
using System;
using System.IO;
using System.Net;
namespace Director
{
public class HumanCommunication
{
private static string call_for_humans_discord_webhook{get;set;}
private static HumanCommunication _instance = null;
private static readonly object createLock = new object();
public static void Configure(string call_for_humans_discord_webhook)
{
lock (createLock)
{
if (_instance == null)
{
_instance = new HumanCommunication(call_for_humans_discord_webhook);
}
}
}
public static HumanCommunication Instance
{
get
{
lock (createLock)
{
if (_instance == null)
{
throw new NotInitializedException("Configure me first");
}
}
return _instance;
}
}
private HumanCommunication(string discord_webhook)
{
HumanCommunication.call_for_humans_discord_webhook = discord_webhook;
}
public void Say(string message, LogLevel logLevel = LogLevel.Info)
{
Console.WriteLine($"[{DateTime.Now.ToLongTimeString()}] [{logLevel.ToString()}] {message}");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(call_for_humans_discord_webhook);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write($"{{\"content\":\"[{DateTime.Now.ToLongTimeString()}] [{logLevel.ToString()}] {message}\"}}");
}
httpWebRequest.GetResponse();
}
public enum LogLevel { Trace, Verbose, Info, Warning, Error, Showstopper }
}
public class NotInitializedException : Exception
{
public NotInitializedException(string message) : base(message) { }
}
}

20
Log.cs
View File

@ -1,20 +0,0 @@
using System;
using System.IO;
using System.Net;
public class Log
{
public static string call_for_humans_discord_webhook{get;set;}
public static void Panic(string message)
{
Console.Error.WriteLine(message);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(call_for_humans_discord_webhook);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write("{\"content\":\"" + message + "\"}");
}
httpWebRequest.GetResponse();
}
}

View File

@ -8,6 +8,7 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Director;
using franz; using franz;
using Newtonsoft.Json; using Newtonsoft.Json;
using ShowHandlers; using ShowHandlers;
@ -34,7 +35,7 @@ namespace director
return; return;
} }
conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText("appsettings.json")); conf = JsonConvert.DeserializeObject<Config>(File.ReadAllText("appsettings.json"));
Log.call_for_humans_discord_webhook = conf.call_for_humans_discord_webhook; HumanCommunication.Configure(conf.call_for_humans_discord_webhook);
httpClient = new HttpClient(); httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
@ -94,7 +95,7 @@ namespace director
} }
Console.WriteLine("calendars checked"); Console.WriteLine("calendars checked");
#if (DEBUG) #if (DEBUG)
for (int i = 0; i < 3; i++) for (int i = 0; i < 1; i++)
{ {
var psuedo = new Schedulable.Schedulable(); var psuedo = new Schedulable.Schedulable();
psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30); psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30);
@ -156,6 +157,7 @@ namespace director
{ {
Task.WaitAll(Task.Delay(napLength)); Task.WaitAll(Task.Delay(napLength));
} }
Console.WriteLine("places, everyone!");
try try
{ {
@ -173,7 +175,7 @@ namespace director
} }
catch (Exception e) catch (Exception e)
{ {
Log.Panic($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}"); HumanCommunication.Instance.Say($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}", HumanCommunication.LogLevel.Showstopper);
} }
break; break;
} }

View File

@ -12,12 +12,12 @@ namespace Schedulable.Show
public class TaskList public class TaskList
{ {
public IEnumerable<Checklistable> Manual { get; set; } public IEnumerable<Checklistable> Manual { get; set; }
public IEnumerable<string> Commands { get; set; } public IEnumerable<CommandLine> Commands { get; set; }
} }
public class PreshowTaskList : TaskList public class PreshowTaskList : TaskList
{ {
public IEnumerable<string> AgentsNeeded { get; set; } public IEnumerable<string> AgentsNeeded { get; set; }
public IEnumerable<string> Checks { get; set; } public IEnumerable<CommandLineCheck> Checks { get; set; }
} }
public class Checklistable public class Checklistable
{ {
@ -25,9 +25,17 @@ namespace Schedulable.Show
public string Label { get; set; } public string Label { get; set; }
public IEnumerable<Checklistable> Items { get; set; } //if no items, just throw up a checkbox public IEnumerable<Checklistable> Items { get; set; } //if no items, just throw up a checkbox
} }
public class CommandLine
{
public string cmd { get; set; }
public string args { get; set; }
}
public class CommandLineCheck : CommandLine
{
public string target { get; set; }
}
#endregion #endregion
} }
public class TwitchStream : Show public class TwitchStream : Show
{ {
public string TwitchTitle { get; set; } public string TwitchTitle { get; set; }

View File

@ -5,6 +5,7 @@ namespace ShowHandlers
{ {
public abstract class ShowHandler public abstract class ShowHandler
{ {
protected abstract TimeSpan AgentsRespondingBuffer { get; }
public abstract void Handle(iCalHoopJumping.CalendarOccurrence evt); public abstract void Handle(iCalHoopJumping.CalendarOccurrence evt);
} }
} }

View File

@ -5,6 +5,7 @@ namespace ShowHandlers
{ {
public class TwitchStreamHandler : ShowHandler public class TwitchStreamHandler : ShowHandler
{ {
protected override TimeSpan AgentsRespondingBuffer { get { return TimeSpan.FromHours(1); } }
public override void Handle(iCalHoopJumping.CalendarOccurrence evt) public override void Handle(iCalHoopJumping.CalendarOccurrence evt)
{ {
Console.WriteLine("I am a twitch stream handler, it falls to me to..."); Console.WriteLine("I am a twitch stream handler, it falls to me to...");

View File

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using director; using director;
using Director;
using franz; using franz;
using Newtonsoft.Json; using Newtonsoft.Json;
using Schedulable.Show; using Schedulable.Show;
@ -14,57 +16,109 @@ namespace ShowHandlers
{ {
private YoutubeRelease config; private YoutubeRelease config;
private List<string> agentsPresent = new List<string>(); private List<string> agentsPresent = new List<string>();
private bool essentialChecksPassed = false; private Dictionary<string, bool> checksProgress = new Dictionary<string, bool>();
private bool allChecksPassed = false;
private readonly AutoResetEvent checkSignal = new AutoResetEvent(false); private readonly AutoResetEvent checkSignal = new AutoResetEvent(false);
protected override TimeSpan AgentsRespondingBuffer { get { return TimeSpan.FromHours(1); } }
public override void Handle(iCalHoopJumping.CalendarOccurrence evt) public override void Handle(iCalHoopJumping.CalendarOccurrence evt)
{ {
config = JsonConvert.DeserializeObject<YoutubeRelease>(Program.conf.show_template_yt_release); Console.WriteLine($"begin youtube handler. current time {DateTime.Now.ToLongTimeString()}");
config.PreShow.AgentsNeeded = config.PreShow.AgentsNeeded.Select(s => s.ToLower()); Console.WriteLine(JsonConvert.SerializeObject(evt));
Telefranz.Instance.addHandler<silver_messages.global.report>(agentReports); var ytConfig = File.ReadAllText(Program.conf.show_template_yt_release);
config = JsonConvert.DeserializeObject<YoutubeRelease>(ytConfig);
config.PreShow.AgentsNeeded = config.PreShow.AgentsNeeded.Select(s => s.ToLower()).ToList();
Task.Run(() => PreShow(evt)); Task.Run(() => PreShow(evt));
if(evt.OccurrenceStart > DateTime.Now) Console.WriteLine("kicked off the pre-show");
if (evt.OccurrenceStart > DateTime.Now)
{ {
Task.WaitAll(Task.Delay(evt.OccurrenceStart - DateTime.Now)); Task.WaitAll(Task.Delay(evt.OccurrenceStart - DateTime.Now));
} }
//youtube releases in particular jump right to post-show
PostShow(evt); PostShow(evt);
} }
~YoutubeHandler() ~YoutubeHandler()
{ {
Telefranz.Instance.removeHandler<silver_messages.global.report>(agentReports); //Telefranz.Instance.removeHandler<silver_messages.global.report>(agentReports);
} }
protected void PreShow(iCalHoopJumping.CalendarOccurrence evt) protected void PreShow(iCalHoopJumping.CalendarOccurrence evt)
{ {
Console.WriteLine($"it's the pre-show, showtime at: {evt.OccurrenceStart}"); Console.WriteLine($"it's the pre-show, showtime at: {evt.OccurrenceStart}");
preShow_GetAgents(evt);
preShow_RunChecks(evt);
preShow_RunCommands(evt);
}
protected void preShow_GetAgents(iCalHoopJumping.CalendarOccurrence evt)
{
Console.WriteLine($"preshow agents needed: {string.Join(", ", config.PreShow.AgentsNeeded)}");
agentsPresent.Clear(); agentsPresent.Clear();
essentialChecksPassed = false;
allChecksPassed = false; //TODO: actual telefranz
Task.WaitAny( //Telefranz.Instance.addHandler<silver_messages.global.report>(agentReports);
Task.Delay(10) //Telefranz.Instance.ProduceMessage(new silver_messages.global.sound_off());
); while (config.PreShow.AgentsNeeded.Where(ap => !agentsPresent.Contains(ap))?.Count() > 0)
Telefranz.Instance.ProduceMessage(new silver_messages.global.sound_off()); {
while(agentsPresent.Where(ap => !config.PreShow.AgentsNeeded.Contains(ap))?.Count() > 0) checkSignal.WaitOne(TimeSpan.FromMinutes(1));
if(DateTime.Now > evt.OccurrenceStart - AgentsRespondingBuffer)
{
var miaAgents = string.Join(", ", config.PreShow.AgentsNeeded.Where(ap => !agentsPresent.Contains(ap)));
HumanCommunication.Instance.Say($"Youtube handler getting antsy and going forward. mia agents: {miaAgents}",
HumanCommunication.LogLevel.Error);
break;
}
}
Console.WriteLine("going forward");
}
protected void preShow_RunChecks(iCalHoopJumping.CalendarOccurrence evt)
{
Console.WriteLine("pre show: run checks:");
//TODO
checksProgress.Clear();
var argsList = new List<string>() { JsonConvert.SerializeObject(evt) };
//Telefranz.Instance.addHandler()
foreach (var c in config.PreShow.Checks)
{
checksProgress.Add(c.cmd, false);
Telefranz.Instance.ProduceMessage(new silver_messages.directorial.execute_check()
{
check = c.cmd,
args = argsList.Append(c.args).ToList()
});
}
while (true)
{ {
checkSignal.WaitOne(TimeSpan.FromSeconds(45)); checkSignal.WaitOne(TimeSpan.FromSeconds(45));
} }
// //await sound offs for some timeout }
// config.PreShow.AgentsNeeded; protected void preShow_RunCommands(iCalHoopJumping.CalendarOccurrence evt)
// //once that's ok, await checks {
// config.PreShow.Checks; Console.WriteLine("pre show: run commands");
// //once that's ok, await first command, then start running them
// config.PreShow.Commands;
} }
protected void agentReports(silver_messages.global.report r) protected void agentReports(silver_messages.global.report r)
{ {
if(config.PreShow.AgentsNeeded?.FirstOrDefault(an => an.ToLower() == r.name.ToLower()) != null) if (config.PreShow.AgentsNeeded?.FirstOrDefault(an => an.ToLower() == r.name.ToLower()) != null)
{ {
lock(agentsPresent) lock (agentsPresent)
{ {
agentsPresent.Add(r.name.ToLower()); agentsPresent.Add(r.name.ToLower());
checkSignal.Set(); checkSignal.Set();
} }
} }
} }
protected void checkReports(silver_messages.directorial.check_complete check)
{
var myCheck = config.PreShow.Checks.FirstOrDefault(psc => psc.cmd == check.check);
if (myCheck != null)
{
lock (checksProgress)
{
checksProgress[myCheck.cmd] = check.result == myCheck.target;
}
checkSignal.Set();
}
}
protected void PostShow(iCalHoopJumping.CalendarOccurrence evt) protected void PostShow(iCalHoopJumping.CalendarOccurrence evt)
{ {
Console.WriteLine("can't do this bit without understanding when a show ends, sooo."); Console.WriteLine("can't do this bit without understanding when a show ends, sooo.");