Merge pull request #6183 from elsa-workflows/bug/background-execution-cancels-itself

Introduce job unscheduling functionality
This commit is contained in:
raymonddenhaan 2024-12-06 09:25:45 +01:00 committed by GitHub
commit 2c70b98891
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 1 deletions

View file

@ -23,6 +23,13 @@ public interface IJobQueue
/// <returns>The ID of the job.</returns>
string Enqueue(Func<CancellationToken, Task> job);
/// <summary>
/// Dequeues a job.
/// </summary>
/// <param name="jobId">The ID of the job to dequeue.</param>
/// <returns><c>true</c> if the job was dequeued; otherwise, <c>false</c>.</returns>
bool Dequeue(string jobId);
/// <summary>
/// Cancels a job.
/// </summary>

View file

@ -40,6 +40,16 @@ public class JobQueue(IJobsChannel jobsChannel, ILogger<JobQueue> logger) : IJob
return jobItem.JobId;
}
/// <inheritdoc />
public bool Dequeue(string jobId)
{
if (!_pendingItems.TryRemove(jobId, out _))
if (!_scheduledItems.TryRemove(jobId, out _))
return false;
return true;
}
/// <inheritdoc />
public bool Cancel(string jobId)
{

View file

@ -32,6 +32,12 @@ public class HangfireBackgroundActivityScheduler(IBackgroundJobClient background
return Task.FromResult(jobId);
}
public Task UnscheduledAsync(string jobId, CancellationToken cancellationToken = default)
{
backgroundJobClient.Delete(jobId);
return Task.CompletedTask;
}
/// <inheritdoc />
public Task CancelAsync(string jobId, CancellationToken cancellationToken = default)
{

View file

@ -26,6 +26,11 @@ public interface IBackgroundActivityScheduler
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A handle representing the asynchronous invocation.</returns>
Task<string> ScheduleAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellationToken = default);
/// <summary>
/// Removes the specified job from the schedule.
/// </summary>
Task UnscheduledAsync(string jobId, CancellationToken cancellationToken = default);
/// <summary>
/// Cancels the specified job.

View file

@ -22,7 +22,7 @@ public class CancelBackgroundActivities(IBackgroundActivityScheduler backgroundA
{
var payload = removedBookmark.GetPayload<BackgroundActivityStimulus>();
if (payload.JobId != null)
await backgroundActivityScheduler.CancelAsync(payload.JobId, cancellationToken);
await backgroundActivityScheduler.UnscheduledAsync(payload.JobId, cancellationToken);
}
}
}

View file

@ -27,6 +27,12 @@ public class LocalBackgroundActivityScheduler(IJobQueue jobQueue, IServiceScopeF
return Task.FromResult(jobId);
}
public Task UnscheduledAsync(string jobId, CancellationToken cancellationToken = default)
{
jobQueue.Dequeue(jobId);
return Task.CompletedTask;
}
/// <inheritdoc />
public Task CancelAsync(string jobId, CancellationToken cancellationToken = default)
{