79 lines
3.2 KiB
C#
79 lines
3.2 KiB
C#
using bts.api.core;
|
|
using bts.test;
|
|
using ManyConsole;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bts {
|
|
class TailService : ConsoleCommand {
|
|
static bool _all = false;
|
|
static bool _timestamp = false;
|
|
public TailService() {
|
|
void ignore(string _) { }
|
|
IsCommand("tailservice", "Tail updates to named service api. Ex: tail --method=SyntheticSecurityService.metadata");
|
|
HasRequiredOption("method", "Service.method to tail", ignore);
|
|
HasOption("time", "Include timestamp", _ => _timestamp = true);
|
|
// The following are not implemented. If you need them get to work.
|
|
HasOption("path", "Path inside the row to use for display. Ex: 'Id' or 'Name' ", ignore);
|
|
HasOption("all", "Invlude 'all' rows on every print", _ => _all = true);
|
|
}
|
|
public override int Run(string[] remainingArguments) {
|
|
var arg = this.get_argument("method");
|
|
var split = arg.Split('.');
|
|
if (split.Length != 2) {
|
|
Console.WriteLine("Try --method=[ServiceName].[Method]");
|
|
return -1;
|
|
}
|
|
tail(split[0], split[1]);
|
|
return 0;
|
|
}
|
|
class SubscriptionLogger<T> : IObserver<T> {
|
|
public void OnCompleted() {
|
|
ConsoleEx.WriteLine("Completed.", ConsoleColor.Yellow);
|
|
}
|
|
|
|
public void OnError(Exception error) {
|
|
ConsoleEx.WriteLine($"Error. '{error}'", ConsoleColor.Red);
|
|
}
|
|
|
|
public void OnNext(T value) {
|
|
if (_timestamp) {
|
|
Console.Write(DateTime.Now.ToString("HH:mm:ss.fff"));
|
|
Console.Write(" ");
|
|
}
|
|
if (_all) { }
|
|
Console.WriteLine(value);
|
|
}
|
|
}
|
|
private static void tail(string service_name, string method_name) {
|
|
var spark_api = typeof(ServiceContext).Assembly;
|
|
var service_type = spark_api.GetExportedTypes().FirstOrDefault(t => t.Name == service_name);
|
|
if (service_type == null) {
|
|
Console.WriteLine($"Could not find service type {service_name} in spark_api.dll");
|
|
return;
|
|
}
|
|
var subscribe_method = service_type.GetMethod("subscribei_" + method_name);
|
|
using (var ctx = new ServiceContext()) {
|
|
var locate = typeof(ServiceContext).GetMethod("locate").MakeGenericMethod(service_type);
|
|
var service = locate.Invoke(ctx, new[] { "" });
|
|
// Subscribe with null qualifier
|
|
object observable = subscribe_method.Invoke(service, new object[] { null });
|
|
Type update_type = observable.GetType().GetGenericArguments().First(arg => arg.Name.StartsWith("Update"));
|
|
Type observer_type = typeof(SubscriptionLogger<>).MakeGenericType(update_type);
|
|
object observer = Activator.CreateInstance(observer_type);
|
|
|
|
var subscribe = observable.GetType().GetMethod("Subscribe", new[] { observer_type });
|
|
var dispose = subscribe.Invoke(observable, new[] { observer });
|
|
|
|
while (true) {
|
|
System.Threading.Thread.Sleep(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|