pull/1/head
a.bozhenov 5 years ago
parent 7db645b688
commit e82a56a104

@ -1,6 +1,4 @@
using System; using ZeroLevel;
using System.Net;
using ZeroLevel;
namespace TestApp namespace TestApp
{ {
@ -9,7 +7,7 @@ namespace TestApp
private static void Main(string[] args) private static void Main(string[] args)
{ {
Configuration.Save(Configuration.ReadFromApplicationConfig()); Configuration.Save(Configuration.ReadFromApplicationConfig());
Bootstrap.Startup<MyService>(args, Bootstrap.Startup<MyService>(args,
() => Configuration.ReadSetFromIniFile("config.ini")) () => Configuration.ReadSetFromIniFile("config.ini"))
.EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug) .EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug)
//.UseDiscovery() //.UseDiscovery()

@ -1,14 +1,38 @@
namespace ZeroLevel.Discovery using System;
using Topshelf;
using static ZeroLevel.Bootstrap;
namespace ZeroLevel.Discovery
{ {
internal static class Program internal static class Program
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
Bootstrap.Startup<DiscoveryService>(args) IServiceExecution se = null;
.Run() HostFactory.Run(x =>
.WaitWhileStatus(ZeroServiceStatus.Running) {
.Stop(); x.StartAutomatically();
Bootstrap.Shutdown(); x.Service<BootstrapFluent>(s =>
{
s.ConstructUsing(name => Bootstrap.Startup<DiscoveryService>(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);
}
} }
} }
} }

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.2</TargetFramework>
<PublishDir>bin\Release\netcoreapp2.2\publish\</PublishDir>
</PropertyGroup>
</Project>

@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Topshelf" Version="4.2.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -18,5 +18,8 @@ namespace ZeroLevel.SqlServer
int ExecuteNonResult(string query); int ExecuteNonResult(string query);
int ExecuteNonResult(string query, DbParameter[] par); int ExecuteNonResult(string query, DbParameter[] par);
void LazySelect(string query, DbParameter[] par, Func<DbDataReader, bool> readHandler, int timeout); void LazySelect(string query, DbParameter[] par, Func<DbDataReader, bool> readHandler, int timeout);
T Read<T>(DbDataReader reader, int index);
T Read<T>(DbDataReader reader, string name);
} }
} }

@ -366,5 +366,16 @@ namespace ZeroLevel.SqlServer
} }
} }
#endregion #endregion
public T Read<T>(DbDataReader reader, int index)
{
if (reader[index] == DBNull.Value) return default(T);
return (T)Convert.ChangeType(reader[index], typeof(T));
}
public T Read<T>(DbDataReader reader, string name)
{
if (reader[name] == DBNull.Value) return default(T);
return (T)Convert.ChangeType(reader[name], typeof(T));
}
} }
} }

@ -46,20 +46,30 @@ namespace ZeroLevel.Services.Applications
public void ReadServiceInfo() public void ReadServiceInfo()
{ {
this.Name = ReadName(); if (string.IsNullOrWhiteSpace(this.Name))
this.Key = ReadKey(); this.Name = ReadName();
this.Version = ReadVersion(); if (string.IsNullOrWhiteSpace(this.Key))
this.Group = ReadServiceGroup(); this.Key = ReadKey();
this.Type = ReadServiceType(); 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) public void ReadServiceInfo(IConfigurationSet set)
{ {
this.Name = ReadName(set); if (string.IsNullOrWhiteSpace(this.Name))
this.Key = ReadKey(set); this.Name = ReadName(set);
this.Version = ReadVersion(set); if (string.IsNullOrWhiteSpace(this.Key))
this.Group = ReadServiceGroup(set); this.Key = ReadKey(set);
this.Type = ReadServiceType(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) private string ReadName(IConfigurationSet set = null)
@ -128,6 +138,7 @@ namespace ZeroLevel.Services.Applications
if (_state == ZeroServiceStatus.Running if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized) || _state == ZeroServiceStatus.Initialized)
{ {
ReadServiceInfo();
_exhange.UseDiscovery(); _exhange.UseDiscovery();
} }
} }
@ -137,6 +148,7 @@ namespace ZeroLevel.Services.Applications
if (_state == ZeroServiceStatus.Running if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized) || _state == ZeroServiceStatus.Initialized)
{ {
ReadServiceInfo();
_exhange.UseDiscovery(endpoint); _exhange.UseDiscovery(endpoint);
} }
} }
@ -146,6 +158,7 @@ namespace ZeroLevel.Services.Applications
if (_state == ZeroServiceStatus.Running if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized) || _state == ZeroServiceStatus.Initialized)
{ {
ReadServiceInfo();
_exhange.UseDiscovery(endpoint); _exhange.UseDiscovery(endpoint);
} }
} }
@ -344,7 +357,7 @@ namespace ZeroLevel.Services.Applications
try try
{ {
_state = ZeroServiceStatus.Running; _state = ZeroServiceStatus.Running;
StartAction(); StartAction();
Log.Debug($"[{Name}] Service started"); Log.Debug($"[{Name}] Service started");
} }
catch (Exception ex) catch (Exception ex)

@ -15,6 +15,7 @@ namespace ZeroLevel.Network
IRouter UseHost(IPEndPoint endpoint); IRouter UseHost(IPEndPoint endpoint);
IServiceRoutesStorage RoutesStorage { get; } IServiceRoutesStorage RoutesStorage { get; }
IServiceRoutesStorage DiscoveryStorage { get; }
ExClient GetConnection(string alias); ExClient GetConnection(string alias);
ExClient GetConnection(IPEndPoint endpoint); ExClient GetConnection(IPEndPoint endpoint);

@ -15,6 +15,8 @@ namespace ZeroLevel.Network
void Remove(IPEndPoint endpoint); void Remove(IPEndPoint endpoint);
IEnumerable<KeyValuePair<string, IPEndPoint>> GetAll();
InvokeResult<IPEndPoint> Get(string key); InvokeResult<IPEndPoint> Get(string key);
InvokeResult<IEnumerable<IPEndPoint>> GetAll(string key); InvokeResult<IEnumerable<IPEndPoint>> GetAll(string key);
InvokeResult<IPEndPoint> GetByType(string type); InvokeResult<IPEndPoint> GetByType(string type);

@ -20,6 +20,7 @@ namespace ZeroLevel.Network
private readonly ExClientServerCachee _cachee = new ExClientServerCachee(); private readonly ExClientServerCachee _cachee = new ExClientServerCachee();
public IServiceRoutesStorage RoutesStorage => _user_aliases; public IServiceRoutesStorage RoutesStorage => _user_aliases;
public IServiceRoutesStorage DiscoveryStorage => _dicovery_aliases;
private readonly IZeroService _owner; private readonly IZeroService _owner;
#region Ctor #region Ctor
@ -506,6 +507,7 @@ namespace ZeroLevel.Network
{ {
Sheduller.Remove(_register_in_discovery_table_task); Sheduller.Remove(_register_in_discovery_table_task);
} }
UpdateServiceListFromDiscovery();
_register_in_discovery_table_task = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(500), _update_discovery_table_period, RegisterServicesInDiscovery); _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); _update_discovery_table_task = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(750), _register_in_discovery_table_period, UpdateServiceListFromDiscovery);
} }

@ -252,6 +252,7 @@ namespace ZeroLevel.Network
} }
return InvokeResult.Fault<IPEndPoint>($"No endpoints by key '{key}'"); return InvokeResult.Fault<IPEndPoint>($"No endpoints by key '{key}'");
} }
public InvokeResult<IEnumerable<IPEndPoint>> GetAll(string key) public InvokeResult<IEnumerable<IPEndPoint>> GetAll(string key)
{ {
key = key.ToUpperInvariant(); key = key.ToUpperInvariant();
@ -262,6 +263,13 @@ namespace ZeroLevel.Network
} }
return InvokeResult.Fault<IEnumerable<IPEndPoint>>($"No endpoints by key '{key}'"); return InvokeResult.Fault<IEnumerable<IPEndPoint>>($"No endpoints by key '{key}'");
} }
public IEnumerable<KeyValuePair<string, IPEndPoint>> GetAll()
{
return _tableByKey.SelectMany(pair => pair.Value.Source.Select(s => new KeyValuePair<string, IPEndPoint>(pair.Key, s)));
}
public InvokeResult<IPEndPoint> GetByType(string type) public InvokeResult<IPEndPoint> GetByType(string type)
{ {
type = type.ToUpperInvariant(); type = type.ToUpperInvariant();

@ -1,5 +1,4 @@
using System.Collections.Concurrent; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;

Loading…
Cancel
Save

Powered by TurnKey Linux.