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)
{
conf = JsonConvert.DeserializeObject<Config>(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<string>();
@ -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();
@ -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

View File

@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<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>