-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Angeling3/hangfire-jobs
Add hangfire implementations to the job scheduler and enqueuer
- Loading branch information
Showing
10 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/ContractImplementations.Hangfire/ContractImplementations.Hangfire.csproj
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="../MSBuild/Base.props"/> | ||
<Import Project="../MSBuild/Packable.props"/> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Bootstrapping\Bootstrapping.csproj" /> | ||
<ProjectReference Include="..\Foundation\Foundation.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Hangfire.Core" Version="1.8.*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
38 changes: 38 additions & 0 deletions
38
src/ContractImplementations.Hangfire/Jobs/HangfireJobEnqueuer.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,38 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Hangfire; | ||
using IOKode.OpinionatedFramework.Jobs; | ||
|
||
namespace IOKode.OpinionatedFramework.ContractImplementations.Hangfire.Jobs; | ||
|
||
public class HangfireJobEnqueuer : IJobEnqueuer | ||
{ | ||
public Task EnqueueAsync(Queue queue, IJob job, CancellationToken cancellationToken) | ||
{ | ||
BackgroundJob.Enqueue(queue.Name, () => InvokeJob(job)); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task EnqueueWithDelayAsync(Queue queue, IJob job, TimeSpan delay, CancellationToken cancellationToken) | ||
{ | ||
BackgroundJob.Schedule(queue.Name, () => InvokeJob(job), delay); | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// This method is not intended to be called directly in application code. | ||
/// Exists to allow Hangfire to serialize and deserialize the job object | ||
/// that it will receive as an argument. This ensures that the job | ||
/// can be processed correctly during execution. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method must be public because it is used by Hangfire during the deserialization | ||
/// and execution of enqueued tasks. Hangfire requires that methods to be invoked | ||
/// are publicly accessible to resolve them when deserializing the previously generated expression. | ||
/// </remarks> | ||
public async Task InvokeJob(IJob job) | ||
{ | ||
await job.InvokeAsync(default); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/ContractImplementations.Hangfire/Jobs/HangfireJobScheduler.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,51 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Cronos; | ||
using Hangfire; | ||
using IOKode.OpinionatedFramework.Ensuring; | ||
using IOKode.OpinionatedFramework.Jobs; | ||
|
||
namespace IOKode.OpinionatedFramework.ContractImplementations.Hangfire.Jobs; | ||
|
||
public class HangfireJobScheduler : IJobScheduler | ||
{ | ||
public Task<ScheduledJob> ScheduleAsync(IJob job, CronExpression interval, CancellationToken cancellationToken) | ||
{ | ||
var scheduledJob = new HangfireMutableScheduledJob(interval, job); | ||
RecurringJob.AddOrUpdate(scheduledJob.Identifier.ToString(), () => InvokeAsync(job), interval.ToString); | ||
return Task.FromResult<ScheduledJob>(scheduledJob); | ||
} | ||
|
||
public Task RescheduleAsync(ScheduledJob scheduledJob, CronExpression interval, CancellationToken cancellationToken) | ||
{ | ||
Ensure.Type.IsAssignableTo(scheduledJob.GetType(), typeof(MutableScheduledJob)) | ||
.ElseThrowsIllegalArgument($"Type must be assignable to {nameof(MutableScheduledJob)} type.", nameof(scheduledJob)); | ||
|
||
RecurringJob.AddOrUpdate(scheduledJob.Identifier.ToString(), () => InvokeAsync(scheduledJob.Job), interval.ToString); | ||
((MutableScheduledJob) scheduledJob).ChangeInterval(interval); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task UnscheduleAsync(ScheduledJob scheduledJob, CancellationToken cancellationToken) | ||
{ | ||
RecurringJob.RemoveIfExists(scheduledJob.Identifier.ToString()); | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// This method is not intended to be called directly in application code. | ||
/// Exists to allow Hangfire to serialize and deserialize the job object | ||
/// that it will receive as an argument. This ensures that the job | ||
/// can be processed correctly during execution. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method must be public because it is used by Hangfire during the deserialization | ||
/// and execution of scheduled tasks. Hangfire requires that methods to be invoked | ||
/// are publicly accessible to resolve them when deserializing the previously generated expression. | ||
/// </remarks> | ||
public async Task InvokeAsync(IJob job) | ||
{ | ||
await job.InvokeAsync(default); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/ContractImplementations.Hangfire/Jobs/HangfireMutableScheduledJob.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,16 @@ | ||
using System; | ||
using Cronos; | ||
using IOKode.OpinionatedFramework.Jobs; | ||
|
||
namespace IOKode.OpinionatedFramework.ContractImplementations.Hangfire.Jobs; | ||
|
||
public class HangfireMutableScheduledJob : MutableScheduledJob | ||
{ | ||
public HangfireMutableScheduledJob(CronExpression interval, IJob job) : base(interval, job) | ||
{ | ||
} | ||
|
||
public HangfireMutableScheduledJob(CronExpression interval, IJob job, Guid id) : base(interval, job, id) | ||
{ | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/ContractImplementations.Hangfire/Jobs/ServiceExtensions.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,18 @@ | ||
using System; | ||
using Hangfire; | ||
using IOKode.OpinionatedFramework.Bootstrapping; | ||
using IOKode.OpinionatedFramework.Jobs; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace IOKode.OpinionatedFramework.ContractImplementations.Hangfire.Jobs; | ||
|
||
public static class ServiceExtensions | ||
{ | ||
public static void AddHangfireJobsImplementations(this IOpinionatedServiceCollection services, Action<IGlobalConfiguration> configuration) | ||
{ | ||
services.AddSingleton<IJobEnqueuer, HangfireJobEnqueuer>(); | ||
services.AddSingleton<IJobScheduler, HangfireJobScheduler>(); | ||
|
||
configuration.Invoke(GlobalConfiguration.Configuration); | ||
} | ||
} |
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
Oops, something went wrong.