Skip to content

Commit

Permalink
Feature/styling and formatting (#130)
Browse files Browse the repository at this point in the history
* Update CONTRIBUTING.md

* Update dependabot.yml

* Update FUNDING.yml

* nuget updates

* move test nugets into test csprojs

* target frameworks netstandard2.0 and net8.0

* test naming updates

* Update Pilgaard.ScheduledJobs.Tests.csproj

* more cleanup, more nuget updates
  • Loading branch information
NielsPilgaard authored Jun 9, 2024
1 parent f042590 commit ea7a177
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 289 deletions.
1 change: 0 additions & 1 deletion src/Pilgaard.BackgroundJobs/BackgroundJobScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public async IAsyncEnumerable<BackgroundJobRegistration> GetBackgroundJobsAsync(
}
}


public IEnumerable<BackgroundJobRegistration> GetRecurringJobs() => _options.Value.Registrations.Where(registration => registration.IsRecurringJob);

/// <summary>
Expand Down
369 changes: 184 additions & 185 deletions src/Pilgaard.BackgroundJobs/BackgroundJobService.cs

Large diffs are not rendered by default.

35 changes: 17 additions & 18 deletions src/Pilgaard.BackgroundJobs/IBackgroundJobScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
namespace Pilgaard.BackgroundJobs;


/// <summary>
/// IBackgroundJobScheduler is responsible for scheduling background jobs.
/// </summary>
internal interface IBackgroundJobScheduler
{
/// <summary>
/// Asynchronously retrieves an ordered enumerable of background job registrations.
/// <para>
/// Jobs that implement <see cref="IRecurringJob"/> are not retrieved, they are scheduled during startup.
/// </para>
/// <para>
/// Each background job registration is returned when it should be run.
/// </para>
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used for cancelling the enumeration.</param>
/// <returns>An asynchronous enumerable of background job registrations.</returns>
IAsyncEnumerable<BackgroundJobRegistration> GetBackgroundJobsAsync(CancellationToken cancellationToken);
/// <summary>
/// Asynchronously retrieves an ordered enumerable of background job registrations.
/// <para>
/// Jobs that implement <see cref="IRecurringJob"/> are not retrieved, they are scheduled during startup.
/// </para>
/// <para>
/// Each background job registration is returned when it should be run.
/// </para>
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used for cancelling the enumeration.</param>
/// <returns>An asynchronous enumerable of background job registrations.</returns>
IAsyncEnumerable<BackgroundJobRegistration> GetBackgroundJobsAsync(CancellationToken cancellationToken);

/// <summary>
/// Retrieves all <see cref="BackgroundJobRegistration"/>s where <see cref="BackgroundJobRegistration.IsRecurringJob"/> is <c>true</c>
/// </summary>
/// <returns>An enumerable of background job registrations where <see cref="BackgroundJobRegistration.IsRecurringJob"/> is <c>true</c></returns>
IEnumerable<BackgroundJobRegistration> GetRecurringJobs();
/// <summary>
/// Retrieves all <see cref="BackgroundJobRegistration"/>s where <see cref="BackgroundJobRegistration.IsRecurringJob"/> is <c>true</c>
/// </summary>
/// <returns>An enumerable of background job registrations where <see cref="BackgroundJobRegistration.IsRecurringJob"/> is <c>true</c></returns>
IEnumerable<BackgroundJobRegistration> GetRecurringJobs();
}
62 changes: 31 additions & 31 deletions src/Pilgaard.BackgroundJobs/Jobs/BackgroundJobExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ namespace Pilgaard.BackgroundJobs;

public static class BackgroundJobExtensions
{
/// <summary>
/// Get the next occurrence of the background job.
/// </summary>
/// <param name="backgroundJob">The background job to get the next occurrence of.</param>
/// <returns><see cref="DateTime"/> of the next occurrence, or <c>null</c> if none.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static DateTime? GetNextOccurrence(this IBackgroundJob backgroundJob) =>
backgroundJob switch
{
ICronJob cronJob => cronJob.GetNextOccurrence(),
IRecurringJob recurringJob => recurringJob.GetNextOccurrence(),
IOneTimeJob oneTimeJob => oneTimeJob.GetNextOccurrence(),
_ => throw new ArgumentOutOfRangeException(nameof(IBackgroundJob),
$"Background job must implement either {nameof(ICronJob)}, {nameof(IRecurringJob)} or {nameof(IOneTimeJob)}")
};
/// <summary>
/// Get the next occurrence of the background job.
/// </summary>
/// <param name="backgroundJob">The background job to get the next occurrence of.</param>
/// <returns><see cref="DateTime"/> of the next occurrence, or <c>null</c> if none.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static DateTime? GetNextOccurrence(this IBackgroundJob backgroundJob) =>
backgroundJob switch
{
ICronJob cronJob => cronJob.GetNextOccurrence(),
IRecurringJob recurringJob => recurringJob.GetNextOccurrence(),
IOneTimeJob oneTimeJob => oneTimeJob.GetNextOccurrence(),
_ => throw new ArgumentOutOfRangeException(nameof(backgroundJob),
$"Background job must implement either {nameof(ICronJob)}, {nameof(IRecurringJob)} or {nameof(IOneTimeJob)}")
};

/// <summary>
/// Get all occurrences of the background job up to a certain date.
/// </summary>
/// <param name="backgroundJob">The background job to get the occurrences of.</param>
/// <param name="toUtc">The date up to which to get occurrences.</param>
/// <returns><see cref="IEnumerable{T}"/> of all occurrences.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static IEnumerable<DateTime> GetOccurrences(this IBackgroundJob backgroundJob, DateTime toUtc) =>
backgroundJob switch
{
ICronJob cronJob => cronJob.GetOccurrences(toUtc),
IRecurringJob recurringJob => recurringJob.GetOccurrences(toUtc),
IOneTimeJob oneTimeJob => oneTimeJob.GetOccurrences(toUtc),
_ => throw new ArgumentOutOfRangeException(nameof(IBackgroundJob),
$"Background job must implement either {nameof(ICronJob)}, {nameof(IRecurringJob)} or {nameof(IOneTimeJob)}")
};
/// <summary>
/// Get all occurrences of the background job up to a certain date.
/// </summary>
/// <param name="backgroundJob">The background job to get the occurrences of.</param>
/// <param name="toUtc">The date up to which to get occurrences.</param>
/// <returns><see cref="IEnumerable{T}"/> of all occurrences.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static IEnumerable<DateTime> GetOccurrences(this IBackgroundJob backgroundJob, DateTime toUtc) =>
backgroundJob switch
{
ICronJob cronJob => cronJob.GetOccurrences(toUtc),
IRecurringJob recurringJob => recurringJob.GetOccurrences(toUtc),
IOneTimeJob oneTimeJob => oneTimeJob.GetOccurrences(toUtc),
_ => throw new ArgumentOutOfRangeException(nameof(backgroundJob),
$"Background job must implement either {nameof(ICronJob)}, {nameof(IRecurringJob)} or {nameof(IOneTimeJob)}")
};
}
4 changes: 2 additions & 2 deletions src/Pilgaard.BackgroundJobs/Pilgaard.BackgroundJobs.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetStandardVersion);$(NetVersion)</TargetFrameworks>
Expand All @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Cronos" Version="0.8.4" />
</ItemGroup>

<ItemGroup Label="Net Nugets" Condition="'$(TargetFramework)' == '$(NetVersion)'">
Expand Down
4 changes: 2 additions & 2 deletions src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetStandardVersion);$(NetVersion)</TargetFrameworks>
Expand All @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Cronos" Version="0.8.4" />
</ItemGroup>

</Project>
42 changes: 21 additions & 21 deletions src/ValueStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
// Copied from: https://github.com/dotnet/extensions/blob/master/src/Shared/src/ValueStopwatch/ValueStopwatch.cs
internal readonly struct ValueStopwatch
{
private static readonly double _timestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
private static readonly double _timestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;

private readonly long _startTimestamp;
private readonly long _startTimestamp;

public bool IsActive => _startTimestamp != 0;
public bool IsActive => _startTimestamp != 0;

private ValueStopwatch(long startTimestamp)
{
_startTimestamp = startTimestamp;
}
private ValueStopwatch(long startTimestamp)
{
_startTimestamp = startTimestamp;
}

public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp());
public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp());

public TimeSpan GetElapsedTime()
{
// Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0.
// So it being 0 is a clear indication of default(ValueStopwatch)
if (!IsActive)
{
throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
}
public TimeSpan GetElapsedTime()
{
// Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0.
// So it being 0 is a clear indication of default(ValueStopwatch)
if (!IsActive)
{
throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
}

long end = Stopwatch.GetTimestamp();
long timestampDelta = end - _startTimestamp;
long ticks = (long)(_timestampToTicks * timestampDelta);
return new TimeSpan(ticks);
}
var end = Stopwatch.GetTimestamp();
var timestampDelta = end - _startTimestamp;
var ticks = (long)(_timestampToTicks * timestampDelta);
return new TimeSpan(ticks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.26.0">
<PackageReference Include="FluentAssertions.Analyzers" Version="0.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
19 changes: 11 additions & 8 deletions tests/Pilgaard.CronJobs.Tests/Pilgaard.CronJobs.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetVersion)</TargetFramework>
Expand All @@ -12,20 +12,23 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.26.0">
<PackageReference Include="FluentAssertions.Analyzers" Version="0.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetVersion)</TargetFramework>
Expand All @@ -12,20 +12,23 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.26.0">
<PackageReference Include="FluentAssertions.Analyzers" Version="0.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.26.0">
<PackageReference Include="FluentAssertions.Analyzers" Version="0.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

</Project>

0 comments on commit ea7a177

Please sign in to comment.