Skip to content

Jobs

Jobs are part of the beta

The jobs routes are new and may change more than the rest of Kiln.

A job is a saved machine spec that Kiln turns into a machine on a schedule or on request. Use it for work you repeat: a nightly export, a test run every hour, a script a workflow starts.

Each run is an ordinary lease. It is paid and limited like any machine, and its machine is deleted when its main process exits. It does not go back to saved, so runs do not pile up as saved machines: the job is the saved definition. The run's record keeps the exit code, the times and the log (7 days). Nothing else survives, so a job that produces something exports it itself, for example to Media or with ptkl and the run's PROJECT_API_TOKEN.

Id and name

A job has an id and a name. The name is unique in the project. Every route takes either: /v1/kiln/jobs/{id-or-name}.

What a job holds

  • The machine's spec: image, size (MiB), disk, minutes, sequence, cwd, egress, extra_hosts, intake, flat in the job as when you create a machine. A job has no start, because every run starts. The job's minutes are optional: with them each run pays them when it starts, and without them a run lasts until its command finishes, at most 24 hours (see Runs pay for the time they use).
  • Its environment, env: global env keys, { "TOKEN": { "global_env": "GITHUB_TOKEN" } }, and custom values, { "REGION": "eu" }. Each run reads the global keys when it starts, through the job's run_as, so the value is whatever that identity may read; see using a global env key, including the group the identity must be allowed to read. A custom value is kept encrypted with the job and never shown again: GET returns names only, env showing { "kind": "global_env", "global_env": "KEY" } or { "kind": "value" } for each. Every run is given the values. Setting, replacing or removing one needs secrets::write protokol-kiln; see Environment variables.
  • Its run identity, run_as. Every run acts as it, and the identity must hold machines::manage when the run starts, plus Media read if the job takes an intake, which also needs Media installed on the project from the marketplace. Otherwise the run fails with that reason. Nothing runs as an admin. Changing a job's code (its sequence, env, cwd, image, intake, egress or extra_hosts) needs you to hold its run_as, and is refused with 403 run_identity_refused otherwise. Its name, schedule, time zone, overlap, size, minutes and enabled need no hold.
  • The project env, project_env: dev or live, dev if left out. Every run is a lease for it, so the run's token acts in that env and PROJECT_ENV says which. The job shows it, and a change applies to the runs that start after it.
  • Every run has ptkl and its own PROJECT_API_TOKEN, API_HOST, PROJECT_UUID and PROJECT_ENV in its environment, as every machine does. The token holds what the job's run_as holds, in the job's project_env, and stops working when the run ends. Choose an identity that holds only what the job needs.
  • An optional schedule: a 5-field cron expression in schedule, and an IANA time zone in time_zone (UTC by default). The shortest allowed interval is 5 minutes, and a faster schedule is refused with 400. Without a schedule the job runs on request only. @daily style shortcuts are not accepted, and the time zone is its own field, not part of the expression. When clocks go back, a time that happens twice runs once, the first time. When clocks go forward, a time that does not exist runs at the minute the clock jumps to.
  • overlap and enabled.
{
  "name": "nightly-report",
  "size": 256,
  "minutes": 20,
  "sequence": { "steps": ["cd /srv/report", "python3 report.py"] },
  "intake": [{ "media": "/jobs/report", "to": "/srv/report" }],
  "env": { "API_TOKEN": { "global_env": "REPORT_API_TOKEN" } },
  "run_as": "prn:role:5f0c1c1e-2a58-4a38-a7c1-7d2f3a1f8b11",
  "schedule": "0 2 * * *",
  "time_zone": "Europe/Belgrade",
  "overlap": "skip",
  "enabled": true
}

POST /v1/kiln/jobs with this answers 201 with the job, including its uuid and next_run_at. overlap is required; minutes is optional. A name another job already has is 409 job_name_taken. A name is at most 80 characters, has no /, ? or #, and is never a uuid.

Change and delete a job

PATCH /v1/kiln/jobs/{id-or-name} takes any field of a create, and a field you leave out stays as it was. sequence, env, extra_hosts and intake are replaced whole when you send them. Because a value is never shown, { "keep": true } in env keeps the custom value the job holds for that name; a custom value you leave out is removed. Send "schedule": "" to make the job run on request only, and "enabled": false to pause the schedule without losing it. A change applies to the next run.

DELETE /v1/kiln/jobs/{id-or-name} answers 204. It stops the schedule. A run that is going keeps going to its end, and a queued run is skipped.

Start a run by hand

POST /v1/kiln/jobs/{id-or-name}/runs, by a caller with machines::manage. It answers 202 with the run. The run records who started it and still acts as the job's run_as. Send an Idempotency-Key so a retry does not start a second run.

If the job's overlap does not let another run begin, the answer is 409 job_busy. The attempt is recorded as a skipped run with the reason, so the history shows it. With queue, a manual run behind a running one waits, and answers 202 with the run queued.

Overlap

overlap decides what happens when a run is due while an earlier run of the same job is still going. You choose it per job. The dashboard asks for it when you create a job, and the API requires it.

Value What happens
skip The due run is recorded as skipped and nothing starts. A manual run answers 409 job_busy
parallel It starts its own machine, within your tier's machines at once
queue It waits (queued) and starts when the running one ends. At most one run waits. Further due runs are recorded as skipped, and a manual one answers 409 job_busy

Runs pay for the time they use

A run ends its lease when its main process exits, so the minutes it did not use come back, credits first and then GiB-hours, with the shortest lease counting as 5 minutes. How much a run pays up front depends on minutes:

  • With minutes (5 up to the tier's longest lease, never over 24 hours), each run pays all of them when it starts, from the tier's GiB-hours first and then credits, like any lease. Set minutes to the longest the job can need. The run is stopped when they are used up.
  • Without minutes, a run lasts until its main command finishes, at most 24 hours, so you do not need to know how long it will take. It pays its first hour when it starts, and shortly before the paid time ends it pays the next hour the same way (GiB-hours first, then credits). The last payment is cut so the run never passes 24 hours. When the command exits, the unused rest of the paid hour comes back as above.

If the next hour cannot be paid (the tier's limit is reached, or there are no GiB-hours or credits left, or the subscription is no longer active), the run is terminated: its machine is stopped, its lease ends, and the run is recorded as failed with a reason such as Stopped: not enough GiB-hours or credits left for the next hour. A run that reaches 24 hours is recorded as failed with Stopped: reached the 24 hour limit. If payments cannot be tried for a moment, for example because billing is unreachable, Kiln keeps trying until the time already paid for ends, and then ends the run as failed with Stopped: the next hour could not be paid.

To go back to a run that lasts until its command finishes, update the job with minutes: 0.

A run that the tier's limits refuse, or that cannot be paid for, is recorded as failed with the reason. The next scheduled run tries again. If Kiln was down when a run was due, one run starts when it is back, not one for every missed time.

Jobs cost nothing until they run, and each run counts toward your machines at once like any machine.

Run history

GET /v1/kiln/jobs/{id-or-name}/runs lists the last 50 runs, newest first, and GET /v1/kiln/jobs/{id-or-name}/runs/{run} reads one. A run has:

Field Meaning
state queued (waiting for the running one), started (it holds its machine), finished, failed or skipped. The last three are final
trigger schedule or manual
started_by Who started it. For a manual run, the caller
due_at, started_at, finished_at When it was due, started and ended
exit_code The main process's exit code, once it ended
machine_uuid Its machine. It is an ordinary machine, so while the run goes you can read its log, open a terminal or watch it like any machine. The machine is deleted when the run ends
reason Why a run was skipped, failed to start, or ran out of memory

The list of jobs, GET /v1/kiln/jobs, shows each job's last_run. The log of a run stays readable for 7 days.

Events and audit

Event When
kiln.job.run.started A run's machine was created
kiln.job.run.finished The run ended. Carries the exit code
kiln.job.run.failed The run could not start or failed. Carries the reason
kiln.job.run.skipped A due run was skipped by overlap

Each carries job_uuid, run_uuid, trigger, started_by and state, and machine_uuid once there is one. A workflow can start on these, as integration.kiln.job.run.<state>. The audit trail records kiln.job.create, .update, .delete and .run (who started it).