Merge pull request #6183 from elsa-workflows/bug/background-execution-cancels-itself
Introduce job unscheduling functionality
This commit is contained in:
commit
2c70b98891
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue