diff --git a/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs b/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs index 40ba933..0ae0b8d 100644 --- a/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs +++ b/Elfo.Wardein.Core/DependencyInjection/ServicesContainer.cs @@ -39,7 +39,7 @@ protected ServicesContainer() protected void Configure() { - serviceCollection = serviceCollection = new ServiceCollection() + serviceCollection = serviceCollection = new ServiceCollection() .AddSingleton>(sp => filePath => new MailConfigurationManagerFromJSON(filePath)) .AddSingleton>(sp => filePath => new WardeinConfigurationManagerFromJSON(filePath)) .AddTransient>>(sp => filePath => new WindowsServiceStatsPersistenceInJSON(filePath)) @@ -55,7 +55,7 @@ protected void Configure() throw new KeyNotFoundException($"Notification service {notificationType.ToString()} not supported yet"); } }) - .AddTransient>(sp => (serviceManagerType, serviceName) => + .AddTransient>(sp => (serviceManagerType, serviceName) => { switch (serviceManagerType) { @@ -66,8 +66,7 @@ protected void Configure() default: throw new KeyNotFoundException($"Notification service {serviceManagerType.ToString()} not supported yet"); } - }) - .AddTransient(); + }); serviceProvider = serviceCollection.BuildServiceProvider(); } @@ -129,8 +128,6 @@ public static IAmServiceManager ServiceManager(string serviceName, ServiceManage return instanceResolver(serviceManagerType, serviceName); } - public static WardeinInstance WardeinInstance => Current.serviceProvider.GetService(); - #endregion } } diff --git a/Elfo.Wardein.Core/Helpers/StringExtensions.cs b/Elfo.Wardein.Core/Helpers/StringExtensions.cs new file mode 100644 index 0000000..f4d8c4a --- /dev/null +++ b/Elfo.Wardein.Core/Helpers/StringExtensions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Elfo.Wardein.Core.Helpers +{ + public static class StringExtensions + { + public static string Repeat(this string source, int times) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + + return string.Concat(Enumerable.Repeat(source, times)); + } + } +} diff --git a/Elfo.Wardein.Core/Models/WardeinConfig.cs b/Elfo.Wardein.Core/Models/WardeinConfig.cs index a33af53..1b2ba2c 100644 --- a/Elfo.Wardein.Core/Models/WardeinConfig.cs +++ b/Elfo.Wardein.Core/Models/WardeinConfig.cs @@ -16,9 +16,6 @@ public class WardeinConfig [JsonProperty(PropertyName = "numberOfNotificationsWithoutRateLimitation")] public int NumberOfNotificationsWithoutRateLimitation { get; set; } = 2; // Default values - [JsonProperty(PropertyName = "services")] - public IList Services { get; set; } - [JsonProperty(PropertyName = "persistenceType")] public string PersistenceType { get; set; } = "JSON"; @@ -29,7 +26,5 @@ public class WardeinConfig IsInMaintenanceMode = false, MaintenanceModeStartDateInUTC = DateTime.UtcNow }; // Default values - [JsonProperty(PropertyName = "cleanUps")] - public CleanUps[] CleanUps { get; set; } } } diff --git a/Elfo.Wardein.Watchers/Elfo.Wardein.Watchers.csproj b/Elfo.Wardein.Watchers/Elfo.Wardein.Watchers.csproj new file mode 100644 index 0000000..e2848b8 --- /dev/null +++ b/Elfo.Wardein.Watchers/Elfo.Wardein.Watchers.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + + + + diff --git a/Elfo.Wardein.Core/Models/CleanUp/CleanUps.cs b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpConfig.cs similarity index 68% rename from Elfo.Wardein.Core/Models/CleanUp/CleanUps.cs rename to Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpConfig.cs index bf0331d..41c419f 100644 --- a/Elfo.Wardein.Core/Models/CleanUp/CleanUps.cs +++ b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpConfig.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Text; -namespace Elfo.Wardein.Core.Models +namespace Elfo.Wardein.Watchers.FileSystem { - public class CleanUps + public class FileSystemCleanUpConfig { [JsonProperty(PropertyName = "filePath")] public string FilePath { get; set; } @@ -14,6 +14,6 @@ public class CleanUps public double TimeSpanFromSeconds { get; set; } = 10; // Default values [JsonProperty(PropertyName = "cleanUpOptions")] - public CleanUpParams CleanUpOptions { get; set; } = new CleanUpParams(); + public FileSystemCleanUpOptions CleanUpOptions { get; set; } = new FileSystemCleanUpOptions(); } } diff --git a/Elfo.Wardein.Core/Models/CleanUp/CleanUpParams.cs b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpOptions.cs similarity index 90% rename from Elfo.Wardein.Core/Models/CleanUp/CleanUpParams.cs rename to Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpOptions.cs index d9f96e1..a3ae2a6 100644 --- a/Elfo.Wardein.Core/Models/CleanUp/CleanUpParams.cs +++ b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemCleanUpOptions.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Text; -namespace Elfo.Wardein.Core.Models +namespace Elfo.Wardein.Watchers.FileSystem { - public class CleanUpParams + public class FileSystemCleanUpOptions { [JsonProperty(PropertyName = "thresholdInSeconds")] public int ThresholdInSeconds { get; set; } diff --git a/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherCheckResult.cs b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherCheckResult.cs new file mode 100644 index 0000000..3e1d193 --- /dev/null +++ b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherCheckResult.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers.FileSystem +{ + public class WindowsServiceWatcherCheckResult : WatcherCheckResult + { + protected WindowsServiceWatcherCheckResult(FileSystemWatcher watcher, bool isValid, string description) : base(watcher, isValid, description) + { + } + + public static WindowsServiceWatcherCheckResult Create(FileSystemWatcher watcher, string description) + { + var wasRunSuccessful = string.IsNullOrWhiteSpace(description); + return new WindowsServiceWatcherCheckResult(watcher, wasRunSuccessful, description); + } + } +} diff --git a/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherConfig.cs b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherConfig.cs new file mode 100644 index 0000000..cf318a6 --- /dev/null +++ b/Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherConfig.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Elfo.Wardein.Watchers.FileSystem +{ + public class FileSystemWatcherConfig : IWatcherConfig + { + /// + /// Property defines if watcher has to be running in maintainance mode + /// Default value false + /// + [JsonProperty(PropertyName = "isInMaintenanceMode")] + public bool IsInMaintenanceMode { get; set; } = false; + + /// + /// Property that defines frequency of watcher polling + /// Default value 10 seconds + /// + [JsonProperty(PropertyName = "timeSpanFromSeconds")] + public double TimeSpanFromSeconds { get; set; } = 10; + + /// + /// List of folder configurations that has to be monitored and cleaned by criteria + /// + [JsonProperty(PropertyName = "cleanUps")] + public IEnumerable CleanUps { get; set; } = new List(); + } +} diff --git a/Elfo.Wardein.Watchers/FileSystem/Extensions.cs b/Elfo.Wardein.Watchers/FileSystem/Extensions.cs new file mode 100644 index 0000000..88b603b --- /dev/null +++ b/Elfo.Wardein.Watchers/FileSystem/Extensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Warden; +using Warden.Core; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers.FileSystem +{ + public static class Extensions + { + public static WardenConfiguration.Builder AddFileSystemWatcher(this WardenConfiguration.Builder builder, + FileSystemWatcherConfig config, + string group = null, + Action hooks = null) + { + builder.AddWatcher(FileSystemWatcher.Create(config, group), hooks, TimeSpan.FromSeconds(config.TimeSpanFromSeconds)); + return builder; + } + } +} diff --git a/Elfo.Wardein.Watchers/FileSystem/FileSystemWatcher.cs b/Elfo.Wardein.Watchers/FileSystem/FileSystemWatcher.cs new file mode 100644 index 0000000..3e61bde --- /dev/null +++ b/Elfo.Wardein.Watchers/FileSystem/FileSystemWatcher.cs @@ -0,0 +1,85 @@ +using Elfo.CleanUpManager; +using Elfo.Wardein.Core; +using Elfo.Wardein.Core.Helpers; +using Elfo.Wardein.Core.Models; +using NLog; +using PeterKottas.DotNetCore.WindowsService.Base; +using PeterKottas.DotNetCore.WindowsService.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers.FileSystem +{ + public class FileSystemWatcher : WardeinWatcher + { + protected FileSystemWatcher(FileSystemWatcherConfig config, string group = null) : base(nameof(FileSystemWatcher), config, group) + { } + + public static FileSystemWatcher Create(FileSystemWatcherConfig config, string group = null) + { + return new FileSystemWatcher(config, group); + } + + public override async Task ExecuteWatcherActionAsync() + { + log.Info("---\tStarting FileSystemWatcher\t---"); + + var resultDescription = new StringBuilder(string.Empty); + + foreach (var cleanUp in Config.CleanUps) + { + var iterationMessage = string.Empty; + try + { + var guid = Guid.NewGuid(); + log.Info($"{Environment.NewLine}{"-".Repeat(24)} Cache cleanup @ {guid} started {"-".Repeat(24)}"); + + var options = GetCleanUpOptions(cleanUp); + var filesProcessor = new Cleaner(log, options); + + filesProcessor.CleanUp(); + + log.Info($"{Environment.NewLine}{"-".Repeat(24)} Cache cleanup @ {guid} finished {"-".Repeat(24)}{Environment.NewLine.Repeat(3)}"); + } + catch (Exception ex) + { + iterationMessage = $"Exception inside path: {cleanUp.FilePath}: {ex.ToString()} stack trace: {ex.StackTrace}{Environment.NewLine}"; + resultDescription.AppendLine(iterationMessage); + log.Error(ex, iterationMessage); + } + } + + var watcherResultDescription = resultDescription.ToString(); + return await Task.FromResult(WindowsServiceWatcherCheckResult.Create(this, watcherResultDescription)); + } + + private CleanUpOptions GetCleanUpOptions(FileSystemCleanUpConfig cleanUp) + { + var options = new CleanUpOptions(cleanUp.FilePath); + options.RemoveEmptyFolders = cleanUp.CleanUpOptions.RemoveEmptyFolders; + options.DisplayOnly = cleanUp.CleanUpOptions.DisplayOnly; + options.RemoveEmptyFolders = cleanUp.CleanUpOptions.RemoveEmptyFolders; + options.UseRecycleBin = cleanUp.CleanUpOptions.UseRecycleBin; + options.Recursive = cleanUp.CleanUpOptions.Recursive; + ConfigureThreshold(); + return options; + + #region Local Functions + void ConfigureThreshold() + { + if (cleanUp.CleanUpOptions.ThresholdInSeconds != default(int) && cleanUp.CleanUpOptions.ThresholdInDays == default(int)) + options.Seconds = cleanUp.CleanUpOptions.ThresholdInSeconds; + else if (cleanUp.CleanUpOptions.ThresholdInSeconds == default(int) && cleanUp.CleanUpOptions.ThresholdInDays != default(int)) + options.Days = cleanUp.CleanUpOptions.ThresholdInDays; + else if (cleanUp.CleanUpOptions.ThresholdInSeconds != default(int) && cleanUp.CleanUpOptions.ThresholdInDays != default(int)) + options.Days = cleanUp.CleanUpOptions.ThresholdInDays; + else + options.Seconds = 300; + } + #endregion + } + } +} diff --git a/Elfo.Wardein.Watchers/IWatcherConfig.cs b/Elfo.Wardein.Watchers/IWatcherConfig.cs new file mode 100644 index 0000000..ebf3056 --- /dev/null +++ b/Elfo.Wardein.Watchers/IWatcherConfig.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Elfo.Wardein.Watchers +{ + public interface IWatcherConfig + { + bool IsInMaintenanceMode { get; } + + double TimeSpanFromSeconds { get; } + } +} diff --git a/Elfo.Wardein.Watchers/WardeinWatcher.cs b/Elfo.Wardein.Watchers/WardeinWatcher.cs new file mode 100644 index 0000000..c592c86 --- /dev/null +++ b/Elfo.Wardein.Watchers/WardeinWatcher.cs @@ -0,0 +1,41 @@ +using NLog; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers +{ + public abstract class WardeinWatcher : IWatcher where TConfig : IWatcherConfig + { + protected WardeinWatcher(string name, TConfig config, string group = null) + { + Name = name; + Group = group; + Config = config; + } + + public virtual string Name { get; protected set; } + + public virtual string Group { get; protected set; } + + public TConfig Config { get; protected set; } + + protected static ILogger log = LogManager.GetCurrentClassLogger(); + + public async Task ExecuteAsync() + { + if (Config.IsInMaintenanceMode) + { + var message = $"Wardein {Name} running in maintainance mode. Skipping Execution."; + log.Info(message); + return await Task.FromResult(WatcherCheckResult.Create(this, true, message)); + } + + return await ExecuteWatcherActionAsync(); + } + + public abstract Task ExecuteWatcherActionAsync(); + } +} diff --git a/Elfo.Wardein.Core/Models/WindowsService.cs b/Elfo.Wardein.Watchers/WindowsService/Config/ObservableWindowsService.cs similarity index 92% rename from Elfo.Wardein.Core/Models/WindowsService.cs rename to Elfo.Wardein.Watchers/WindowsService/Config/ObservableWindowsService.cs index b241033..803a283 100644 --- a/Elfo.Wardein.Core/Models/WindowsService.cs +++ b/Elfo.Wardein.Watchers/WindowsService/Config/ObservableWindowsService.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Text; -namespace Elfo.Wardein.Core.Models +namespace Elfo.Wardein.Watchers.WindowsService { - public class WindowsService + public class ObservableWindowsService { [JsonProperty(PropertyName = "serviceName")] public string ServiceName { get; set; } diff --git a/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherCheckResult.cs b/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherCheckResult.cs new file mode 100644 index 0000000..f09ffa6 --- /dev/null +++ b/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherCheckResult.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers.WindowsService +{ + public class WindowsServiceWatcherCheckResult : WatcherCheckResult + { + protected WindowsServiceWatcherCheckResult(WindowsServiceWatcher watcher, bool isValid, string description) : base(watcher, isValid, description) + { + } + + public static WindowsServiceWatcherCheckResult Create(WindowsServiceWatcher watcher, string description) + { + var wasRunSuccessful = string.IsNullOrWhiteSpace(description); + return new WindowsServiceWatcherCheckResult(watcher, wasRunSuccessful, description); + } + } +} diff --git a/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherConfig.cs b/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherConfig.cs new file mode 100644 index 0000000..d96de78 --- /dev/null +++ b/Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherConfig.cs @@ -0,0 +1,33 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Elfo.Wardein.Watchers.WindowsService +{ + public class WindowsServiceWatcherConfig : IWatcherConfig + { + /// + /// Property defines if watcher has to be running in maintainance mode + /// Default value false + /// + [JsonProperty(PropertyName = "isInMaintenanceMode")] + public bool IsInMaintenanceMode { get; set; } = false; + + /// + /// Property that defines frequency of watcher polling + /// Default value 10 seconds + /// + [JsonProperty(PropertyName = "timeSpanFromSeconds")] + public double TimeSpanFromSeconds { get; set; } = 10; + + [JsonProperty(PropertyName = "sendRepeatedNotificationAfterSeconds")] + public double SendRepeatedNotificationAfterSeconds { get; set; } = 3600; // Default values + + [JsonProperty(PropertyName = "numberOfNotificationsWithoutRateLimitation")] + public int NumberOfNotificationsWithoutRateLimitation { get; set; } = 2; // Default values + + [JsonProperty(PropertyName = "services")] + public IEnumerable Services { get; set; } + } +} diff --git a/Elfo.Wardein.Watchers/WindowsService/Extensions.cs b/Elfo.Wardein.Watchers/WindowsService/Extensions.cs new file mode 100644 index 0000000..b9a99d3 --- /dev/null +++ b/Elfo.Wardein.Watchers/WindowsService/Extensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Warden; +using Warden.Core; +using Warden.Watchers; + +namespace Elfo.Wardein.Watchers.WindowsService +{ + public static class Extensions + { + public static WardenConfiguration.Builder AddWindowsServiceWatcher(this WardenConfiguration.Builder builder, + WindowsServiceWatcherConfig config, + string group = null, + Action hooks = null) + { + builder.AddWatcher(WindowsServiceWatcher.Create(config, group), hooks, TimeSpan.FromSeconds(config.TimeSpanFromSeconds)); + return builder; + } + } +} diff --git a/Elfo.Wardein.Core/WardeinInstance.cs b/Elfo.Wardein.Watchers/WindowsService/WindowsServiceWatcher.cs similarity index 73% rename from Elfo.Wardein.Core/WardeinInstance.cs rename to Elfo.Wardein.Watchers/WindowsService/WindowsServiceWatcher.cs index 816c1a8..7ef5118 100644 --- a/Elfo.Wardein.Core/WardeinInstance.cs +++ b/Elfo.Wardein.Watchers/WindowsService/WindowsServiceWatcher.cs @@ -1,76 +1,50 @@ -using Elfo.Wardein.Core.ConfigurationReader; -using Elfo.Wardein.Core.Helpers; -using Elfo.Wardein.Core.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Linq; -using System.Threading.Tasks; +using Elfo.Wardein.Core; using Elfo.Wardein.Core.Abstractions; -using Elfo.Wardein.Core.Persistence; +using Elfo.Wardein.Core.Helpers; using Elfo.Wardein.Core.NotificationService; -using Microsoft.Extensions.DependencyInjection; -using NLog; using Elfo.Wardein.Core.ServiceManager; +using System; +using System.Threading.Tasks; +using Warden.Watchers; -namespace Elfo.Wardein.Core +namespace Elfo.Wardein.Watchers.WindowsService { - public class WardeinInstance + public class WindowsServiceWatcher : WardeinWatcher { - #region Private variables - - private WardeinConfig wardeinConfig = null; - private readonly static Logger log = LogManager.GetCurrentClassLogger(); - private readonly IAmWardeinConfigurationManager wardeinConfigurationReader; + protected WindowsServiceWatcher(WindowsServiceWatcherConfig config, string name, string group = null) : base(name, config, group) + { } - #endregion - - #region Constructor - - public WardeinInstance() + public static WindowsServiceWatcher Create(WindowsServiceWatcherConfig config, string group = null) { - this.wardeinConfigurationReader = ServicesContainer.WardeinConfigurationManager(Const.WARDEIN_CONFIG_PATH); - - GetWarderinConfigAndThrowErrorIfNotExist(); - - #region Local Functions + return new WindowsServiceWatcher(config, $"{nameof(WindowsServiceWatcher)}", group); + } - void GetWarderinConfigAndThrowErrorIfNotExist() + public override async Task ExecuteWatcherActionAsync() + { + log.Info($"---\tStarting {Name}\t---"); + try { - if (!File.Exists(Const.WARDEIN_CONFIG_PATH)) - { - //TODO: throw error or something... - } - else - { - this.wardeinConfig = wardeinConfigurationReader.GetConfiguration(); - if (this.wardeinConfig == null) - throw new ArgumentNullException("Wardein configuration not found or not well formatted"); - } + var guid = Guid.NewGuid(); + log.Info($"{Environment.NewLine}{"-".Repeat(24)} Services health check @ {guid} started {"-".Repeat(24)}"); + await RunCheck(); + log.Info($"{Environment.NewLine}{"-".Repeat(24)} Services health check @ {guid} finished {"-".Repeat(24)}{Environment.NewLine.Repeat(24)}"); + } + catch (Exception ex) + { + log.Error(ex, $"Exception inside polling action: {ex.ToString()}\n"); } - #endregion + return await Task.FromResult(null); } - #endregion - - public async Task RunCheck() + protected async Task RunCheck() { log.Info($"{Environment.NewLine}> CHECKING SERVICES HEALTH"); - if (this.wardeinConfigurationReader.IsInMaintenanceMode) - { - log.Info("Wardein is in maintenance mode."); - return; - } - - foreach (var service in wardeinConfig.Services) + foreach (var service in Config.Services) { - await Task.Delay(TimeSpan.FromMilliseconds(250)); // TODO: Do we really need this? - using (var persistenceService = ServicesContainer.PersistenceService(Const.DB_PATH)) { IAmServiceManager serviceManager = GetServiceManager(); @@ -91,7 +65,6 @@ public async Task RunCheck() persistenceService.CreateOrUpdateCachedEntity(item); - #region Local Functions IAmServiceManager GetServiceManager() @@ -151,7 +124,7 @@ bool IsRepeatedMailTimeoutElapsed() return DateTime.UtcNow.Subtract(item.LastNotificationSentAtThisTimeUTC.GetValueOrDefault(DateTime.MinValue)) >= timeout; } - bool NeverSentANotificationBefore() => item.LastNotificationSentAtThisTimeUTC.HasValue == false; + bool NeverSentANotificationBefore() => item.LastNotificationSentAtThisTimeUTC.HasValue == false; #endregion } @@ -175,11 +148,11 @@ async Task PerformActionOnServiceRestored() } TimeSpan GetServiceSendRepeatedNotificationAfterSecondsOrDefault() => - TimeSpan.FromSeconds(service.SendRepeatedNotificationAfterSeconds.GetValueOrDefault(wardeinConfig.SendRepeatedNotificationAfterSeconds)); + TimeSpan.FromSeconds(service.SendRepeatedNotificationAfterSeconds.GetValueOrDefault(Config.SendRepeatedNotificationAfterSeconds)); int GetServiceNumberOfNotificationWithoutRateLimitationOrDefault() { - var result = service.NumberOfNotificationsWithoutRateLimitation.GetValueOrDefault(wardeinConfig.NumberOfNotificationsWithoutRateLimitation); + var result = service.NumberOfNotificationsWithoutRateLimitation.GetValueOrDefault(Config.NumberOfNotificationsWithoutRateLimitation); if (result <= 0) return int.MaxValue; diff --git a/Elfo.Wardein.sln b/Elfo.Wardein.sln index 00688c6..32c837c 100644 --- a/Elfo.Wardein.sln +++ b/Elfo.Wardein.sln @@ -3,24 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27703.2026 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elfo.Wardein.Services", "Elfo.Wardein.Services\Elfo.Wardein.Services.csproj", "{0D1C46CE-0291-4D7F-BAAA-582F210A7278}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elfo.Wardein.Core", "Elfo.Wardein.Core\Elfo.Wardein.Core.csproj", "{EB0438FD-BA9B-428E-88C7-430A0FB3AA53}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elfo.Wardein", "Elfo.Wardein\Elfo.Wardein.csproj", "{7EED5720-57EC-49E7-B831-C3E5538E7B3E}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elfo.Wardein.APIs", "Elfo.Wardein.APIs\Elfo.Wardein.APIs.csproj", "{56FD636F-0F4E-4644-8713-6AA1FDFF713B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elfo.Wardein.Watchers", "Elfo.Wardein.Watchers\Elfo.Wardein.Watchers.csproj", "{69EDA8D3-51B4-4986-A6AD-E57514071EEC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {0D1C46CE-0291-4D7F-BAAA-582F210A7278}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0D1C46CE-0291-4D7F-BAAA-582F210A7278}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0D1C46CE-0291-4D7F-BAAA-582F210A7278}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0D1C46CE-0291-4D7F-BAAA-582F210A7278}.Release|Any CPU.Build.0 = Release|Any CPU {EB0438FD-BA9B-428E-88C7-430A0FB3AA53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EB0438FD-BA9B-428E-88C7-430A0FB3AA53}.Debug|Any CPU.Build.0 = Debug|Any CPU {EB0438FD-BA9B-428E-88C7-430A0FB3AA53}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -33,6 +29,10 @@ Global {56FD636F-0F4E-4644-8713-6AA1FDFF713B}.Debug|Any CPU.Build.0 = Debug|Any CPU {56FD636F-0F4E-4644-8713-6AA1FDFF713B}.Release|Any CPU.ActiveCfg = Release|Any CPU {56FD636F-0F4E-4644-8713-6AA1FDFF713B}.Release|Any CPU.Build.0 = Release|Any CPU + {69EDA8D3-51B4-4986-A6AD-E57514071EEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69EDA8D3-51B4-4986-A6AD-E57514071EEC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69EDA8D3-51B4-4986-A6AD-E57514071EEC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69EDA8D3-51B4-4986-A6AD-E57514071EEC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Elfo.Wardein/Elfo.Wardein.csproj b/Elfo.Wardein/Elfo.Wardein.csproj index c1ce19a..de9adf6 100644 --- a/Elfo.Wardein/Elfo.Wardein.csproj +++ b/Elfo.Wardein/Elfo.Wardein.csproj @@ -15,7 +15,7 @@ - + diff --git a/Elfo.Wardein/Program.cs b/Elfo.Wardein/Program.cs index 9048063..6793a87 100644 --- a/Elfo.Wardein/Program.cs +++ b/Elfo.Wardein/Program.cs @@ -1,5 +1,5 @@ using Elfo.Wardein.APIs; -using Elfo.Wardein.Services; +using Elfo.Wardein.Watchers; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using NLog; @@ -17,93 +17,60 @@ static void Main(string[] args) { try { - var wardeinMicroService = new WardeinService(); - var cacheCleanUpService = new CacheCleanUpService(); + var wardeinService = new WardeinMicroService(); - new Thread(() => - { - Thread.CurrentThread.IsBackground = true; - - log.Debug("Starting APIs..."); - ConfigureAPIHosting(); - log.Debug("APIs started"); - - #region Local Functions - void ConfigureAPIHosting() - { - new WebHostBuilder() - .UseUrls("http://*:5000") - .UseKestrel() - .ConfigureServices(serviceCollection => - { - serviceCollection.AddSingleton(wardeinMicroService); - }) - .UseStartup() - .Build() - .Run(); - } - #endregion - }).Start(); - - new Thread(() => - { - ServiceRunner.Run(config => - { - var name = config.GetDefaultName(); - config.Service(serviceConfig => - { - serviceConfig.ServiceFactory((extraArguments, controller) => - { - return wardeinMicroService; - }); - - serviceConfig.OnStart((service, extraParams) => - { - log.Info("Service {0} started", name); - service.Start(); - }); + //new Thread(() => + //{ + // Thread.CurrentThread.IsBackground = true; - serviceConfig.OnStop(service => - { - log.Info("Service {0} stopped", name); - service.Stop(); - }); + // log.Debug("Starting APIs..."); + // ConfigureAPIHosting(); + // log.Debug("APIs started"); - serviceConfig.OnError(e => - { - log.Error("Service {0} errored with exception : {1}", name, e.Message); - }); - }); - }); - }).Start(); + // #region Local Functions + // void ConfigureAPIHosting() + // { + // new WebHostBuilder() + // .UseUrls("http://*:5000") + // .UseKestrel() + // .ConfigureServices(serviceCollection => + // { + // serviceCollection.AddSingleton(wardeinService); + // }) + // .UseStartup() + // .Build() + // .Run(); + // } + // #endregion + //}).Start(); new Thread(() => { - ServiceRunner.Run(config => + ServiceRunner.Run(config => { var name = config.GetDefaultName(); config.Service(serviceConfig => { serviceConfig.ServiceFactory((extraArguments, controller) => { - return cacheCleanUpService; + return wardeinService; }); serviceConfig.OnStart((service, extraParams) => { - log.Info("Service {0} started", name); + log.Info($"Service {name} started"); service.Start(); }); serviceConfig.OnStop(service => { - log.Info("Service {0} stopped", name); + log.Info($"Service {name} stopped"); service.Stop(); }); serviceConfig.OnError(e => { - log.Error("Service {0} errored with exception : {1}", name, e.Message); + log.Error($"Service {name} errored with exception : {e.Message}"); }); }); }); @@ -111,7 +78,7 @@ void ConfigureAPIHosting() } catch (Exception ex) { - log.Error("Fatal error in Program: {0}", ex.ToString()); + log.Error($"Fatal error in Program: {ex.ToString()}"); throw; } } diff --git a/Elfo.Wardein/WardeinMicroService.cs b/Elfo.Wardein/WardeinMicroService.cs new file mode 100644 index 0000000..9c22fa6 --- /dev/null +++ b/Elfo.Wardein/WardeinMicroService.cs @@ -0,0 +1,21 @@ +using PeterKottas.DotNetCore.WindowsService.Base; +using PeterKottas.DotNetCore.WindowsService.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Elfo.Wardein +{ + internal class WardeinMicroService : MicroService, IMicroService + { + public void Start() + { + throw new NotImplementedException(); + } + + public void Stop() + { + throw new NotImplementedException(); + } + } +}