Adding jobs

The most basic operation in BullMQ is adding a job. A job is simply a data structure that has a name, some custom data and options. Jobs are added posting a JSON object to the /queues/:queue-name endpoint. The endpoint accepts an array of jobs, if sending a standalone job do not forget to wrap it in an array.

Posting jobs is an atomic operation, either all jobs or none will be added to the queue, so if the call fails for any reason, you can be certain that no jobs were added at all.

The JSON object must follow this interface:

interface Job {
  name: string;
  data: any;
  opts?: JobOpts;
}

Lets add the simplest possible job:

curl --location 'https://myproxy.dev/queues/my-queue/jobs' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer my-secret-token' \
--data '[{
	"name": "paint-red",
        "data": {},
        }]'

Note how we wrap the job in an array, as we are only sending one job in this example.

The call should succeed and return the complete Job as a Json object with all the defaults as it is stored in BullMQ:

[
  {
    "name": "paint-red",
    "data": {},
    "opts": { "attempts": 0, "delay": 0 },
    "id": "116",
    "progress": 0,
    "returnvalue": null,
    "stacktrace": null,
    "attemptsStarted": 0,
    "attemptsMade": 0,
    "delay": 0,
    "timestamp": 1708597320043,
    "queueQualifiedName": "bull:my-test-queue"
  }
]

There are obviously many more advanced jobs and features which we will cover in the next pages.

Last updated