-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor quartz job scheduler into async background service
When a system has many suspended workflows, it takes a long time for Quartz to schedule each trigger, blocking application startup. Moving this logic to a hosted service allows this process to happen in the background, not blocking startup
- Loading branch information
1 parent
d7d8c5e
commit 8e97f7a
Showing
6 changed files
with
102 additions
and
30 deletions.
There are no files selected for viewing
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
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
10 changes: 10 additions & 0 deletions
10
src/core/Elsa.Core/HostedServices/IScopedBackgroundService.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,10 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Elsa.HostedServices | ||
{ | ||
public interface IScopedBackgroundService | ||
{ | ||
Task ExecuteAsync(CancellationToken cancellationToken); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/core/Elsa.Core/HostedServices/ScopedBackgroundService.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,24 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Elsa.HostedServices | ||
{ | ||
/// <summary> | ||
/// Executed the specified worker within a scoped-lifetime scope. | ||
/// </summary> | ||
public class ScopedBackgroundService<TWorker> : BackgroundService where TWorker:IScopedBackgroundService | ||
{ | ||
private readonly IServiceScopeFactory _scopeFactory; | ||
|
||
public ScopedBackgroundService(IServiceScopeFactory scopeFactory) => _scopeFactory = scopeFactory; | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
using var scope = _scopeFactory.CreateScope(); | ||
var worker = (IScopedBackgroundService)ActivatorUtilities.GetServiceOrCreateInstance<TWorker>(scope.ServiceProvider); | ||
await worker.ExecuteAsync(stoppingToken); | ||
} | ||
} | ||
} |