update for updated telefranz

This commit is contained in:
Adam R. Grey 2021-10-15 11:04:49 -04:00
parent 40e050c334
commit 10806b1524
2 changed files with 31 additions and 19 deletions

View File

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

View File

@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="silvermeddlists.franz" Version="0.0.3" /> <PackageReference Include="silvermeddlists.franz" Version="0.0.5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>