All resources
Build5 min read

Writing your first job worker

A worker is your code that does the work behind a service task. Here is the loop it runs, in Node.js, Python and Java.

In short

  • 01

    A worker asks for jobs of a type, does the work, and reports the result.

  • 02

    Failures are retried; after the last retry an incident is raised.

  • 03

    Business errors are different from failures and can be caught in the process.

A service task with a job type does not run code itself. It creates a job and waits. A worker — a small program you write and run — picks the job up, does the work, and tells Orkovia how it went.

Service task
Job created
Worker activates
Your code runs
Complete or fail
Process continues

Node.js

import { HttpJobTransport, JobResult, WorkerClient } from '@orkovia/worker';

const transport = new HttpJobTransport(
  'http://localhost:3000/api/v1',
  '<token>',
  'worker-1',
);
const client = new WorkerClient(transport);

client.registerHandler('send-welcome-email', async (job) => {
  const to = job.variables.to;
  // ... send the email ...
  return JobResult.complete({ sent: true });
});

while (true) {
  await client.pollAndDispatch('send-welcome-email', 5);
}

Python

from orkovia_worker import HttpJobTransport, JobResult, WorkerClient

transport = HttpJobTransport(
    base_url="http://localhost:3000/api/v1",
    bearer_token="<token>",
    worker_id="worker-1",
)
client = WorkerClient(transport)


def send_welcome_email(job):
    to = job.variables.get("to")
    # ... send the email ...
    return JobResult.complete({"sent": True})


client.register_handler("send-welcome-email", send_welcome_email)

while True:
    client.poll_and_dispatch("send-welcome-email", max_jobs=5)

Java

In Java, annotate a method with the job type it handles. The method takes a Job and returns a JobResult.

@JobWorker(type = "send-welcome-email")
public JobResult sendWelcomeEmail(Job job) {
    // ... send the email ...
    return JobResult.complete(Map.of("sent", true));
}
The worker SDKs are not on public package registries yet. During the beta they are provided on request. Both the Node.js and Python SDKs have no runtime dependencies.

Three ways a job can end

OutcomeUse it whenWhat Orkovia does
CompleteThe work succeededMerges your variables into the instance and moves on
FailSomething technical went wrong (timeout, service down)Retries the job; when retries run out, raises an incident
Business errorA known business outcome, such as "card declined"Follows a matching error boundary event, if the process has one

Good to know

  • A job is locked while a worker holds it. For long work, send a heartbeat to keep the lock.
  • When failing a job you can say how long to wait before the next attempt.
  • Long polling and a streaming endpoint let a worker receive jobs within a moment, without hammering the API.
  • No SDK for your language? The same loop is three REST calls: activate, complete, fail.

See it on your own process

Book a walkthrough with the Orkovia team.

Request a Demo