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.
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));
}Three ways a job can end
| Outcome | Use it when | What Orkovia does |
|---|---|---|
| Complete | The work succeeded | Merges your variables into the instance and moves on |
| Fail | Something technical went wrong (timeout, service down) | Retries the job; when retries run out, raises an incident |
| Business error | A 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.
Keep reading
Build without the backend: mocking service tasks
Give a service task a pretend response, delay and failure rate, so you can run the whole process before the real system or worker exists.
Read articleCalling external systems with connectors
Call REST, GraphQL and SOAP services or send email straight from a service task — configured, not coded.
Read articleMessages, signals and timers explained
Three ways a process can wait for something to happen. When to use each, with examples.
Read article