Skip to main content
Version: 2.0

Worker Gateway

With MilvaionConfig:Transport:Type set to Postgres, workers connect to the API over HTTP instead of to a broker. They need the API's address and an API key, and no access to the database. On every other transport the gateway answers 503.

API key​

Create an API key with the WorkerGateway.Connect permission.

  • The key receives jobs of its own project only, and only for workers it has registered.
  • A worker id belongs to the project that registered it first. Another project gets 409 until the keys of the first one are revoked.
  • To serve another project, run a worker with a key created in that project, under its own worker id. Moving a job to another project moves it away from gateway workers of the old project.
  • Status updates and logs about other projects' occurrences are ignored.
  • {{secret:name}} references and the AI connection of a job are resolved when the job is handed out. Serve the API over HTTPS.
  • AI budgets are checked when a job is dispatched and again when it is handed out. A job whose budget runs out while it waits in the queue fails.

Endpoints​

All endpoints are under /api/v1/worker-gateway. Send the key in the X-ApiKey header, the same one the rest of the API uses. Bodies are JSON. Message bodies are those of the worker protocol.

EndpointPurpose
GET pingChecks the key and the gateway
POST registerRegisters a worker instance and declares its queue. Repeat it on every start
POST heartbeatInstance heartbeat
POST lease{ workerId, maxJobs, waitSeconds, excludedJobTypes }. Waits up to 30 seconds for a job
POST renew{ workerId, leases: [{ id, leaseId }] }. Call it every third of leaseSeconds while jobs run
POST settle{ workerId, id, leaseId, settlement, retryCount, maxRetries, delaySeconds, exception, status }
POST statusStatus update of an occurrence
POST status/batchUp to 1000 status updates of one worker, as a JSON array. Send the updates that wait while a request is on its way together
POST logs?workerId=Up to 1000 log entries
POST cancellations{ workerId, occurrenceIds, waitSeconds }. Returns occurrences to stop, waiting up to 30 seconds

settlement is 0 (ack), 1 (requeue), 2 (retry after delaySeconds) or 3 (dead letter). 409 means the lease is not held any more: the job is delivered again.

Delivery​

Delivery is at least once. A lease that is not renewed expires after PostgresTransport:LeaseSeconds, and the job is delivered again with redelivered set. After PostgresTransport:MaxDeliveries such deliveries the job becomes a failed occurrence.

.NET workers​

Set the transport of the worker SDK to Http. The worker then uses no broker and no Redis; other Worker settings stay as they are.

"Worker": {
"WorkerId": "email-worker",
"Transport": { "Type": "Http" },
"Http": {
"BaseUrl": "https://milvaion.example.com",
"ApiKey": "<api key>"
}
}
SettingDefaultDescription
Http:BaseUrl-Address of the API, with its base path if it has one
Http:ApiKey-API key. Pass it as Worker__Http__ApiKey, not in a committed file
Http:RequestTimeoutSeconds30Timeout of a request, on top of the time a long poll waits
Http:LeaseWaitSeconds20How long a request for jobs waits for one, at most 30

Retries wait in the API, so a failed job holds no slot of the worker. Quartz and Hangfire workers publish over RabbitMQ and cannot use Http.

Other languages​

The Node.js, Python, Go, Java, Rust, PHP and Ruby SDKs have the same transport. Set it to http and give the base URL and the API key; each SDK's README lists its option names. They also read MILVAION_BASE_URL and MILVAION_API_KEY from the environment. In Rust it is behind the http cargo feature. A PHP worker runs one job at a time, so it asks for cancellations of its running job every 2 seconds instead of waiting for them.