Incremental work on Hangfire job provider

This commit is contained in:
Sipke Schoorstra 2022-03-15 13:20:06 +01:00
parent 1c47d4b80d
commit 0a5987eba8
4 changed files with 65 additions and 9 deletions

View file

@ -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<IJob>(x => _jobRunner.RunJobAsync(x, CancellationToken.None));
_backgroundJobClient.Create(hangfireJob, new EnqueuedState(queueName ?? "default"));
return Task.CompletedTask;
}
}

View file

@ -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<IJobQueue, HangfireJobQueue>();

View file

@ -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.

View file

@ -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<IJobQueue>();
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);
}
}