diff --git a/TestApp/Program.cs b/TestApp/Program.cs index 98085ac..1dfcc35 100644 --- a/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -1,6 +1,4 @@ -using System; -using System.Net; -using ZeroLevel; +using ZeroLevel; namespace TestApp { @@ -9,7 +7,7 @@ namespace TestApp private static void Main(string[] args) { Configuration.Save(Configuration.ReadFromApplicationConfig()); - Bootstrap.Startup(args, + Bootstrap.Startup(args, () => Configuration.ReadSetFromIniFile("config.ini")) .EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug) //.UseDiscovery() diff --git a/ZeroLevel.Discovery/Program.cs b/ZeroLevel.Discovery/Program.cs index 47ae75c..e33a3fb 100644 --- a/ZeroLevel.Discovery/Program.cs +++ b/ZeroLevel.Discovery/Program.cs @@ -1,14 +1,38 @@ -namespace ZeroLevel.Discovery +using System; +using Topshelf; +using static ZeroLevel.Bootstrap; + +namespace ZeroLevel.Discovery { internal static class Program { private static void Main(string[] args) { - Bootstrap.Startup(args) - .Run() - .WaitWhileStatus(ZeroServiceStatus.Running) - .Stop(); - Bootstrap.Shutdown(); + IServiceExecution se = null; + HostFactory.Run(x => + { + x.StartAutomatically(); + x.Service(s => + { + s.ConstructUsing(name => Bootstrap.Startup(args)); + s.WhenStopped(tc => { tc.Stop(); Bootstrap.Shutdown(); }); + s.WhenStarted(tc => { se = tc.Run(); }); + }); + x.RunAsLocalSystem(); + + x.SetDescription("Discovery"); + x.SetDisplayName("Discovery"); + x.SetServiceName("Discovery"); + + x.OnException(ex => + { + Log.Error(ex, "Service exception"); + }); + }); + if (Environment.UserInteractive && args?.Length < 1) + { + se?.WaitWhileStatus(ZeroServiceStatus.Running); + } } } } \ No newline at end of file diff --git a/ZeroLevel.Discovery/Properties/PublishProfiles/FolderProfile.pubxml b/ZeroLevel.Discovery/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..acc147b --- /dev/null +++ b/ZeroLevel.Discovery/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ + + + + + FileSystem + Release + Any CPU + netcoreapp2.2 + bin\Release\netcoreapp2.2\publish\ + + \ No newline at end of file diff --git a/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj b/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj index 3a7cb5b..2a6d31d 100644 --- a/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj +++ b/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj @@ -7,6 +7,7 @@ + diff --git a/ZeroLevel.SQL/Contracts/IDbProvider.cs b/ZeroLevel.SQL/Contracts/IDbProvider.cs index df9d8bc..7bcbf13 100644 --- a/ZeroLevel.SQL/Contracts/IDbProvider.cs +++ b/ZeroLevel.SQL/Contracts/IDbProvider.cs @@ -18,5 +18,8 @@ namespace ZeroLevel.SqlServer int ExecuteNonResult(string query); int ExecuteNonResult(string query, DbParameter[] par); void LazySelect(string query, DbParameter[] par, Func readHandler, int timeout); + + T Read(DbDataReader reader, int index); + T Read(DbDataReader reader, string name); } } diff --git a/ZeroLevel.SQL/SqlDbProvider.cs b/ZeroLevel.SQL/SqlDbProvider.cs index a756c26..2c93c6b 100644 --- a/ZeroLevel.SQL/SqlDbProvider.cs +++ b/ZeroLevel.SQL/SqlDbProvider.cs @@ -366,5 +366,16 @@ namespace ZeroLevel.SqlServer } } #endregion + + public T Read(DbDataReader reader, int index) + { + if (reader[index] == DBNull.Value) return default(T); + return (T)Convert.ChangeType(reader[index], typeof(T)); + } + public T Read(DbDataReader reader, string name) + { + if (reader[name] == DBNull.Value) return default(T); + return (T)Convert.ChangeType(reader[name], typeof(T)); + } } } diff --git a/ZeroLevel/Services/BaseZeroService.cs b/ZeroLevel/Services/BaseZeroService.cs index 340eff2..fdb99bd 100644 --- a/ZeroLevel/Services/BaseZeroService.cs +++ b/ZeroLevel/Services/BaseZeroService.cs @@ -46,20 +46,30 @@ namespace ZeroLevel.Services.Applications public void ReadServiceInfo() { - this.Name = ReadName(); - this.Key = ReadKey(); - this.Version = ReadVersion(); - this.Group = ReadServiceGroup(); - this.Type = ReadServiceType(); + if (string.IsNullOrWhiteSpace(this.Name)) + this.Name = ReadName(); + if (string.IsNullOrWhiteSpace(this.Key)) + this.Key = ReadKey(); + if (string.IsNullOrWhiteSpace(this.Version)) + this.Version = ReadVersion(); + if (string.IsNullOrWhiteSpace(this.Group)) + this.Group = ReadServiceGroup(); + if (string.IsNullOrWhiteSpace(this.Type)) + this.Type = ReadServiceType(); } public void ReadServiceInfo(IConfigurationSet set) { - this.Name = ReadName(set); - this.Key = ReadKey(set); - this.Version = ReadVersion(set); - this.Group = ReadServiceGroup(set); - this.Type = ReadServiceType(set); + if (string.IsNullOrWhiteSpace(this.Name)) + this.Name = ReadName(set); + if (string.IsNullOrWhiteSpace(this.Key)) + this.Key = ReadKey(set); + if (string.IsNullOrWhiteSpace(this.Version)) + this.Version = ReadVersion(set); + if (string.IsNullOrWhiteSpace(this.Group)) + this.Group = ReadServiceGroup(set); + if (string.IsNullOrWhiteSpace(this.Type)) + this.Type = ReadServiceType(set); } private string ReadName(IConfigurationSet set = null) @@ -128,6 +138,7 @@ namespace ZeroLevel.Services.Applications if (_state == ZeroServiceStatus.Running || _state == ZeroServiceStatus.Initialized) { + ReadServiceInfo(); _exhange.UseDiscovery(); } } @@ -137,6 +148,7 @@ namespace ZeroLevel.Services.Applications if (_state == ZeroServiceStatus.Running || _state == ZeroServiceStatus.Initialized) { + ReadServiceInfo(); _exhange.UseDiscovery(endpoint); } } @@ -146,6 +158,7 @@ namespace ZeroLevel.Services.Applications if (_state == ZeroServiceStatus.Running || _state == ZeroServiceStatus.Initialized) { + ReadServiceInfo(); _exhange.UseDiscovery(endpoint); } } @@ -344,7 +357,7 @@ namespace ZeroLevel.Services.Applications try { _state = ZeroServiceStatus.Running; - StartAction(); + StartAction(); Log.Debug($"[{Name}] Service started"); } catch (Exception ex) diff --git a/ZeroLevel/Services/Network/Contracts/IExchange.cs b/ZeroLevel/Services/Network/Contracts/IExchange.cs index 7094c8e..250ff36 100644 --- a/ZeroLevel/Services/Network/Contracts/IExchange.cs +++ b/ZeroLevel/Services/Network/Contracts/IExchange.cs @@ -15,6 +15,7 @@ namespace ZeroLevel.Network IRouter UseHost(IPEndPoint endpoint); IServiceRoutesStorage RoutesStorage { get; } + IServiceRoutesStorage DiscoveryStorage { get; } ExClient GetConnection(string alias); ExClient GetConnection(IPEndPoint endpoint); diff --git a/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs b/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs index d8b5364..5f7719a 100644 --- a/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs +++ b/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs @@ -15,6 +15,8 @@ namespace ZeroLevel.Network void Remove(IPEndPoint endpoint); + IEnumerable> GetAll(); + InvokeResult Get(string key); InvokeResult> GetAll(string key); InvokeResult GetByType(string type); diff --git a/ZeroLevel/Services/Network/Exchange.cs b/ZeroLevel/Services/Network/Exchange.cs index 95d4b29..343c145 100644 --- a/ZeroLevel/Services/Network/Exchange.cs +++ b/ZeroLevel/Services/Network/Exchange.cs @@ -20,6 +20,7 @@ namespace ZeroLevel.Network private readonly ExClientServerCachee _cachee = new ExClientServerCachee(); public IServiceRoutesStorage RoutesStorage => _user_aliases; + public IServiceRoutesStorage DiscoveryStorage => _dicovery_aliases; private readonly IZeroService _owner; #region Ctor @@ -506,6 +507,7 @@ namespace ZeroLevel.Network { Sheduller.Remove(_register_in_discovery_table_task); } + UpdateServiceListFromDiscovery(); _register_in_discovery_table_task = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(500), _update_discovery_table_period, RegisterServicesInDiscovery); _update_discovery_table_task = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(750), _register_in_discovery_table_period, UpdateServiceListFromDiscovery); } diff --git a/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs b/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs index ae47af1..fd835b9 100644 --- a/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs +++ b/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs @@ -252,6 +252,7 @@ namespace ZeroLevel.Network } return InvokeResult.Fault($"No endpoints by key '{key}'"); } + public InvokeResult> GetAll(string key) { key = key.ToUpperInvariant(); @@ -262,6 +263,13 @@ namespace ZeroLevel.Network } return InvokeResult.Fault>($"No endpoints by key '{key}'"); } + + + public IEnumerable> GetAll() + { + return _tableByKey.SelectMany(pair => pair.Value.Source.Select(s => new KeyValuePair(pair.Key, s))); + } + public InvokeResult GetByType(string type) { type = type.ToUpperInvariant(); diff --git a/ZeroLevel/Services/Semantic/Trie.cs b/ZeroLevel/Services/Semantic/Trie.cs index d13367a..357c5cd 100644 --- a/ZeroLevel/Services/Semantic/Trie.cs +++ b/ZeroLevel/Services/Semantic/Trie.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading; using ZeroLevel.Services.Serialization;