telefranz is a singleton
kind of wrecked my ability to use telefranz.tests, but I'm not going to ruin my entire design just for the fun of writing twice as much code for no benefit
This commit is contained in:
parent
1e3ea299df
commit
07a5f0a5f4
@ -8,9 +8,9 @@ namespace franz.tests
|
|||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello World!");
|
Console.WriteLine("Hello World!");
|
||||||
var f1 = new Telefranz("libfranztest1", "focalor:9092");
|
Telefranz.Configure("libfranztest1", "focalor:9092");
|
||||||
var f1Reported = false;
|
var f1Reported = false;
|
||||||
f1.addHandler<silver_messages.global.report>((r) => {
|
Telefranz.Instance.addHandler<silver_messages.global.report>((r) => {
|
||||||
Console.WriteLine($"someone reporting. {r}");
|
Console.WriteLine($"someone reporting. {r}");
|
||||||
if(r.name =="libfranztest1")
|
if(r.name =="libfranztest1")
|
||||||
{
|
{
|
||||||
@ -19,24 +19,6 @@ namespace franz.tests
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
await Task.Delay(1000);
|
await Task.Delay(1000);
|
||||||
f1.ProduceMessage(new silver_messages.global.sound_off());
|
|
||||||
Task.WaitAny(
|
|
||||||
Task.Run(async () => {
|
|
||||||
while(f1Reported == false)
|
|
||||||
{
|
|
||||||
Console.WriteLine("not ready, giving another 100ms");
|
|
||||||
await Task.Delay(100);
|
|
||||||
}
|
|
||||||
Console.WriteLine("done, ready, green");
|
|
||||||
Environment.Exit(0);
|
|
||||||
}),
|
|
||||||
Task.Run(async () => {
|
|
||||||
await Task.Delay(30000);
|
|
||||||
Console.WriteLine("time up");
|
|
||||||
await Task.Delay(500);
|
|
||||||
Environment.Exit(-1);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
Console.WriteLine("done I guess?");
|
Console.WriteLine("done I guess?");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,11 @@ namespace franz
|
|||||||
{
|
{
|
||||||
public class Telefranz
|
public class Telefranz
|
||||||
{
|
{
|
||||||
static class _TelefranzConsumers<T> where T : silver_messages.message {
|
private static Telefranz instance = null;
|
||||||
|
private static readonly object createLock = new object();
|
||||||
|
|
||||||
|
static class _TelefranzConsumers<T> where T : silver_messages.message
|
||||||
|
{
|
||||||
internal static readonly Dictionary<Action<T>, Action<KafkaRecord<string, string>>> wrappings
|
internal static readonly Dictionary<Action<T>, Action<KafkaRecord<string, string>>> wrappings
|
||||||
= new Dictionary<Action<T>, Action<KafkaRecord<string, string>>>();
|
= new Dictionary<Action<T>, Action<KafkaRecord<string, string>>>();
|
||||||
}
|
}
|
||||||
@ -24,23 +28,22 @@ namespace franz
|
|||||||
private Dictionary<string, int> topicSubscribers { get; set; } = new Dictionary<string, int>();
|
private Dictionary<string, int> topicSubscribers { get; set; } = new Dictionary<string, int>();
|
||||||
private Kafka.Public.Configuration clusterClientConfig = null;
|
private Kafka.Public.Configuration clusterClientConfig = null;
|
||||||
private static ConsoleLogger consoleLogger = new ConsoleLogger();
|
private static ConsoleLogger consoleLogger = new ConsoleLogger();
|
||||||
|
private Telefranz(string name, string bootstrap_servers,
|
||||||
public Telefranz(string name, string bootstrap_servers,
|
|
||||||
List<string> commands = null, List<string> checks = null,
|
List<string> commands = null, List<string> checks = null,
|
||||||
List<string> errors = null, List<string> warnings = null)
|
List<string> errors = null, List<string> warnings = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
var serializer = new StringSerializer();
|
var serializer = new StringSerializer();
|
||||||
var deserializer = new StringDeserializer();
|
var deserializer = new StringDeserializer();
|
||||||
var serializationConfig = new SerializationConfig() { SerializeOnProduce = true };
|
var serializationConfig = new SerializationConfig() { SerializeOnProduce = true };
|
||||||
serializationConfig.SetDefaultSerializers(serializer, serializer);
|
serializationConfig.SetDefaultSerializers(serializer, serializer);
|
||||||
serializationConfig.SetDefaultDeserializers(deserializer, deserializer);
|
serializationConfig.SetDefaultDeserializers(deserializer, deserializer);
|
||||||
clusterClientConfig = new Kafka.Public.Configuration {
|
this.clusterClientConfig = new Kafka.Public.Configuration
|
||||||
|
{
|
||||||
Seeds = bootstrap_servers,
|
Seeds = bootstrap_servers,
|
||||||
ClientId = name,
|
ClientId = name,
|
||||||
SerializationConfig = serializationConfig
|
SerializationConfig = serializationConfig
|
||||||
};
|
};
|
||||||
clusterClient = new ClusterClient(clusterClientConfig, consoleLogger);
|
this.clusterClient = new ClusterClient(clusterClientConfig, consoleLogger);
|
||||||
|
|
||||||
handling_group = name;
|
handling_group = name;
|
||||||
this.howToReport = new silver_messages.global.report()
|
this.howToReport = new silver_messages.global.report()
|
||||||
@ -77,7 +80,34 @@ namespace franz
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
public static void Configure(string name, string bootstrap_servers,
|
||||||
|
List<string> commands = null, List<string> checks = null,
|
||||||
|
List<string> errors = null, List<string> warnings = null)
|
||||||
|
{
|
||||||
|
lock (createLock)
|
||||||
|
{
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
instance = new Telefranz(name, bootstrap_servers, commands, checks, errors, warnings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Telefranz Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (createLock)
|
||||||
|
{
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
throw new NotInitializedException("Configure me first");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
public void addHandler<T>(Action<T> theAction) where T : silver_messages.message
|
public void addHandler<T>(Action<T> theAction) where T : silver_messages.message
|
||||||
{
|
{
|
||||||
var topic = typeof(T).ToString();
|
var topic = typeof(T).ToString();
|
||||||
@ -98,7 +128,8 @@ namespace franz
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if(!topicConsumers.ContainsKey(typeof(T).ToString())){
|
if (!topicConsumers.ContainsKey(typeof(T).ToString()))
|
||||||
|
{
|
||||||
topicConsumers[topic] = new KafkaConsumer<string, string>(topic, clusterClient);
|
topicConsumers[topic] = new KafkaConsumer<string, string>(topic, clusterClient);
|
||||||
topicConsumers[topic].ConsumeFromLatest();
|
topicConsumers[topic].ConsumeFromLatest();
|
||||||
topicSubscribers.Add(topic, 0);
|
topicSubscribers.Add(topic, 0);
|
||||||
@ -124,4 +155,8 @@ namespace franz
|
|||||||
}
|
}
|
||||||
public void HiImAFunction() { }
|
public void HiImAFunction() { }
|
||||||
}
|
}
|
||||||
|
public class NotInitializedException : Exception
|
||||||
|
{
|
||||||
|
public NotInitializedException(string message) : base(message) { }
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<PackageId>silvermeddlists.franz</PackageId>
|
<PackageId>silvermeddlists.franz</PackageId>
|
||||||
<Version>0.0.3</Version>
|
<Version>0.0.4</Version>
|
||||||
<Authors>adam</Authors>
|
<Authors>adam</Authors>
|
||||||
<Company>Silver Meddlists</Company>
|
<Company>Silver Meddlists</Company>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user