Skip to content

Commit

Permalink
Removing SystemClock and IMetricsClient (#305)
Browse files Browse the repository at this point in the history
* Removing SystemClock and IMetricsClient

* Respond to feedback and revert some API changes

* Fix test

* More test cleanup

* Fix broken tests

* Fix test

* Increase max metrics

* PR feedback

* Try fixing flaky test
  • Loading branch information
ejsmith authored Aug 8, 2024
1 parent 5a8436e commit 7c501e3
Show file tree
Hide file tree
Showing 87 changed files with 514 additions and 2,896 deletions.
12 changes: 0 additions & 12 deletions Foundatio.All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{A1DF
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio", "src\Foundatio\Foundatio.csproj", "{392A3FAB-0067-4A9E-968A-0919B565B51C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.MetricsNET", "src\Foundatio.MetricsNET\Foundatio.MetricsNET.csproj", "{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.HostingSample", "samples\Foundatio.HostingSample\Foundatio.HostingSample.csproj", "{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.TestHarness", "src\Foundatio.TestHarness\Foundatio.TestHarness.csproj", "{6A0F1A4B-C0D2-423C-9B9D-7E75B91B41EE}"
Expand All @@ -36,8 +34,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.JsonNet", "src\Fo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.Utf8Json", "src\Foundatio.Utf8Json\Foundatio.Utf8Json.csproj", "{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.AppMetrics", "src\Foundatio.AppMetrics\Foundatio.AppMetrics.csproj", "{D97811C5-186A-4ED3-8675-925A7ACD32F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.Extensions.Hosting", "src\Foundatio.Extensions.Hosting\Foundatio.Extensions.Hosting.csproj", "{FC503293-E2AD-4FE8-856E-F2605D0422D1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.MessagePack", "src\Foundatio.MessagePack\Foundatio.MessagePack.csproj", "{1D0CF7B3-D0B3-4739-A280-5C0822169E08}"
Expand Down Expand Up @@ -116,10 +112,6 @@ Global
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Release|Any CPU.Build.0 = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|Any CPU.Build.0 = Release|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -144,10 +136,6 @@ Global
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Release|Any CPU.Build.0 = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|Any CPU.Build.0 = Release|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
28 changes: 0 additions & 28 deletions Foundatio.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{A1DF
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio", "src\Foundatio\Foundatio.csproj", "{392A3FAB-0067-4A9E-968A-0919B565B51C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.MetricsNET", "src\Foundatio.MetricsNET\Foundatio.MetricsNET.csproj", "{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.HostingSample", "samples\Foundatio.HostingSample\Foundatio.HostingSample.csproj", "{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.TestHarness", "src\Foundatio.TestHarness\Foundatio.TestHarness.csproj", "{6A0F1A4B-C0D2-423C-9B9D-7E75B91B41EE}"
Expand All @@ -35,8 +33,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.JsonNet", "src\Fo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.Utf8Json", "src\Foundatio.Utf8Json\Foundatio.Utf8Json.csproj", "{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.AppMetrics", "src\Foundatio.AppMetrics\Foundatio.AppMetrics.csproj", "{D97811C5-186A-4ED3-8675-925A7ACD32F6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.Extensions.Hosting", "src\Foundatio.Extensions.Hosting\Foundatio.Extensions.Hosting.csproj", "{FC503293-E2AD-4FE8-856E-F2605D0422D1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundatio.MessagePack", "src\Foundatio.MessagePack\Foundatio.MessagePack.csproj", "{1D0CF7B3-D0B3-4739-A280-5C0822169E08}"
Expand Down Expand Up @@ -65,18 +61,6 @@ Global
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Release|x64.Build.0 = Release|Any CPU
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Release|x86.ActiveCfg = Release|Any CPU
{392A3FAB-0067-4A9E-968A-0919B565B51C}.Release|x86.Build.0 = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|x64.ActiveCfg = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|x64.Build.0 = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|x86.ActiveCfg = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Debug|x86.Build.0 = Debug|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|Any CPU.Build.0 = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|x64.ActiveCfg = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|x64.Build.0 = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|x86.ActiveCfg = Release|Any CPU
{F9AEEE11-9E18-4FFE-A962-EB7281BCD561}.Release|x86.Build.0 = Release|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39AFF0C0-D64A-4D57-871F-17D9BF2E5A93}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -149,18 +133,6 @@ Global
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Release|x64.Build.0 = Release|Any CPU
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Release|x86.ActiveCfg = Release|Any CPU
{44A5A11C-4D9E-4219-9E56-46F07E37A3B0}.Release|x86.Build.0 = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|x64.ActiveCfg = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|x64.Build.0 = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|x86.ActiveCfg = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Debug|x86.Build.0 = Debug|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|Any CPU.Build.0 = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|x64.ActiveCfg = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|x64.Build.0 = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|x86.ActiveCfg = Release|Any CPU
{D97811C5-186A-4ED3-8675-925A7ACD32F6}.Release|x86.Build.0 = Release|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC503293-E2AD-4FE8-856E-F2605D0422D1}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down
5 changes: 2 additions & 3 deletions samples/Foundatio.HostingSample/Jobs/Sample2Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Jobs;
using Foundatio.Utility;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;

Expand All @@ -22,7 +21,7 @@ public Sample2Job(ILoggerFactory loggerFactory)

public Task<JobResult> RunAsync(CancellationToken cancellationToken = default)
{
_lastRun = SystemClock.UtcNow;
_lastRun = DateTime.UtcNow;
Interlocked.Increment(ref _iterationCount);
if (_logger.IsEnabled(LogLevel.Information))
_logger.LogTrace("Sample2Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId);
Expand All @@ -35,7 +34,7 @@ public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, Canc
if (!_lastRun.HasValue)
return Task.FromResult(HealthCheckResult.Healthy("Job has not been run yet."));

if (SystemClock.UtcNow.Subtract(_lastRun.Value) > TimeSpan.FromSeconds(5))
if (DateTime.UtcNow.Subtract(_lastRun.Value) > TimeSpan.FromSeconds(5))
return Task.FromResult(HealthCheckResult.Unhealthy("Job has not run in the last 5 seconds."));

return Task.FromResult(HealthCheckResult.Healthy("Job has run in the last 5 seconds."));
Expand Down
2 changes: 0 additions & 2 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Foundatio.Caching;
using Foundatio.Extensions.Hosting.Jobs;
using Foundatio.Extensions.Hosting.Startup;
using Foundatio.Jobs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down
35 changes: 0 additions & 35 deletions src/Foundatio.AppMetrics/AppMetricsClient.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Foundatio.AppMetrics/Foundatio.AppMetrics.csproj

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PackageTags>$(PackageTags);DataProtection</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="6.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Foundatio\Foundatio.csproj" />
Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions.Hosting.Startup;
using Foundatio.Jobs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down
12 changes: 6 additions & 6 deletions src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class ScheduledJobRunner
private readonly ScheduledJobOptions _jobOptions;
private readonly IServiceProvider _serviceProvider;
private readonly ICacheClient _cacheClient;
private readonly ISystemClock _systemClock;
private readonly TimeProvider _timeProvider;
private CronExpression _cronSchedule;
private readonly ILockProvider _lockProvider;
private readonly ILogger _logger;
Expand All @@ -30,7 +30,7 @@ public ScheduledJobRunner(ScheduledJobOptions jobOptions, IServiceProvider servi
_jobOptions = jobOptions;
_jobOptions.Name ??= Guid.NewGuid().ToString("N").Substring(0, 10);
_serviceProvider = serviceProvider;
_systemClock = serviceProvider.GetService<ISystemClock>() ?? SystemClock.Instance;
_timeProvider = serviceProvider.GetService<TimeProvider>() ?? TimeProvider.System;
_cacheClient = new ScopedCacheClient(cacheClient, "jobs");
_logger = loggerFactory?.CreateLogger<ScheduledJobRunner>() ?? NullLogger<ScheduledJobRunner>.Instance;

Expand All @@ -40,7 +40,7 @@ public ScheduledJobRunner(ScheduledJobOptions jobOptions, IServiceProvider servi

var interval = TimeSpan.FromDays(1);

var nextOccurrence = _cronSchedule.GetNextOccurrence(_systemClock.UtcNow());
var nextOccurrence = _cronSchedule.GetNextOccurrence(_timeProvider.GetUtcNow().UtcDateTime);
if (nextOccurrence.HasValue)
{
var nextNextOccurrence = _cronSchedule.GetNextOccurrence(nextOccurrence.Value);
Expand All @@ -50,7 +50,7 @@ public ScheduledJobRunner(ScheduledJobOptions jobOptions, IServiceProvider servi

_lockProvider = new ThrottlingLockProvider(_cacheClient, 1, interval.Add(interval));

NextRun = _cronSchedule.GetNextOccurrence(_systemClock.UtcNow());
NextRun = _cronSchedule.GetNextOccurrence(_timeProvider.GetUtcNow().UtcDateTime);
}

public ScheduledJobOptions Options => _jobOptions;
Expand All @@ -62,7 +62,7 @@ public string Schedule
set
{
_cronSchedule = CronExpression.Parse(value);
NextRun = _cronSchedule.GetNextOccurrence(_systemClock.UtcNow());
NextRun = _cronSchedule.GetNextOccurrence(_timeProvider.GetUtcNow().UtcDateTime);
_schedule = value;
}
}
Expand All @@ -87,7 +87,7 @@ public async ValueTask<bool> ShouldRunAsync()
return false;

// not time yet
if (NextRun > _systemClock.UtcNow())
if (NextRun > _timeProvider.GetUtcNow().UtcDateTime)
return false;

// check if already run
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions.Hosting.Startup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ internal void MarkStartupComplete(RunStartupActionsResult result)
public async Task<RunStartupActionsResult> WaitForStartupAsync(CancellationToken cancellationToken, TimeSpan? maxTimeToWait = null)
{
bool isFirstWaiter = Interlocked.Increment(ref _waitCount) == 1;
var startTime = SystemClock.UtcNow;
var lastStatus = SystemClock.UtcNow;
var startTime = DateTime.UtcNow;
var lastStatus = DateTime.UtcNow;
maxTimeToWait ??= TimeSpan.FromMinutes(5);

while (!cancellationToken.IsCancellationRequested && SystemClock.UtcNow.Subtract(startTime) < maxTimeToWait)
while (!cancellationToken.IsCancellationRequested && DateTime.UtcNow.Subtract(startTime) < maxTimeToWait)
{
if (IsStartupComplete)
return Result;

if (isFirstWaiter && SystemClock.UtcNow.Subtract(lastStatus) > TimeSpan.FromSeconds(5) && _logger.IsEnabled(LogLevel.Information))
if (isFirstWaiter && DateTime.UtcNow.Subtract(lastStatus) > TimeSpan.FromSeconds(5) && _logger.IsEnabled(LogLevel.Information))
{
lastStatus = SystemClock.UtcNow;
_logger.LogInformation("Waiting for startup actions to be completed for {Duration:mm\\:ss}...", SystemClock.UtcNow.Subtract(startTime));
lastStatus = DateTime.UtcNow;
_logger.LogInformation("Waiting for startup actions to be completed for {Duration:mm\\:ss}...", DateTime.UtcNow.Subtract(startTime));
}

await Task.Delay(1000, cancellationToken).AnyContext();
}

if (isFirstWaiter && _logger.IsEnabled(LogLevel.Error))
_logger.LogError("Timed out waiting for startup actions to be completed after {Duration:mm\\:ss}", SystemClock.UtcNow.Subtract(startTime));
_logger.LogError("Timed out waiting for startup actions to be completed after {Duration:mm\\:ss}", DateTime.UtcNow.Subtract(startTime));

return new RunStartupActionsResult { Success = false, ErrorMessage = $"Timed out waiting for startup actions to be completed after {SystemClock.UtcNow.Subtract(startTime):mm\\:ss}" };
return new RunStartupActionsResult { Success = false, ErrorMessage = $"Timed out waiting for startup actions to be completed after {DateTime.UtcNow.Subtract(startTime):mm\\:ss}" };
}
}
11 changes: 0 additions & 11 deletions src/Foundatio.MetricsNET/Foundatio.MetricsNET.csproj

This file was deleted.

25 changes: 0 additions & 25 deletions src/Foundatio.MetricsNET/MetricsNETClient.cs

This file was deleted.

Loading

0 comments on commit 7c501e3

Please sign in to comment.