diff --git a/Config.cs b/Config.cs index de1daf1..8664461 100644 --- a/Config.cs +++ b/Config.cs @@ -4,17 +4,11 @@ public class Config { public string name { get; set; } = "guy who didn't configure"; public string bootstrap_servers { get; set; } = "localhost:9092"; - public string handling_group { get; set; } = null; - public string kafka_location { get; set; } = "/usr/lib/librdkafka.so"; - public Dictionary topics { get; set; } = new Dictionary() { - {"global", "silver_meddlists.global"}, - {"directorial", "silver_meddlists.directorial"} - }; public class Command { public string name { get; set; } public string shell { get; set; } } - public List commands { get; set; } = new List() { new Command() { name = "echo", shell = "echo" } }; + public List commands { get; set; } public List checks { get; set; } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index b99494d..efb9a1c 100644 --- a/Program.cs +++ b/Program.cs @@ -21,17 +21,18 @@ namespace directors_assistant static async Task Main(string[] args) { conf = JsonConvert.DeserializeObject(File.ReadAllText("appsettings.json")); - telefranz = new Telefranz(conf.name, conf.bootstrap_servers, new List() { "proof_of_concept" }); + telefranz = new Telefranz(conf.name, conf.bootstrap_servers, + conf.commands.Select(c => c.name).ToList(), + conf.checks.Select(c => c.name).ToList()); telefranz.addHandler((silver_messages.directorial.execute_command ec) => { - Console.WriteLine(JsonConvert.SerializeObject(ec)); var matchedCommands = conf.commands.Where(c => c.name == ec.command)?.ToList(); //I swear I have a vague memory of foreach in linq'd query throwing an exception when results empty. if (matchedCommands != null && matchedCommands.Count > 0) { foreach (var cmd in matchedCommands) { - Console.WriteLine($"executing {cmd.name}, given args {ec.args}" + cmd.name); + Console.WriteLine($"executing {cmd.name}{(ec.args != null ? ", given args " + string.Join(' ', ec.args) : "")} with timeout {ec.timeout}"); var cmdAndArgs = cmd.shell.Split(' '); var justArgs = cmdAndArgs.Skip(1).ToList(); @@ -52,13 +53,13 @@ namespace directors_assistant } } }); - Console.WriteLine("off we go"); + telefranz.addHandler((silver_messages.directorial.execute_check ec) => { + //TODO + }); await Task.Delay(1000); - Console.WriteLine("im a strong independent director's assistant who don't need no director"); telefranz.ProduceMessage(new silver_messages.global.sound_off()); - await Task.Delay(120000); - Console.WriteLine("alright fuck it"); + await Task.Delay(-1); } private static void executeTimed(string commandName, string commandPath, string commandArguments, int timeout) @@ -84,10 +85,11 @@ namespace directors_assistant if (process.HasExited) { stopwatch.Stop(); + Console.WriteLine($"{commandName} completed"); telefranz.ProduceMessage(new silver_messages.directorial.command_completed() { command = commandName, - runtime = (uint)stopwatch.ElapsedMilliseconds, + runtimeMilliseconds = stopwatch.ElapsedMilliseconds, exit_code = process.ExitCode, stdout = string.Join('\n', outputs), stderr = string.Join('\n', errors) @@ -96,38 +98,56 @@ namespace directors_assistant else { process.Kill(); + Console.WriteLine($"{commandName} expired"); telefranz.ProduceMessage(new silver_messages.directorial.command_expired() { - command = commandName, - runtime = (uint)timeout + command = commandName, + stdout = string.Join('\n', outputs), + stderr = string.Join('\n', errors) }); } } private static void executeUntimed(string commandName, string commandPath, string commandArguments) { + var stopwatch = new Stopwatch(); var process = readableProcess(commandPath, commandArguments); process.OutputDataReceived += new DataReceivedEventHandler((s, e) => { + Console.WriteLine($"{commandName} output: {e.Data}"); + if(string.IsNullOrWhiteSpace(e.Data)) + { + return; + } telefranz.ProduceMessage(new silver_messages.directorial.command_output() { - stdout = e.Data + stdout = e.Data, + command = commandName, + runtime = (uint)stopwatch.ElapsedMilliseconds }); }); process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { + if(string.IsNullOrWhiteSpace(e.Data)) + { + return; + } + Console.Error.WriteLine($"{commandName} err (but not necessarily dead?): {e.Data}"); telefranz.ProduceMessage(new silver_messages.directorial.command_error() { - stderr = e.Data + stderr = e.Data, + command = commandName, + runtime = (uint)stopwatch.ElapsedMilliseconds }); }); - var stopwatch = new Stopwatch(); stopwatch.Start(); process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); + stopwatch.Stop(); + Console.WriteLine($"{commandName} returned {process.ExitCode} after {stopwatch.ElapsedMilliseconds}ms"); telefranz.ProduceMessage(new silver_messages.directorial.command_completed() { command = commandName, - runtime = (uint)stopwatch.ElapsedMilliseconds, + runtimeMilliseconds = stopwatch.ElapsedMilliseconds, exit_code = process.ExitCode }); } diff --git a/appsettings.example.json b/appsettings.example.json index 4ae86df..4b5610b 100644 --- a/appsettings.example.json +++ b/appsettings.example.json @@ -1,14 +1,10 @@ { "name": "guy who didn't configure", "bootstrap servers": "localhost:9092", - "handling group": "directors_assistant.foo", - "kafka location": "/usr/lib/librdkafka.so", - "topics": [ - "silver_meddlists.global", - "silver_meddlists.directorial" - ], "commands": [ {"name":"echo", "shell": "echo"} ], - "checks": [] + "checks": [ + {"name": "echo", "shell": "echo 1"} + ] } \ No newline at end of file diff --git a/directors-assistant.csproj b/directors-assistant.csproj index b8adb97..22f3b87 100644 --- a/directors-assistant.csproj +++ b/directors-assistant.csproj @@ -13,12 +13,15 @@ - + Always + + Always + diff --git a/loopy.sh b/loopy.sh new file mode 100755 index 0000000..68328ed --- /dev/null +++ b/loopy.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +for i in {1..10} +do + echo "$i: $(date)" + sleep 3 +done \ No newline at end of file