-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
374694c
commit 005bf43
Showing
21 changed files
with
420 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Elfo.CleanUpManager" Version="0.0.12" /> | ||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" /> | ||
<PackageReference Include="PeterKottas.DotNetCore.WindowsService" Version="2.0.11" /> | ||
<PackageReference Include="Warden" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Elfo.Wardein.APIs\Elfo.Wardein.APIs.csproj" /> | ||
<ProjectReference Include="..\Elfo.Wardein.Core\Elfo.Wardein.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherCheckResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Elfo.Wardein.Watchers/FileSystem/Config/FileSystemWatcherConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/// <summary> | ||
/// Property defines if watcher has to be running in maintainance mode | ||
/// Default value false | ||
/// </summary> | ||
[JsonProperty(PropertyName = "isInMaintenanceMode")] | ||
public bool IsInMaintenanceMode { get; set; } = false; | ||
|
||
/// <summary> | ||
/// Property that defines frequency of watcher polling | ||
/// Default value 10 seconds | ||
/// </summary> | ||
[JsonProperty(PropertyName = "timeSpanFromSeconds")] | ||
public double TimeSpanFromSeconds { get; set; } = 10; | ||
|
||
/// <summary> | ||
/// List of folder configurations that has to be monitored and cleaned by criteria | ||
/// </summary> | ||
[JsonProperty(PropertyName = "cleanUps")] | ||
public IEnumerable<FileSystemCleanUpConfig> CleanUps { get; set; } = new List<FileSystemCleanUpConfig>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<WatcherHooksConfiguration.Builder> hooks = null) | ||
{ | ||
builder.AddWatcher(FileSystemWatcher.Create(config, group), hooks, TimeSpan.FromSeconds(config.TimeSpanFromSeconds)); | ||
return builder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FileSystemWatcherConfig> | ||
{ | ||
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<IWatcherCheckResult> 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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TConfig> : 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<IWatcherCheckResult> 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<IWatcherCheckResult> ExecuteWatcherActionAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Elfo.Wardein.Watchers/WindowsService/Config/WindowsServiceWatcherCheckResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.