From 0a5987eba8a99d767f3e7c10fd73ad40bf2eb72f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 15 Mar 2022 13:20:06 +0100 Subject: [PATCH] Incremental work on Hangfire job provider --- .../Services/HangfireJobQueue.cs | 19 +++++++++- .../Services/HangfireJobQueueProvider.cs | 16 +++++--- .../aspnet/Elsa.Samples.Web1/Program.cs | 2 +- .../Workflows/SubmitJobWorkflow.cs | 37 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/samples/aspnet/Elsa.Samples.Web1/Workflows/SubmitJobWorkflow.cs diff --git a/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueue.cs b/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueue.cs index 97114165a..ec13bc599 100644 --- a/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueue.cs +++ b/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueue.cs @@ -1,11 +1,26 @@ using Elsa.Jobs.Contracts; +using Hangfire; +using Hangfire.States; +using HangfireJob = Hangfire.Common.Job; -namespace Elsa.Modules.Hangfire; +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) { - throw new NotImplementedException(); + var hangfireJob = HangfireJob.FromExpression(x => _jobRunner.RunJobAsync(x, CancellationToken.None)); + _backgroundJobClient.Create(hangfireJob, new EnqueuedState(queueName ?? "default")); + + return Task.CompletedTask; } } \ No newline at end of file diff --git a/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueueProvider.cs b/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueueProvider.cs index 63318d677..43f6dec40 100644 --- a/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueueProvider.cs +++ b/src/modules/Elsa.Modules.Hangfire/Services/HangfireJobQueueProvider.cs @@ -28,12 +28,16 @@ public class HangfireJobQueueProvider : IJobQueueProvider { if (RegisterHangfire) { - var storageOptions = SqlServerStorageOptions ?? new SqlServerStorageOptions(); - - services - .AddHangfire(configuration => configuration - .UseSimpleAssemblyNameTypeSerializer() - .UseSqlServerStorage(SqlServerConnectionString, storageOptions)); + services.AddHangfire(configuration => + { + configuration.UseSimpleAssemblyNameTypeSerializer(); + + if (UseSqlServerStorage) + { + var storageOptions = SqlServerStorageOptions ?? new SqlServerStorageOptions(); + configuration.UseSqlServerStorage(SqlServerConnectionString, storageOptions); + } + }); } services.AddSingleton(); diff --git a/src/samples/aspnet/Elsa.Samples.Web1/Program.cs b/src/samples/aspnet/Elsa.Samples.Web1/Program.cs index 26e62fa73..1485a2285 100644 --- a/src/samples/aspnet/Elsa.Samples.Web1/Program.cs +++ b/src/samples/aspnet/Elsa.Samples.Web1/Program.cs @@ -47,7 +47,6 @@ services .AddElsaManagement() .AddJobServices(new QuartzJobSchedulerProvider(), new HangfireJobQueueProvider()) .AddHttpActivityServices() - .AddJobServices() .AddAzureServiceBusServices(options => configuration.GetSection("AzureServiceBus").Bind(options)) .ConfigureWorkflowRuntime(options => { @@ -61,6 +60,7 @@ services options.Workflows.Add(nameof(ReceiveMessageWorkflow), new ReceiveMessageWorkflow()); options.Workflows.Add(nameof(RunJavaScriptWorkflow), new RunJavaScriptWorkflow()); options.Workflows.Add(nameof(WorkflowContextsWorkflow), new WorkflowContextsWorkflow()); + options.Workflows.Add(nameof(SubmitJobWorkflow), new SubmitJobWorkflow()); }); // Testing only: allow client app to connect from anywhere. diff --git a/src/samples/aspnet/Elsa.Samples.Web1/Workflows/SubmitJobWorkflow.cs b/src/samples/aspnet/Elsa.Samples.Web1/Workflows/SubmitJobWorkflow.cs new file mode 100644 index 000000000..dba2896ea --- /dev/null +++ b/src/samples/aspnet/Elsa.Samples.Web1/Workflows/SubmitJobWorkflow.cs @@ -0,0 +1,37 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Elsa.Activities.Primitives; +using Elsa.Contracts; +using Elsa.Jobs.Contracts; +using Elsa.Jobs.Models; +using Elsa.Runtime.Contracts; + +namespace Elsa.Samples.Web1.Workflows; + +public class SubmitJobWorkflow : IWorkflow +{ + public void Build(IWorkflowDefinitionBuilder workflow) + { + workflow.WithRoot(new Inline(async context => + { + var jobQueue = context.GetRequiredService(); + var job = new ReadTheInternetJob(); + await jobQueue.SubmitJobAsync(job, cancellationToken: context.CancellationToken); + })); + } +} + +public class ReadTheInternetJob : Job +{ + protected override async ValueTask ExecuteAsync(JobExecutionContext context) + { + var httpClient = new HttpClient + { + BaseAddress = new Uri("https://www.google.com") + }; + + var response = await httpClient.GetStringAsync("/", context.Cancellation); + Console.WriteLine(response); + } +} \ No newline at end of file