Update Quartz scheduler to only schedule once

This commit is contained in:
Sipke Schoorstra 2023-05-30 21:52:31 +02:00
parent 1094b73d71
commit 71e55ea999
8 changed files with 45 additions and 2 deletions

View file

@ -168,7 +168,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "clients", "clients", "{8960
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Api.Client", "src\clients\Elsa.Api.Client\Elsa.Api.Client.csproj", "{1FCB2200-28B8-4703-8E89-73241AAED047}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.QuartzIntegration", "src\samples\aspnet\Elsa.Samples.QuartzIntegration\Elsa.Samples.QuartzIntegration.csproj", "{B0312D9E-FA30-43E9-B666-40A8782D6E1C}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.AspNet.QuartzIntegration", "src\samples\aspnet\Elsa.Samples.AspNet.QuartzIntegration\Elsa.Samples.AspNet.QuartzIntegration.csproj", "{B0312D9E-FA30-43E9-B666-40A8782D6E1C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.HangfireIntegration", "src\samples\aspnet\Elsa.Samples.HangfireIntegration\Elsa.Samples.HangfireIntegration.csproj", "{D2614FC7-102F-4F78-BB06-7C87304A10BA}"
EndProject

View file

@ -81,7 +81,10 @@ public class QuartzWorkflowScheduler : IWorkflowScheduler
{
var scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
var trigger = TriggerBuilder.Create().ForJob(RunWorkflowJob.JobKey).UsingJobData(CreateJobDataMap(request)).WithIdentity(taskName).WithCronSchedule(cronExpression).Build();
await scheduler.ScheduleJob(trigger, cancellationToken);
if(!await scheduler.CheckExists(trigger.Key, cancellationToken))
await scheduler.ScheduleJob(trigger, cancellationToken);
}
/// <inheritdoc />

View file

@ -44,6 +44,8 @@ builder.Services.AddElsa(elsa =>
// Use default authentication (JWT).
elsa.UseDefaultAuthentication();
elsa.AddWorkflowsFrom<Program>();
});
// Configure CORS to allow designer app hosted on a different origin to invoke the APIs.

View file

@ -0,0 +1,38 @@
using Elsa.Common.Contracts;
using Elsa.Scheduling.Activities;
using Elsa.Workflows.Core.Abstractions;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
using JetBrains.Annotations;
namespace Elsa.Samples.AspNet.QuartzIntegration.Workflows;
[PublicAPI]
public class HeartbeatWorkflow : WorkflowBase
{
private readonly ISystemClock _systemClock;
public HeartbeatWorkflow(ISystemClock systemClock)
{
_systemClock = systemClock;
}
protected override void Build(IWorkflowBuilder builder)
{
builder.Root = new Sequence
{
Activities =
{
new Cron
{
CronExpression = new("*/1 * * * * ?"),
CanStartWorkflow = true
},
new WriteLine
{
Text = new($"Heartbeat workflow triggered at {_systemClock.UtcNow.LocalDateTime}")
}
}
};
}
}