diff --git a/Program.cs b/Program.cs index a85c11b..88a7f69 100644 --- a/Program.cs +++ b/Program.cs @@ -21,8 +21,9 @@ namespace directors_assistant static async Task Main(string[] args) { conf = JsonConvert.DeserializeObject(File.ReadAllText("appsettings.json")); - telefranz = new Telefranz(conf.name, conf.bootstrap_servers, - conf.commands.Select(c => c.name).ToList()); + Telefranz.Configure(name: conf.name, bootstrap_servers: conf.bootstrap_servers, + commands: conf.commands.Select(c => c.name).ToList()); + telefranz = Telefranz.Instance; telefranz.addHandler((silver_messages.directorial.execute_command ec) => { var matchedCommands = conf.commands.Where(c => c.name == ec.command)?.ToList(); @@ -38,7 +39,7 @@ namespace directors_assistant justArgs.AddRange(ec.args); var commandPath = cmdAndArgs[0]; var commandArguments = String.Join(' ', justArgs); - if (ec.timeout > 0) + if (ec.timeout > TimeSpan.Zero) { executeTimed(cmd.name, commandPath, commandArguments, ec.timeout); } @@ -58,7 +59,7 @@ namespace directors_assistant await Task.Delay(-1); } - private static void executeTimed(string commandName, string commandPath, string commandArguments, int timeout) + private static void executeTimed(string commandName, string commandPath, string commandArguments, TimeSpan? timeout) { var process = readableProcess(commandPath, commandArguments); var outputs = new List(); @@ -71,13 +72,20 @@ namespace directors_assistant process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); - Task.WaitAny( - Task.Run(() => - { - process.WaitForExit(); - }), - Task.Delay(new TimeSpan(0, 0, timeout)) - ); + if (timeout > TimeSpan.Zero) + { + Task.WaitAny( + Task.Run(() => + { + process.WaitForExit(); + }), + Task.Delay(timeout.Value) + ); + } + else + { + process.WaitForExit(); + } if (process.HasExited) { stopwatch.Stop(); @@ -97,7 +105,7 @@ namespace directors_assistant Console.WriteLine($"{commandName} expired"); telefranz.ProduceMessage(new silver_messages.directorial.command_expired() { - command = commandName, + command = commandName, stdout = string.Join('\n', outputs), stderr = string.Join('\n', errors) }); @@ -108,25 +116,29 @@ namespace directors_assistant { var stopwatch = new Stopwatch(); var process = readableProcess(commandPath, commandArguments); - process.OutputDataReceived += new DataReceivedEventHandler((s, e) => { + process.OutputDataReceived += new DataReceivedEventHandler((s, e) => + { Console.WriteLine($"{commandName} output: {e.Data}"); - if(string.IsNullOrWhiteSpace(e.Data)) + if (string.IsNullOrWhiteSpace(e.Data)) { return; } - telefranz.ProduceMessage(new silver_messages.directorial.command_output() { + telefranz.ProduceMessage(new silver_messages.directorial.command_output() + { stdout = e.Data, command = commandName, runtime = (uint)stopwatch.ElapsedMilliseconds }); }); - process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { - if(string.IsNullOrWhiteSpace(e.Data)) + 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() { + telefranz.ProduceMessage(new silver_messages.directorial.command_error() + { stderr = e.Data, command = commandName, runtime = (uint)stopwatch.ElapsedMilliseconds diff --git a/directors-assistant.csproj b/directors-assistant.csproj index 22f3b87..3821ee2 100644 --- a/directors-assistant.csproj +++ b/directors-assistant.csproj @@ -13,7 +13,7 @@ - +