Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cancel Invocable with payload on ConsumeQueueOnShutdown #274

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Src/Coravel/Queuing/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ public Guid QueueInvocable<T>() where T : IInvocable

public Guid QueueInvocableWithPayload<T, TParams>(TParams payload) where T : IInvocable, IInvocableWithPayload<TParams>
{
var tokenSource = new CancellationTokenSource();
var job = this.EnqueueInvocable<T>(invocable => {
IInvocableWithPayload<TParams> invocableWithParams = (IInvocableWithPayload<TParams>) invocable;
var invocableWithParams = (IInvocableWithPayload<TParams>) invocable;
invocableWithParams.Payload = payload;
if (invocableWithParams is ICancellableTask)
lengockyquang marked this conversation as resolved.
Show resolved Hide resolved
{
((ICancellableTask) invocable).Token = tokenSource.Token;
}
});
this._tokens.TryAdd(job.Guid, tokenSource);
return job.Guid;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ namespace UnitTests.Queuing
{
public class CancellableInvocableForQueueTests
{
[Fact]
public async Task CanCancelInvocableWithPayload()
{
var services = new ServiceCollection();
services.AddTransient<TestCancellableInvocable>();
services.AddTransient<TestCancellableInvocableWithPayload>();
var provider = services.BuildServiceProvider();

var queue = new Queue(provider.GetRequiredService<IServiceScopeFactory>(), new DispatcherStub());
var payload = new TestPayload()
{
Code = "test"
};
queue.QueueInvocableWithPayload<TestCancellableInvocableWithPayload, TestPayload>(payload);
lengockyquang marked this conversation as resolved.
Show resolved Hide resolved
TestCancellableInvocable.TokensCancelled = 0;
await queue.ConsumeQueueOnShutdown();
Assert.Equal(1, TestCancellableInvocableWithPayload.TokensCancelled);
}

[Fact]
public async Task CanCancelInvocable()
{
Expand Down Expand Up @@ -82,9 +101,37 @@ public Task Invoke()
{
Interlocked.Increment(ref TokensCancelled);
}
return Task.CompletedTask;
}
}

private class TestCancellableInvocableWithPayload : IInvocable, IInvocableWithPayload<TestPayload>, ICancellableTask
{
/// <summary>
/// Static fields keeps track of all cancelled tokens count.
/// </summary>
public static int TokensCancelled = 0;
public TestPayload Payload { get; set; }
public TestCancellableInvocableWithPayload() {}

public CancellationToken Token { get; set; }

public Task Invoke()
{
if(this.Token.IsCancellationRequested)
{
Interlocked.Increment(ref TokensCancelled);
}
Thread.Sleep(10000);
lengockyquang marked this conversation as resolved.
Show resolved Hide resolved

return Task.CompletedTask;
}

}

private class TestPayload
{
public string Code { get; set; }
}
}
}