Implement Hangfire proxy job
This commit is contained in:
parent
aa40a7e966
commit
4b9907b049
|
|
@ -29,17 +29,27 @@ services:
|
|||
- "8600:8600/udp"
|
||||
command: "agent -server -bootstrap-expect 3 -ui -client 0.0.0.0"
|
||||
|
||||
sqlserver:
|
||||
image: "mcr.microsoft.com/mssql/server:2019-latest"
|
||||
ports:
|
||||
- "3433:1433"
|
||||
environment:
|
||||
SA_PASSWORD: "Elsa2022!"
|
||||
ACCEPT_EULA: "Y"
|
||||
volumes:
|
||||
- c:\data\docker\sqlserver2019:/var/opt/mssql/data
|
||||
|
||||
mongodb:
|
||||
image: mongo:latest
|
||||
ports:
|
||||
- 127.0.0.1:27017:27017
|
||||
- "127.0.0.1:27017:27017"
|
||||
volumes:
|
||||
- mongodb_data:/data/db
|
||||
|
||||
redis:
|
||||
image: redis:latest
|
||||
ports:
|
||||
- 127.0.0.1:6379:6379
|
||||
- "127.0.0.1:6379:6379"
|
||||
|
||||
networks:
|
||||
consul:
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Elsa.Modules.Hangfire.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
}
|
||||
21
src/modules/Elsa.Modules.Hangfire/Jobs/RunElsaJob.cs
Normal file
21
src/modules/Elsa.Modules.Hangfire/Jobs/RunElsaJob.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Elsa.Jobs.Contracts;
|
||||
|
||||
namespace Elsa.Modules.Hangfire.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// A generic Hangfire job that executes the specified Elsa job. Basically a proxy.
|
||||
/// </summary>
|
||||
public class RunElsaJob
|
||||
{
|
||||
private readonly IJobRunner _jobRunner;
|
||||
|
||||
public RunElsaJob(IJobRunner jobRunner)
|
||||
{
|
||||
_jobRunner = jobRunner;
|
||||
}
|
||||
|
||||
public async Task RunAsync(IJob job, CancellationToken cancellationToken)
|
||||
{
|
||||
await _jobRunner.RunJobAsync(job, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using Elsa.Jobs.Contracts;
|
||||
using Elsa.Modules.Hangfire.Jobs;
|
||||
using Hangfire;
|
||||
using Hangfire.States;
|
||||
using HangfireJob = Hangfire.Common.Job;
|
||||
|
|
@ -8,17 +9,15 @@ namespace Elsa.Modules.Hangfire.Services;
|
|||
public class HangfireJobQueue : IJobQueue
|
||||
{
|
||||
private readonly IBackgroundJobClient _backgroundJobClient;
|
||||
private readonly IJobRunner _jobRunner;
|
||||
|
||||
public HangfireJobQueue(IBackgroundJobClient backgroundJobClient, IJobRunner jobRunner)
|
||||
{
|
||||
_backgroundJobClient = backgroundJobClient;
|
||||
_jobRunner = jobRunner;
|
||||
}
|
||||
|
||||
public Task SubmitJobAsync(IJob job, string? queueName = default, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hangfireJob = HangfireJob.FromExpression<IJob>(x => _jobRunner.RunJobAsync(x, CancellationToken.None));
|
||||
var hangfireJob = HangfireJob.FromExpression<RunElsaJob>(x => x.RunAsync(job, CancellationToken.None));
|
||||
_backgroundJobClient.Create(hangfireJob, new EnqueuedState(queueName ?? "default"));
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using Elsa.Jobs.Contracts;
|
|||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Elsa.Modules.Hangfire.Services;
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ public class HangfireJobQueueProvider : IJobQueueProvider
|
|||
public bool UseSqlServerStorage { get; set; }
|
||||
public string? SqlServerConnectionString { get; set; }
|
||||
public SqlServerStorageOptions? SqlServerStorageOptions { get; set; }
|
||||
public Action<BackgroundJobServerOptions>? ConfigureBackgroundServerOptions { get; set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
|
@ -31,6 +33,7 @@ public class HangfireJobQueueProvider : IJobQueueProvider
|
|||
services.AddHangfire(configuration =>
|
||||
{
|
||||
configuration.UseSimpleAssemblyNameTypeSerializer();
|
||||
configuration.UseRecommendedSerializerSettings(json => json.TypeNameHandling = TypeNameHandling.Objects);
|
||||
|
||||
if (UseSqlServerStorage)
|
||||
{
|
||||
|
|
@ -38,6 +41,14 @@ public class HangfireJobQueueProvider : IJobQueueProvider
|
|||
configuration.UseSqlServerStorage(SqlServerConnectionString, storageOptions);
|
||||
}
|
||||
});
|
||||
|
||||
if (UseSqlServerStorage)
|
||||
services.AddHangfireServer((_, options) => ConfigureBackgroundServerOptions?.Invoke(options), new SqlServerStorage(SqlServerConnectionString));
|
||||
else
|
||||
services.AddHangfireServer(options =>
|
||||
{
|
||||
ConfigureBackgroundServerOptions?.Invoke(options);
|
||||
});
|
||||
}
|
||||
|
||||
services.AddSingleton<IJobQueue, HangfireJobQueue>();
|
||||
|
|
|
|||
|
|
@ -37,6 +37,9 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
var services = builder.Services;
|
||||
var configuration = builder.Configuration;
|
||||
|
||||
// Run the SqlServer container from docker-compose.yml to start a SQL Server container.
|
||||
var sqlServerConnectionString = configuration.GetConnectionString("SqlServer");
|
||||
|
||||
// Add services.
|
||||
services
|
||||
.AddElsa()
|
||||
|
|
@ -45,7 +48,7 @@ services
|
|||
.AddProtoActorWorkflowHost()
|
||||
.IndexWorkflowTriggers()
|
||||
.AddElsaManagement()
|
||||
.AddJobServices(new QuartzJobSchedulerProvider(), new HangfireJobQueueProvider())
|
||||
.AddJobServices(new QuartzJobSchedulerProvider(), new HangfireJobQueueProvider(sqlServerConnectionString))
|
||||
.AddHttpActivityServices()
|
||||
.AddAzureServiceBusServices(options => configuration.GetSection("AzureServiceBus").Bind(options))
|
||||
.ConfigureWorkflowRuntime(options =>
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"AzureServiceBus": ""
|
||||
"AzureServiceBus": "",
|
||||
"SqlServer": "Server=localhost,3433;Database=Elsa3;User ID=sa;Password=Elsa2022!;MultipleActiveResultSets=True;Max Pool Size=500;Connection Timeout=3600;TrustServerCertificate=True"
|
||||
},
|
||||
"AzureServiceBus": {
|
||||
"ConnectionStringOrName": "AzureServiceBus",
|
||||
|
|
|
|||
Loading…
Reference in a new issue