Delivery service contract¶
delivery-service is the last-mile courier of the shop: every paid order gets a
delivery, a simulated courier moves it to the customer's door (or back to the
sender), and its tracking UI is the stack's first micro-frontend. It is an
independent product: it reads the shop's commerce events, never writes to the
backend or warehouse-service, and ships its own UI.
| Item | Value |
|---|---|
| Repository | slawekradzyminski/delivery-service-rs |
| Stack | Rust (axum, sqlx, fe2o3-amqp); micro-frontend in TypeScript + Lit |
| Image | slawekradzyminski/delivery-service-rs (distroless, amd64 + arm64), about 10 MiB RSS |
| Port | 4006 (never published on the host) |
| Database | Schema and role delivery in the backend's database (testdb, aitesters on the sandbox) |
| Healthcheck | ["CMD", "/delivery-service-rs", "healthcheck"] |
| Memory | mem_limit: 64m in every profile |
| OpenAPI | /delivery/v3/api-docs through the gateway ("Delivery service") |
| Micro-frontend | /mfe/delivery/remote.js through the gateway |
How it relates to warehouse-service¶
warehouse-service already simulates a carrier for its shipments and moves the
order to DELIVERED (see the tracking addendum).
That stays the order's record. delivery-service keeps its own delivery next to
it and never changes the order or sends customer emails:
- failed attempts and returns are delivery-service facts only (the order stays
SHIPPED); - the shop's
shipment.deliveredcloses an open delivery asDELIVERED; - when the courier is already returning the parcel, the delivery keeps its state
and is flagged
discrepancy = true: two systems that disagree, a deliberate exercise in eventual consistency.
State machine¶
order.paid shipment.shipped (handover)
(none) ─────────────▶ BOOKED ───────────────────────────▶ PACKED
│ order.cancelled │ courier step
▼ ▼
CANCELLED IN_TRANSIT
│ courier step
▼
┌──────────────── OUT_FOR_DELIVERY ◀───────┐
success │ │ failure │ step (attempt < max)
▼ ▼ │
DELIVERED ◀── shipment.delivered ── FAILED_ATTEMPT
│ step (attempt = max)
▼
RETURNING ──step──▶ RETURNED
Every order that reaches PAID already holds a confirmed stock reservation
(reserve and confirm happen at checkout), so order.paid is the booking trigger.
The courier starts at the handover. Each step takes DELIVERY_COURIER_STEP_INTERVAL
(PT15S locally, PT1M on the server). A doorstep attempt fails with probability
DELIVERY_COURIER_FAILURE_RATE (0.2), decided by a hash of the order id, the
attempt and DELIVERY_COURIER_SEED, so one order always tells the same story.
After DELIVERY_COURIER_MAX_ATTEMPTS (2) failed attempts the parcel goes back.
Events consumed¶
No producer changes. The service reads the existing events:
| Event | Effect |
|---|---|
order.paid |
Book (BOOKED), destination city from the order snapshot when the backend allows it |
shipment.shipped |
BOOKED → PACKED (books first when order.paid was never seen) |
order.cancelled |
BOOKED → CANCELLED; later states are left alone |
shipment.delivered |
Close as DELIVERED, or flag a returning delivery |
- Feed (every profile):
GET /internal/v1/eventsevery 5 s with its cursor (EVENT_FEED.md). - Each event is applied in one transaction with its id in
processed_events. Events older thanEVENTS_MAX_AGE(24 h) are recorded without effect. - An event that can never be applied (no id, no order id) is parked in
delivery.parked_eventswith the reason and passed; an outage stops the run and the next poll retries it.
Public API¶
| Method and path | Access |
|---|---|
GET /api/v1/deliveries?status=&username=&page=&size= |
own deliveries; ROLE_ADMIN and ROLE_WAREHOUSE: all |
GET /api/v1/deliveries/{id} |
owner, admin, warehouse |
GET /api/v1/orders/{orderId}/delivery |
owner, admin, warehouse |
GET /api/v1/deliveries/track/{trackingCode} |
public; without order, user and address |
POST /api/v1/deliveries/{id}/courier/advance |
owner, admin, warehouse: one step now |
PUT /api/v1/deliveries/{id}/courier/script {"attempts":["FAIL","DELIVER"]} |
owner, admin: outcome of the next attempts |
PUT /api/v1/deliveries/{id}/courier/paused {"paused":true} |
owner, admin: freeze this delivery |
GET /api/v1/admin/deliveries/courier |
admin: the courier settings in effect |
The test controls work per delivery, so on the shared production sites nobody
changes everyone's courier. Errors are {"message": ...}; tokens are the
backend's RS256 JWTs, verified with /internal/v1/jwks.json like the other services.
Micro-frontend¶
delivery-service serves two custom elements in one ES module at
/mfe/delivery/remote.js: <awesome-delivery-tracking order-id="…"> and
<awesome-delivery-list>. The frontend loads it at run time on
/orders/:id/delivery and /deliveries through a generic RemoteElement
loader; nothing of it is in the frontend build. Contract: properties
apiBaseUrl and getAccessToken, events delivery:unauthorized (the shell
refreshes the session and calls reload()) and delivery:navigate. The
elements render in an open shadow root with delivery-* test ids. When the
module cannot be loaded, the page shows a notice and the rest of the app works.
Backend support¶
backend-rs 1.2.0 (backend-rs#6) adds what delivery-service needs from the backend, without changing any existing contract:
- the internal caller
DeliverywithDELIVERY_SERVICE_TOKEN, accepted on/internal/v1/jwks.json,/internal/v1/eventsandGET /internal/v1/orders/{id}(token verification, the feed transport and the destination city); - the Swagger UI definition "Delivery service" →
/delivery/v3/api-docs.
With an older backend every authenticated API call answers 503 "Backend unavailable" (the JWKS request is refused) and the feed answers 401, so no delivery is booked.
Deployment¶
- Compose:
delivery-servicein the lightweight, full and server profiles, andaitesters-delivery-serviceon the server, after the one-shotpostgres-rolesservice (roledelivery,DELIVERY_DB_PASSWORD). - Gateway:
/api/v1/deliveries,/api/v1/deliveries/,/api/v1/admin/deliveries/,~ ^/api/v1/orders/\d+/delivery$,^~ /delivery/v3/api-docsand^~ /mfe/delivery/in every application server block, with a variable upstream. - Vault:
delivery_service_token,aitesters_delivery_service_token,delivery_postgres_password,aitesters_delivery_postgres_password(.env.delivery,.env.aitesters-delivery). The deploy refuses to start without them and without a published image digest. - The aitesters nightly reset recreates
aitesters-delivery-servicewith its database. - The deploy's verify role checks the direct and public routes and the role isolation.