Enqueuing jobs

Firing off jobs only requires a few lines of code

Please read the introduction to tasks and queues before continuing with this section.

There are two options for enqueuing a job, either immediately or in the future.

Usage

Enqueuing a job immediately or in the future are functionally exactly the same with the exception of being able to pass a period into the latter.

Enqueuing a job immediately

This job will be executed immediately (as soon as a thread is available to process it)

client.enqueue(wizard -> {
    wizard.queue("high")
          .task("sayHello");
    return wizard;
});

Enqueuing a job in the future

This job will be scheduled to be executed two minutes from now (as soon as a thread is available to process it).

client.enqueueIn(wizard -> {
    wizard.period("2m")
          .queue("high")
          .task("sayHello");
    return wizard;
});

Options

There are a few options you can provide when enqueuing a job. Let's explore them!

NameDescription

name

Provide a name for your job. You can later search for jobs with this name. It does not need to be unique.

identifier

Provide a unique identifier for your job. This is useful if you wish to link a job to an internal identifier in your system.

timeout

Provide a timeout for the job. If a job exceeds the time out it will move to the failed state and be scheduled again for execution if it has not exceeded its maximum number of retries.

maxRetries

Set the number of max retries. If not set then it will use the default max retries set in the initial configuration.

arguments

Pass arguments to your job which can then be accessed during the task execution.

A full example:

client.enqueue(wizard -> {
    wizard.queue("high")
          .task("payment")
          .name("process.payment")
          .identifier("order.123")
          .timeout("10m")
          .maxRetries(25)
          .arguments(
               argument("product", 123),
               // the last parameter indicates if the argument should be encrypted
               argument("creditCardNumber", "123-456-789", true)
          )
          .tags("orders", "payment");
    return wizard;
});

Last updated