Skip to content

Commit

Permalink
daemon refactor
Browse files Browse the repository at this point in the history
- split daemon up into multiple implementations
- daemons now use cronmask to tick
  • Loading branch information
shukriadams committed Dec 17, 2022
1 parent e60e056 commit 73429ed
Show file tree
Hide file tree
Showing 21 changed files with 428 additions and 340 deletions.
2 changes: 1 addition & 1 deletion src/Tetrifact.Core/EnvironmentArgsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static int GetAsInt(string name, int defaultValue)
string value = Environment.GetEnvironmentVariable(name);
int val;
if (!int.TryParse(Environment.GetEnvironmentVariable(name), out val)){
Console.WriteLine($"Environment variable {value} expected to be integer, but could not be parsed, falling back to default {defaultValue}");
Console.WriteLine($"Environment variable \"{value}\" expected to be integer, but could not be parsed, falling back to default {defaultValue}");
return defaultValue;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Tetrifact.Core/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,20 @@ public interface ISettings
/// Interval in hours for metrics to be regenerated.
/// </summary>
int MetricsGenerationInterval { get; set; }

/// <summary>
///
/// </summary>
string CleanCronMask { get; set; }

/// <summary>
///
/// </summary>
string PruneCronMask { get; set; }

/// <summary>
///
/// </summary>
string MetricsCronMask { get; set; }
}
}
14 changes: 13 additions & 1 deletion src/Tetrifact.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public class Settings : ISettings

public string LogLevel{ get; set; }

public string CleanCronMask { get; set; }

public string PruneCronMask { get; set; }

public string MetricsCronMask { get; set; }

#endregion

#region CTORS
Expand Down Expand Up @@ -124,7 +130,10 @@ public Settings()
this.ArchivePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "data", "archives");
this.TagsPath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "data", "tags");
this.MetricsPath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "data", "metrics");

this.CleanCronMask = "0 * * * *"; // ever hour on the hour
this.PruneCronMask = "0 0 * * *"; // once a day at midnight
this.MetricsCronMask = "0 */12 * * *"; // every 12 hours

// try to overrride defaults from environment variables
this.AllowPackageDelete = this.TryGetSetting("ALLOW_PACKAGE_DELETE", this.AllowPackageDelete);
this.AllowPackageCreate = this.TryGetSetting("ALLOW_PACKAGE_CREATE", this.AllowPackageCreate);
Expand Down Expand Up @@ -155,6 +164,9 @@ public Settings()
this.ArchivePath = this.TryGetSetting("ARCHIVE_PATH", this.ArchivePath);
this.TagsPath = this.TryGetSetting("TAGS_PATH", this.TagsPath);
this.MetricsPath = this.TryGetSetting("METRICS_PATH", this.MetricsPath);
this.CleanCronMask = this.TryGetSetting("CLEAN_CRON_MASK", this.CleanCronMask);
this.PruneCronMask = this.TryGetSetting("PRUNE_CRON_MASK", this.PruneCronMask);
this.MetricsPath = this.TryGetSetting("METRICS_CRON_MASK", this.MetricsPath);

string downloadArchiveCompressionEnvVar = Environment.GetEnvironmentVariable("DOWNLOAD_ARCHIVE_COMPRESSION");
if (downloadArchiveCompressionEnvVar == "0")
Expand Down
7 changes: 3 additions & 4 deletions src/Tetrifact.Tests/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ public override void Load()
Bind<IPackagePruneService>().To<PackagePruneService>();
Bind<IPackageDiffService>().To<PackageDiffService>();
Bind<IArchiveService>().To<Core.ArchiveService>();
Bind<W.IDaemon>().To< W.Daemon>();
Bind<ILock>().To<ProcessLock>();
Bind<IMetricsService>().To<MetricsService>();
Bind<ISystemCallsService>().To<SystemCallsService>();
Bind<ILockProvider>().To<Core.LockProvider>();
Bind<W.IDaemonBackgroundProcess>().To<W.DaemonBackgroundProcess>();
Bind<IHostApplicationLifetime>().To<TestHostApplicationLifetime>();

Bind<W.IDaemon>().To<TestDaemon>();

Bind<ILogger<W.PackagesController>>().To<TestLogger<W.PackagesController>>();
Bind<ILogger<W.CleanController>>().To<TestLogger<W.CleanController>>();
Bind<ILogger<W.FilesController>>().To<TestLogger<W.FilesController>>();
Expand All @@ -54,7 +53,7 @@ public override void Load()
Bind<ILogger<IIndexReadService>>().To<TestLogger<IIndexReadService>>();
Bind<ILogger<IPackagePruneService>>().To<TestLogger<IPackagePruneService>>();
Bind<ILogger<W.IDaemon>>().To<TestLogger<W.IDaemon>>();
Bind<ILogger<W.IDaemonBackgroundProcess>>().To<TestLogger<W.IDaemonBackgroundProcess>>();
Bind<ILogger<W.IDaemon>>().To<TestLogger<W.IDaemon>>();
}
}
}
75 changes: 75 additions & 0 deletions src/Tetrifact.Tests/Helpers/MoqHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Moq;
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Tetrifact.Tests
{
public class MoqHelper
{
private static MockRepository repo = new MockRepository(MockBehavior.Loose) { CallBase = true };

public static Mock CreateMock(Type typeToMock)
{
var creator = typeof(Mock<>).MakeGenericType(typeToMock);
return (Mock)Activator.CreateInstance(creator);
}

public static IEnumerable<object> CtorArgs(Type t, object overrid, bool forceMock)
{
return CtorArgs(t, new object[] { overrid }, forceMock);
}

public static IEnumerable<object> CtorArgs(Type t, object[] overrides, bool forceMock)
{
ConstructorInfo ctor = t.GetConstructors().FirstOrDefault();
if (ctor == null)
throw new Exception("no ctor found");

StandardKernel kernel = NinjectHelper.Kernel();

List<object> args = new List<object>();
foreach (ParameterInfo p in ctor.GetParameters())
{
object arg = overrides.Where(r => r.GetType() == p.ParameterType).SingleOrDefault();
if (arg == null)
arg = overrides.Where(r => p.ParameterType.IsAssignableFrom(r.GetType())).SingleOrDefault();

try
{
if (arg == null && !forceMock)
arg = kernel.Get(p.ParameterType);
}
catch (Exception ex)
{
// ignore
}

try
{
arg = CreateMock(p.ParameterType).Object;
}
catch (Exception ex)
{
throw new Exception($"failed to create ctor arg with both ninject and moq, type {t.Name}, parameter {p.ParameterType}", ex);
}

args.Add(arg);
}

return args;
}

public static T WithAllMocked<T>() where T : class
{
return repo.Create<T>(CtorArgs(typeof(T), new object[]{ }, true).ToArray()).Object;
}

public static T With<T>(Mock dependencyMock) where T : class
{
return repo.Create<T>(CtorArgs(typeof(T), dependencyMock.Object, false).ToArray()).Object;
}
}
}
7 changes: 7 additions & 0 deletions src/Tetrifact.Tests/Helpers/NinjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace Tetrifact.Tests
{
public class NinjectHelper
{
public static StandardKernel Kernel()
{
StandardKernel kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}

private static T Get<T>(ConstructorArgument[] args)
{
StandardKernel kernel = new StandardKernel();
Expand Down
17 changes: 17 additions & 0 deletions src/Tetrifact.Tests/Shims/TestDaemon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Tetrifact.Web;

namespace Tetrifact.Tests
{
public class TestDaemon : IDaemon
{
public void Dispose()
{

}

public void Start(Cron Daemon)
{

}
}
}
37 changes: 37 additions & 0 deletions src/Tetrifact.Tests/Web/Core/CleanerCron/Start.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Moq;
using System;
using Tetrifact.Core;
using Xunit;
using W=Tetrifact.Web;

namespace Tetrifact.Tests.Web.Core.Daemon
{
public class Start : TestBase
{
/// <summary>
/// Coverage test
/// </summary>
[Fact]
public void Happy_path()
{
var cleanerCron = MoqHelper.WithAllMocked<W.CleanerCron>();
cleanerCron.Work();
}

/// <summary>
/// Coverage test
/// </summary>
[Fact]
public void Archive_Exception()
{
var archiveService = new Mock<IArchiveService>();
archiveService
.Setup(r => r.PurgeOldArchives())
.Throws(new Exception("some error"));

var cleanerCron = MoqHelper.With<W.CleanerCron>(archiveService);
cleanerCron.Work();
}

}
}
18 changes: 0 additions & 18 deletions src/Tetrifact.Tests/Web/Core/Daemon/Dispose.cs

This file was deleted.

112 changes: 0 additions & 112 deletions src/Tetrifact.Tests/Web/Core/Daemon/Start.cs

This file was deleted.

Loading

0 comments on commit 73429ed

Please sign in to comment.