Aller au contenu

serve

Ce contenu n’est pas encore disponible dans votre langue.

Runs the job state machine as a long-lived HTTP service, and with --watch starts jobs on its own for new objects in a bucket. The endpoints are thin wrappers over the same jobs the CLI runs in process, so a job behaves the same either way, and a job interrupted by a restart of the server stays in the state it was in on disk.

Terminal window
iamigrate serve --listen 127.0.0.1:8080 --store ./jobs/
FlagDescription
--listenAddress to listen on (default 127.0.0.1:8080)
--storeWhere the jobs’ state is kept: a local directory (default ./jobs/), or a remote store URI, see Job stores. --dir is its deprecated former name
--rescanHow often to look for unfinished jobs nobody runs, such as those of a replica that stopped (default 30s; 0 only looks at start)
--hookshooks.yaml path: the hooks every job fires, and the handlers job mappings can hand strategies to
--watchwatchers.yaml path: watchers that start a job from a template for each new object in a bucket
EndpointDoes
GET /The server’s watchers: {"watchers": [...]}, each with the jobs it created and its last error
POST /jobsQueue a job and start running it in the background. Answers 202 with the job record and its Location
GET /jobs/{id}The job record: state, params, and the result of each finished step
GET /jobs/{id}/reportWhat the finished steps found: manifest, problems, warnings, import, verify, and the hook calls that vetoed, failed or were ignored (hooks). Answers 409 while the job is still running
POST /jobs/{id}/retryPut a failed job back in a state and run it again, as job retry does. The optional body {"from": "exporting"} goes back to an earlier state. Answers 202 with the job record, 409 for a job that hasn’t failed, 400 for a state it can’t go back to

POST /jobs takes the same params as job create, under params:

Terminal window
curl -X POST http://127.0.0.1:8080/jobs -d '{
"params": {"target": "kratos", "admin_url": "http://kratos:4434", "users": "/srv/work/users.cmf.jsonl"}
}'

The keys are target, users, source, mapping, connection_id, upsert, in, format, source_mapping, admin_url, schema_id, schema_file, connection, connection_config, export_connection_id, export_fields, export_limit, tenant, filter, extension_attributes, hash_rounds and hash_mem_cost, each matching the job create flag of the same name. The params are checked as job create checks them, and a mistake is a 400. Poll GET /jobs/{id} until state is done or failed.

Errors are {"error": "..."} with the matching status: 400 for a bad request, 401 for a missing or wrong token, 404 for an unknown job, 409 for a request the job’s state doesn’t allow.

With --watch, serve also starts jobs on its own: a watcher creates a job from a template for each new object under a bucket prefix, such as a nightly export dropped in a bucket, with no scheduler or glue code calling POST /jobs.

watchers.yaml
version: 1
work_dir: ./watch/ # default ./watch/, relative to this file
watchers:
- name: nightly
bucket: s3://exports/nightly/
match: "*.cmf.jsonl.gz"
template: nightly.job.yaml
interval: 5m
KeyDescription
nameIdentifies the watcher in GET / and in its jobs’ IDs. Letters, digits, - and _
bucketWhere new objects are looked for: a local directory, or s3://bucket/prefix/, gs://bucket/prefix/ or azblob://container/prefix/, with the credentials of the job stores of the same scheme. The bucket is only read
matchOptional pattern the object’s base name must match (*, ?, [...]). Without it every object counts. Names starting with . never do
templateThe job template file, relative to this file
intervalHow often the bucket is listed (default 1m)
queueOptional URI of a queue the bucket’s event notifications arrive on, read instead of listing the bucket. See Event notifications

The job template is the body of a POST /jobs, whose param values can use the variables of the object that triggered the job:

nightly.job.yaml
params:
target: kratos
admin_url: http://kratos:4434
users: ${input}
VariableValue
${input}The object as a local file: the file itself for a local directory, a copy downloaded into the job’s work directory otherwise. The template must use it
${uri}The object’s URI
${key}The object’s key under the watched prefix
${version}The object’s version: its ETag (S3, Azure), its generation (Cloud Storage), its modification time and size (local)
${work}The job’s own directory, <work_dir>/<job id>/, for the files it writes: users: ${work}/users.cmf.jsonl when the object is an export to run through a source
${job}The job’s ID

The template is checked when serve starts, as POST /jobs checks params, and a mistake stops it. Each job is checked again when it is created; a refused one is reported as the watcher’s last error and not tried again until serve restarts.

Each object version starts one job at most. A job’s ID is derived from the watcher’s name, the object’s URI and its version, and the job store refuses to create a job twice: an object already handled is skipped after a restart, when several replicas watch the same bucket (one of them creates the job and runs it), and when a notification is delivered again. A new version of an object, written over it, is a new job. Every object already under the prefix when a watcher first starts gets a job too, so point a watcher at a prefix that only receives new drops.

A file in a local directory is picked up once it is unchanged across two listings, so a file still being written isn’t. An object in a remote bucket is only listed once complete. work_dir can’t be inside a watched local directory, or the jobs’ outputs would start jobs. With several replicas, work_dir must be on storage they share, like the other paths of a job.

Downloaded copies stay in work_dir after their job finishes. An object is held in memory while it downloads, so give the server memory for the largest one.

Listing a large bucket every interval is slow and costs requests. Where the provider sends event notifications to a queue, a watcher with a queue reads them instead, and starts a job for each object created under its bucket and matching its match:

  • Amazon S3 event notifications, sent to the queue directly or through an SNS topic;
  • Google Cloud Storage Pub/Sub notifications, with the JSON_API_V1 payload or none;
  • Azure Blob Storage events from Event Grid, in the Event Grid or CloudEvents schema.

A notification for anything else, such as a deletion or another bucket’s object, is acknowledged and ignored. A message is acknowledged only once the jobs it notifies of are in the job store; one that isn’t a notification, or whose object can’t be downloaded or whose job is refused, is handed back to the queue, so its redelivery and dead-letter settings apply. A redelivered message finds its job already created, and is acknowledged without creating another. The queue’s scheme must be one a message broker provider registers. serve refuses a scheme it doesn’t know, and names the ones it does.

GET / lists the watchers:

{
"watchers": [
{
"name": "nightly",
"bucket": "s3://exports/nightly/",
"match": "*.cmf.jsonl.gz",
"jobs_created": 3,
"last_job": "w1f0c...",
"last_check_at": "2026-11-12T03:05:00Z",
"last_error": "users-2026-11-11.cmf.jsonl.gz: downloading: s3: AccessDenied (status 403): Access Denied",
"last_error_at": "2026-11-12T02:05:00Z"
}
]
}

jobs_created and last_job count the jobs this server created since it started. last_check_at is the last time the bucket was listed, or the queue polled, successfully. last_error stays until another error replaces it: a last_error_at before last_check_at is an error the watcher has got past. Follow a job with GET /jobs/{id} as any other.

Credentials are never part of a request. Each step reads them from the environment of the serve process, as for job run, and the file paths in a job are paths on the machine it runs on, so treat access to the API as access to both.

Set $IAMIGRATE_API_TOKEN to require it as a bearer token (Authorization: Bearer <token>). Without a token, serve only accepts a loopback --listen address and refuses to start on any other.

SIGINT or SIGTERM stops the server and interrupts its running jobs, which stay in the state they were in. When serve starts it resumes every unfinished job in --store, the ones a crash or a shutdown interrupted, exactly as job run would: finished steps aren’t redone and an Auth0 import carries on from its chunk checkpoints. A job another process is running is left to it, and serve looks again every --rescan.

With a remote --store, such as gs://bucket/prefix, s3://bucket/prefix or azblob://container/prefix (see Job stores), several serve replicas can share the jobs: each job runs on one replica at a time, the one holding its lease. A job is started by the replica that received its POST /jobs (or retry), and the others pick up unfinished jobs nobody holds when they rescan, such as those of a replica that crashed, once its leases expire (60 seconds after its last renewal). The job’s files (users, mapping, …) must be at the same paths on every replica.

A server fires the hooks of the hooks.yaml it was started with, for every job. They come only from --hooks: a request can’t add a hook or change where one points. A job’s params have no hook key, and the mapping a job’s mapping param names can only select handlers that file declares, by name, never hold a URL. See Hooks for what fires when, and how to verify a request’s signature.