update for updated telefranz
This commit is contained in:
parent
40e050c334
commit
10806b1524
34
Program.cs
34
Program.cs
@ -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();
|
||||||
|
if (timeout > TimeSpan.Zero)
|
||||||
|
{
|
||||||
Task.WaitAny(
|
Task.WaitAny(
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
process.WaitForExit();
|
process.WaitForExit();
|
||||||
}),
|
}),
|
||||||
Task.Delay(new TimeSpan(0, 0, timeout))
|
Task.Delay(timeout.Value)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
process.WaitForExit();
|
||||||
|
}
|
||||||
if (process.HasExited)
|
if (process.HasExited)
|
||||||
{
|
{
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
@ -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
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user