Job Queues

Introduction

Offloading long running jobs to the background is a great way to increase performance in your web app. Spark utilizes Coravel’s queuing to accomplish this.

For example, lets say when a user signs up to your app, you send a welcome email to them.

Instead of waiting for that email to send while the user waits for their HTTP request to finish, you could offload that email sending to a job in Spark’s Queue.

Setup

Spark registers the Queue in the Application.Startup.AppServiceRegistration.AddAppServices() method for you.

services.AddQueue();

To use the Queue, you can inject an instance of Coravel.Queueing.Interface.IQueue interface into a razor component, or another registered service.

@* blazor example *@
@page "/"
@inject IQueue _queue
// class example
private IQueue _queue;

public MyService(IQueue queue) {
    this._queue = queue;
}

Queueing Jobs

To queue a job, use the QueueInvocable method.

this._queue.QueueInvocable<ExampleJob>();

Queue With a Payload

Sometimes you will have a scenario where you want to pass paramaters to your job.

To accomplish this, you first need to add Coravel’s InvocableWithPayload<T> interface to your existing job.

public class ExampleJob : IInvocable, IInvocableWithpayload<User>
{
    public User User { get; set; }

    public ExampleJob()
    {
    }

    public Task Invoke()
    {
        // this.User is now available for you to use.
        Console.WriteLine("Do something in the background.");
        return Task.CompletedTask;
    }
}

Now you can queue the job with the QueueInvocableWithPayload method.

var user = await _db.Find(userId);
queue.QueueInvocableWithPayload<ExampleJob, User>(user);