Skip to content

Deploy a worker ​

A worker is your own program, in your own image, with the SDK copied into it. It needs two values and one open route: the server's address, the SDK token, and outbound HTTPS to the server.

What it needs ​

WhatValue
The server's addressIts public origin, https://jobs.example.com. The examples read it from JOOBQ_GATEWAY_URL.
The SDK tokenThe value saved in Settings → Security on the server. The examples read it from JOOBQ_SDK_TOKEN.
NetworkOutbound HTTPS to the server. Nothing connects to the worker; it needs no open port.

It needs no Redis address, no database, no payload keys and no licence: the server holds all of them. Fields a job marks as sealed travel in plain text between the worker and the server, which is one more reason to use HTTPS.

1. Set the SDK token on the server ​

Once, as an admin: Settings → Security → SDK gateway token, 32 characters or more (openssl rand -hex 32). Until it is set the gateway answers 503 sdk_token_not_configured.

2. Write the worker ​

Copy the SDK into your project, register your jobs and workflows, and start the worker. Connecting and starting, in each language:

crystal
worker = JoobQPro::Workflows::Worker.new(activity_workers: 8)
Conf.blueprints.each { |bp| worker.register(bp) }
worker.start
registry = JoobQ::WorkerRegistry.new(id: Conf::WORKER_ID)
registry.start
spawn { JoobQ.forge(trap_signals: false) }
stop = Channel(Nil).new
{Signal::INT, Signal::TERM}.each { |sig| sig.trap { stop.send(nil) } }
stop.receive
worker.stop
JoobQ.shutdown
registry.stop
python
client = JoobQClient(os.environ["JOOBQ_GATEWAY_URL"], os.environ["JOOBQ_SDK_TOKEN"])
worker = JoobQWorker(client, WORKER_ID, [QUEUE, WF_QUEUE], **WORKER_OPTIONS)
for j in JOBS:
    worker.register_job(j)
for bp in blueprints():
    worker.register_workflow(bp)
suite_settings(worker)  # test suite only: its middleware
worker.start()
ts
const client = new JoobQClient(env('JOOBQ_GATEWAY_URL'), env('JOOBQ_SDK_TOKEN'))
const worker = new JoobQWorker(client, { workerId: WORKER_ID, taskQueues: [QUEUE, WF_QUEUE], ...WORKER_OPTIONS })
for (const j of jobs) worker.registerJob(j)
for (const w of workflows) worker.registerWorkflow(w)
suiteSettings(worker) // test suite only: its middleware
await worker.start()
ruby
client = JoobQ::Client.new(gateway_url: ENV.fetch("JOOBQ_GATEWAY_URL"), token: ENV.fetch("JOOBQ_SDK_TOKEN"))
worker = JoobQ::Worker.new(client: client, worker_id: WORKER_ID, task_queues: [QUEUE, WF_QUEUE], **WORKER_OPTIONS)
JOBS.each { |j| worker.register_job(j) }
workflows.each { |w| worker.register_workflow(w) }
suite_settings(worker) # test suite only: its middleware
worker.start
go
client := joobq.NewClient(os.Getenv("JOOBQ_GATEWAY_URL"), os.Getenv("JOOBQ_SDK_TOKEN"))
worker := joobq.NewWorker(client, workerID, []string{queue, wfQueue})
worker.Concurrency = 8
for _, j := range jobs {
	worker.RegisterJob(j)
}
for _, w := range workflows {
	worker.RegisterWorkflow(w)
}
suiteSettings(worker) // test suite only: lease TTL, service name, middleware
if err := worker.Run(context.Background()); err != nil {
	fmt.Fprintln(os.Stderr, err)
	os.Exit(1)
}
rust
let mut client = JoobQClient::new(var("JOOBQ_GATEWAY_URL"), var("JOOBQ_SDK_TOKEN"));
client.service_name = Some(format!("conformance-{}", lang));
let mut worker = Worker::new(client, worker_id, vec![queue.clone(), wf_queue.clone()]);
worker.concurrency = 8;
for j in jobs {
    worker = worker.register_job(j);
}
for w in workflows {
    worker = worker.register_workflow(w);
}
worker = suite_settings(worker); // test suite only: lease TTL, middleware
if let Err(e) = worker.run() {
    eprintln!("{}", e);
    std::process::exit(1);
}
java
JoobQClient client = new JoobQClient(System.getenv("JOOBQ_GATEWAY_URL"), System.getenv("JOOBQ_SDK_TOKEN"));
JoobQWorker worker = new JoobQWorker(client, WORKER_ID, List.of(QUEUE, WF_QUEUE)).concurrency(8);
jobs.forEach(worker::register);
workflows.forEach(worker::register);
suiteSettings(client, worker); // test suite only: lease TTL, service name, middleware
worker.run();
php
$client = new JoobQClient((string)getenv('JOOBQ_GATEWAY_URL'), (string)getenv('JOOBQ_SDK_TOKEN'));
$worker = new JoobQWorker($client, WORKER_ID, [QUEUE, WF_QUEUE], ...$workerOptions);
foreach (JOBS as $job) {
    $worker->registerJob($job);
}
foreach ($workflows as $w) {
    $worker->registerWorkflow($w);
}
suite_settings($worker); // test suite only: its middleware
$worker->run();
csharp
var client = new JoobQClient(Env("JOOBQ_GATEWAY_URL"), Env("JOOBQ_SDK_TOKEN"));
var worker = new JoobQWorker(client, WorkerId, new[] { Queue, WfQueue }) { Concurrency = 8 };
foreach (var j in jobs) worker.Register(j);
foreach (var w in workflows) worker.Register(w);
SuiteSettings(client, worker); // test suite only: lease TTL, service name, middleware
await worker.RunAsync();
elixir
worker = [
  url: System.fetch_env!("JOOBQ_GATEWAY_URL"),
  token: System.fetch_env!("JOOBQ_SDK_TOKEN"),
  worker_id: System.fetch_env!("CONF_WORKER_ID"),
  queues: [Conformance.queue(), Conformance.wf_queue()],
  workers: @jobs,
  workflows: Conformance.Workflows.all(),
  # What the test suite sets on its worker; the harness chooses the lease TTL (CONF_LEASE_TTL_MS).
  concurrency: 8,
  service: "conformance-#{Conformance.lang()}",
  lease_ttl_ms: String.to_integer(System.get_env("CONF_LEASE_TTL_MS", "30000")),
  # Test suite only: the two middlewares of worker_middleware, the first listed outermost.
  middleware: [M1, M2]
]

Supervisor.start_link([{JoobQ, worker}], strategy: :one_for_one, name: Conformance.Supervisor)

Tested against a real server in every language shown.

The samples are the workers of the SDK test suite, so they carry its names: choose a worker id and queues of your own, and leave out the line marked test suite only.

How to reference the SDK, and what a worker does on the wire, is in SDKs: install and connect.

A worker that registers jobs is a queue worker; one that registers workflows is a workflow worker and needs a JoobQ Pro key on the server. One program can be both: see Queue workers and workflow workers.

3. Run it as a service ​

Package it like any other long-running process of yours. Three rules:

  • Pass the token as a secret, not in the image and not in a command line.
  • Stop it with SIGTERM and wait. It finishes what it holds within 30 seconds by default (drain_timeout); give the platform a stop timeout above that (the Compose file below allows 40).
  • Let the platform restart it. While the server cannot be reached the worker waits and retries by itself (250 ms, doubling up to 5 seconds); it does not exit.

A worker has no HTTP listener, so it has no HTTP health check: watch the process, and the console's Workers page.

With Docker Compose, that is one short file of your own, on any host that reaches the server. Save it as worker.compose.yml and name your image in it:

yaml
services:
  worker:
    image: registry.example.com/acme/worker:1.0.0   # your image: your program with the SDK in it
    environment:
      JOOBQ_GATEWAY_URL: https://jobs.example.com    # the server's public address
      JOOBQ_SDK_TOKEN: ${JOOBQ_SDK_TOKEN:?set it in worker.env}
    read_only: true
    tmpfs: ['/tmp:size=16m,noexec,nosuid']
    cap_drop: [ALL]
    security_opt: [no-new-privileges:true]
    init: true
    stop_grace_period: 40s
    restart: unless-stopped

The token goes in a file beside it that only you can read, and Compose hands it to the container:

sh
(umask 077 && echo "JOOBQ_SDK_TOKEN=<the token saved in Settings → Security>" > worker.env)
docker compose -f worker.compose.yml --env-file worker.env up -d

The container runs with a read-only file system, no Linux capabilities and a small /tmp; if your program writes anywhere else, mount a volume there. It publishes no port, because nothing connects to a worker. To run more of them, add --scale worker=3 and give each process a worker id of its own, for example one that ends in the container's host name.

Check it worked ​

  1. The worker shows on the console's Workers page within a few seconds, with its queues. It registers again every 10 seconds: that is its heartbeat.

  2. From the worker's host, the server answers:

    sh
    curl -sS "$JOOBQ_GATEWAY_URL/api/v1/sdk/openapi.json" -o /dev/null -w '%{http_code}\n'   # 200
  3. Enqueue a job of a type the worker registered, and watch it complete on the Jobs page.

  4. For a workflow worker: start a run of one of its workflows and watch it complete on the Workflows page.

If it does not: Worker troubleshooting.

Next ​