Skip to content
Loom Docs
Esc
↑↓navigate↵open⌘Jpreview
Product
View as Markdown
On this page

A document worker as a service

A team's shared index has no agent and no person whose machine could sync it.

A team’s shared index has no agent and no person whose machine could sync it. An operator runs the document worker as a service instead: one process with a service account’s token for Loom and, for each reader, the origin and the credential file its profile names. This guide deploys it: write a profile, mint the token, assign the source, start the process and read the status.

The worker is loom-source-sync, an entry point of the loom-source-worker package (source worker). It claims the sources assigned to its service account, one turn at a time, over POST /v1/{op}. It opens no listener, writes no file and keeps no state: Loom holds each source’s checkpoint and status. What it holds and what it is trusted with are in the worker boundary.

Write a profile and print its digest

A profile is a JSON file: the reader to run and that reader’s configuration.

{
  "reader": "pages",
  "origin": "https://docs.example.com",
  "credential_file": "/run/loom-source-sync/credentials/docs-token"
}
  • reader names a reader class the worker runs. A reader is registered under its name in READERS (source_worker/loom_source_worker/service_backend.py); the package registers none, so a deployment adds its own there. The other keys are the reader’s.
  • origin and credential_file are the same for every reader: the upstream’s https://host[:port], and a file that holds its bearer credential. The worker sends the credential only to origin and follows no redirect. Leave both out for a reader that needs neither.

Put the file in the directory the settings name as profiles_dir, then print its digest with the worker version that will run:

LOOM_SOURCE_SYNC_CONFIG=/etc/loom-source-sync/settings/config.json loom-source-sync --check

Each line is a profile’s digest and its file name. --check parses the settings and every profile, sends no request and needs no token. The digest is the SHA-256 of the profile’s canonical JSON, so it changes with any value and not with spacing or key order. The worker claims only a source whose profile_digest is the digest of a file there, so a profile nobody mounted reads nothing.

Mint the service account's token

The worker calls Loom as a service account: only a service account calling as itself is answered a turn. Give its token the writer role on each namespace it syncs and no role elsewhere (tokens and grants):

loom mint-token --service-account --subject sa:docs-sync --org acme \
  --namespaces team-docs --roles writer --ttl-days 30

Store the token in the secret store that mounts it as the settings’ loom_token_file: a regular file owned by root or the worker’s user, with mode 0400 or 0440. The worker reads the file before each request, so replacing the file rotates the token without a restart.

Assign the source

A namespace administrator, not the service account, assigns the source to the service account’s subject with knowledge.source.worker_assign (a service account’s source in a shared namespace):

loom knowledge source assign --namespace team-docs --name handbook \
  --worker-subject sa:docs-sync --reader pages --ref docs --generation 1 \
  --profile-digest <the digest of step 1> --selection listed --item <the container's id>
Field For a service
namespace A shared namespace: every reader of it reads every copy, so assign only what all of them may read upstream.
worker_subject The subject of the token of step 2.
reader The profile’s reader. A source approved on a profile for another reader ends blocked.
profile_digest The digest of step 1.
ref, items Names a person reads in Loom. The reader’s origin and credential come from the profile, never from these.

Start the worker

The settings file, named by LOOM_SOURCE_SYNC_CONFIG:

{
  "loom_origin": "https://loom.example.com",
  "loom_token_file": "/run/loom-source-sync/credentials/loom-token",
  "org": "acme",
  "profiles_dir": "/etc/loom-source-sync/profiles",
  "poll_seconds": 60,
  "documents_per_turn": 40
}

loom_ca_file names PEM roots for a Loom served under a private CA. An unknown key, a loom_origin that is not https://host[:port], a profiles_dir that cannot be listed, a file that is not a profile, a profile its reader does not admit and a token file that cannot be read stop the process when it starts, with a message that names no value (settings).

Run loom-source-sync, or the source-sync profile of source_worker/compose.yaml, or the chart’s second Deployment (the sync deployment):

helm upgrade --install docs-sync source_worker/chart -f sync-values.yaml

The Pod has no Service and no ingress rule, and its egress is limited to the DNS servers and HTTPS destinations the values list: Loom and each profile’s origin.

One process runs one turn at a time. A second process under the same subject only claims what the first left; for more throughput run more processes under more service accounts, each with its own assignments. A subject is offered 2,000 sources at most.

How the worker runs

  • After a turn that ended, the next claim follows at once. When nothing is due the worker waits what the claim answered, poll_seconds at most. A turn that a rate limit of Loom ended is followed by the wait that answer asked, poll_seconds at most.
  • A turn uploads documents_per_turn documents at most and ends importing when more are left; the next turn goes on from the checkpoint.
  • A document whose upload is over the request cap of Loom (LOOM_MAX_REQUEST_BYTES) or of a proxy in front of it is counted too_large, and the turn goes on.
  • SIGTERM stops the process. A request to Loom in flight is awaited to its answer and none is sent after it; a wait a rate limit asked for ends at once. A turn that is reading its upstream goes on until its next request to Loom, within the turn’s lease, and is cut with the process when that outlasts the grace period: a read holds no write. The turn’s lease ends by its age (LOOM_KNOWLEDGE_SOURCE_SYNC_MAX_PASS_SECONDS, 600 at the default), and the next claim resumes the source from the committed checkpoint.
  • A call to Loom that fails during a turn, as it does while Loom restarts, ends the turn. When the turn’s last commit still lands, as on another replica, the source shows failed with the reason internal and is retried after its wait. When it does not land, the turn’s lease ends by its age. In both cases the next turn resumes from the committed checkpoint.
  • A Loom that does not serve the turn ops is asked again every poll_seconds.
  • The log has one line a turn: the source’s namespace and name, the outcome and the counts. No claim, title or text is logged, and the process has no health endpoint.

How old a copy can be, and when Loom retires the copies of a source no turn verified, is the same as beside a host (how old a copy can be).

Read the status

knowledge.source.list shows the source’s sync.status to every reader of the namespace, and the dashboard’s source row shows the same (every status). For a service:

status reason For a service Remedy
ok, importing The worker reads the source; importing goes on with the next turn. None.
needs_reconnect The profile’s credential file could not be read, or the upstream refused the credential. Replace the credential in the secret store, then run sync_now.
blocked mapping_changed The assignment’s reader is not the reader of the profile with its digest, or the reader refused it. Correct the assignment, or mount the changed profile and assign the source with its digest.
blocked tool_not_allowed The worker holds the profile and has no reader class of that name. Run a worker build that registers the reader, then run sync_now.
failed rate_limited, upstream_unavailable, timeout, protocol, internal Loom, the upstream or the reader failed. None: the turn is retried by itself, each wait twice the last up to six hours.
revoked The reader reported that access was withdrawn; Loom retired the source’s copies. Assign the source again.

A source that shows no status and no sync.last_synced_at was never claimed. The worker is not running, its token does not hold the writer role on the namespace or is not a service account’s own, its subject is not the assignment’s worker_subject, or no file in profiles_dir has the assignment’s profile_digest: loom-source-sync --check prints the digests the worker holds.

A worker whose claim fails syncs nothing and shows no status either. Its log has the line source claim failed: code=<CODE>, once a day for each code: AUTH_INVALID for a token Loom does not accept, such as one that expired, UNAVAILABLE when Loom cannot be reached or the token file cannot be read, and UNREADABLE for an answer that is not Loom’s, such as a proxy’s error page. Any other code is the error code Loom answered the claim with.

Last updated on

Was this page helpful?