-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor job blocking functionality to use BlockRepository strategy
The commit introduces a new `IBlockRepository` interface to abstract job blocking operations. Depending on the environment, the system now uses either `PersistentBlockRepository` or `MemoryBlockRepository` for these operations. This refactoring aims to improve performance for non-clustered schedulers.
- Loading branch information
1 parent
ad4b8b2
commit 22153be
Showing
10 changed files
with
186 additions
and
77 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
34 changes: 34 additions & 0 deletions
34
Quartz.Impl.RavenJobStore/ConcreteStrategies/MemoryBlockRepository.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,34 @@ | ||
using System.Collections.Concurrent; | ||
using Domla.Quartz.Raven.Strategies; | ||
using Raven.Client.Documents.Session; | ||
|
||
namespace Domla.Quartz.Raven.ConcreteStrategies; | ||
|
||
internal class MemoryBlockRepository : IBlockRepository | ||
{ | ||
private ConcurrentDictionary<string, byte> BlockedJobs { get; } = new(); | ||
|
||
public Task BlockJobAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) | ||
{ | ||
BlockedJobs.TryAdd(jobId, 0); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task ReleaseJobAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) | ||
{ | ||
BlockedJobs.TryRemove(jobId, out _); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<bool> IsJobBlockedAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) => | ||
Task.FromResult(BlockedJobs.ContainsKey(jobId)); | ||
|
||
public Task<IReadOnlyList<string>> GetBlockedJobsAsync(IAsyncDocumentSession session, CancellationToken token) => | ||
Task.FromResult<IReadOnlyList<string>>(BlockedJobs.Keys.ToList()); | ||
|
||
public Task ReleaseAllJobsAsync(IAsyncDocumentSession session, CancellationToken token) | ||
{ | ||
BlockedJobs.Clear(); | ||
return Task.CompletedTask; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
Quartz.Impl.RavenJobStore/ConcreteStrategies/PersistentBlockRepository.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,49 @@ | ||
using Domla.Quartz.Raven.Entities; | ||
using Domla.Quartz.Raven.Indexes; | ||
using Domla.Quartz.Raven.Strategies; | ||
using Raven.Client.Documents; | ||
using Raven.Client.Documents.Session; | ||
|
||
namespace Domla.Quartz.Raven.ConcreteStrategies; | ||
|
||
internal class PersistentBlockRepository : IBlockRepository | ||
{ | ||
private string InstanceName { get; } | ||
|
||
public PersistentBlockRepository(string instanceName) | ||
{ | ||
InstanceName = instanceName; | ||
} | ||
|
||
public Task BlockJobAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) => | ||
session.StoreAsync(new BlockedJob(InstanceName, jobId), token); | ||
|
||
public Task ReleaseJobAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) | ||
{ | ||
session.Delete(BlockedJob.GetId(InstanceName, jobId)); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<bool> IsJobBlockedAsync(IAsyncDocumentSession session, string jobId, CancellationToken token) => | ||
session.Advanced.ExistsAsync(BlockedJob.GetId(InstanceName, jobId), token); | ||
|
||
public async Task<IReadOnlyList<string>> GetBlockedJobsAsync( | ||
IAsyncDocumentSession session, | ||
CancellationToken token) => | ||
await ( | ||
from blocked in session.Query<BlockedJob>(nameof(BlockedJobIndex)) | ||
where blocked.Scheduler == InstanceName | ||
select blocked.JobId | ||
).ToListAsync(token).ConfigureAwait(false); | ||
|
||
public async Task ReleaseAllJobsAsync(IAsyncDocumentSession session, CancellationToken token) | ||
{ | ||
var ids = await ( | ||
from blocked in session.Query<BlockedJob>(nameof(BlockedJobIndex)) | ||
where blocked.Scheduler == InstanceName | ||
select blocked.Id | ||
).ToListAsync(token).ConfigureAwait(false); | ||
|
||
ids.ForEach(session.Delete); | ||
} | ||
} |
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,16 @@ | ||
using Domla.Quartz.Raven.Entities; | ||
using Raven.Client.Documents.Indexes; | ||
|
||
namespace Domla.Quartz.Raven.Indexes; | ||
|
||
internal class BlockedJobIndex : AbstractIndexCreationTask<BlockedJob> | ||
{ | ||
internal BlockedJobIndex() | ||
{ | ||
Map = blockedJobs => from blockedJob in blockedJobs | ||
select new | ||
{ | ||
blockedJob.Scheduler | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.