Coravel gives you a zero-configuration queue that runs in-memory.

This is useful to offload long-winded tasks to the background instead of making your users wait for their HTTP request to finish.


In your Startup file, in the ConfigureServices():


That's it!



Inject an instance of the Coravel.Queuing.Interfaces.IQueue interface into your controller, etc.

IQueue _queue;

public HomeController(IQueue queue) {
    this._queue = queue;

Queuing Invocables

To queue an invocable, use QueueInvocable:



Queuing invocables is the recommended way to use Coravel's queuing.

To learn about creating and using invocables see here.

Queue An Invocable With A Payload

Many times you want to queue a background job and also supply a payload/parameters.

For example, you might have an invocable SendWelcomeUserEmailInvocable. However, you need to supply a specific user's information so that the correct user will receive the email!

First, add the IInvocableWithPayload<T> interface to your existing invocable:

                                                         // This one 👇
public class SendWelcomeUserEmailInvocable : IInvocable, IInvocableWithPayload<UserModel>
  // This is the implementation of the interface 👇
  public UserModel Payload { get; set; }

  /* Constructor, etc. */

  public async Task Invoke()
    // `this.Payload` will be available to use now.

To queue this invocable, use the QueueInvocableWithPayload method:

var userModel = await _userService.Get(userId);
queue.QueueInvocableWithPayload<SendWelcomeUserEmailInvocable, UserModel>(userModel);

Now your job will be queued to execute in the background with the specific data it needs to run!

Queuing An Async Task

Use the QueueAsyncTask to queue up an async task:

 this._queue.QueueAsyncTask(async() => {
    await Task.Delay(1000);
    Console.WriteLine("This was queued!");

Queuing A Synchronous Task

You use the QueueTask() method to add a task to the queue.

public IActionResult QueueTask() {
    this._queue.QueueTask(() => Console.WriteLine("This was queued!"));
    return Ok();

Queuing An Event Broadcast

Event broadcasting is great - but what if your event listeners are doing some heavy / long-winded tasks? You don't want that to happen on the same thread as your HTTP request!

By using QueueBroadcast, you can queue an event to be broadcasted in the background.

this._queue.QueueBroadcast(new OrderCreated(orderId)); 


Global Error Handling

In the Configure method of your Startup file:

var provider = app.ApplicationServices;
    .OnError(e =>
        //... handle the error

Adjusting The Consummation Delay

Normally, the queue will consume all of it's queued tasks every 30 seconds. You can adjust this delay in the appsettings.json file.

"Coravel": {
  "Queue": {
    "ConsummationDelay": 1

Logging Executed Task Progress

Coravel uses the ILogger .NET Core interface to allow logging task progress:

var provider = app.ApplicationServices;

Queuing Cancellable Invocables

Sometimes you have a long-running invocable that needs the ability to be cancelled (manually by you or by the system when the application is being shutdown).

By using QueueCancellableInvocable you can build invocables that will have this ability!

First, add the Coravel.Queuing.Interfaces.ICancellableTask to your invocable and implement the property:

CancellationToken Token { get; set; }

Now, in your Invoke method, you will have access to check if the token has been cancelled.

For example:

  await ProcessNextRecord();


Coravel will automatically dispose all TokenCancellationSource objects associated with invocables that have been consumed by the queue.

Also, when the application is being shutdown, Coravel will also cancel all tokens to make sure your invocables can stop their work and allow a graceful shutdown.

If you are manually cancelling/handling tokens then you'll have to code around the potential for your tokens to have been cancelled and even disposed.

On App Closing

When your app is stopped, Coravel will immediately begin consuming any remaining tasks and wait until they are completed.

This will keep your app running in the background - as long as the parent process is not killed.