Adding workers

Workers are added posting a json object to the /workers endpoint. An endpoint is specified as an url, which method (http verb) to use on the call to said url, which optional headers to send, as well as an optional timeout, which if reached will consider the call a failure.

interface WorkerMetadata {
  queue: string;
  endpoint: {
    url: string;
    method: "post" | "get" | "put" | "delete" | "patch";
    headers?: Record<string, string>;
    timeout?: number; // max allowed duration in milliseconds
  }
  // .. more options
}

The most basic object we can send in order to start a worker could for example be the following:

curl --location 'http://localhost:8080/workers' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer 1234' \
--data '{
    "queue": "my-test-queue",
    "endpoint": {
        "url": "http://mydomain.dev",
        "method": "post"
    }
}'

The call will return OK -200 if successful, and a worker would now be ready to start processing jobs by calling the given endpoint.

When the endpoint is called, it will receive a JSON object with the job including its name, data and options in the body.

interface Job {
  name: string;
  data: any;
  opts: JobOptions;
}

The endpoint can do its thing with the job, and when it is ready it must return a status code between 200 and 209 to signal success, and return some optional data that will be stored in the job's returnvalue field. If the job should be marked as fail, the call should set a status code different than 200 to 209, and return some error message that will also be stored in the job's failedReason field.

Note that only one worker can be defined per queue. If the call is repeated for the same queue, it will just overwrite its options and restart the worker with those new options.

Last updated