Skip to Content
WorkloadsJob

Start a run from a Function

Start a Job's run from a Function's code, read the run back, and start one run for an event that arrives more than once.

Usage

A Function starts a run of a Job when an event or a schedule calls for work that runs to its end, such as a monthly report. The Function's spec.bindings.job names the Job under an alias. Its code calls ctx.job.start, context.job.start in Python, with that alias and the run's input, and gets the run's name back at once, without waiting for the run. It reads the run back with ctx.job.get: its phase and, once the run Succeeded, its result.

This example applies the Job report of the Job page's example, or pyreport in Python, and the Function planner, or pyplanner, which binds it under the alias report. A call of the Function with a month starts that month's run of the Job and answers the run's name and phase. The Job's report.yaml sets approval.bypass, so each run runs at once; a run of a Job without it waits for a person's approval, whoever starts it, as Approve a run shows.

It needs funcdctl (Install), Git, curl, Node.js for the TypeScript section, Python 3.14 for the Python section, and a funcd server on the same machine, started as the Job page starts it: funcd --memory, with a data directory that it can write. This page also calls the Function on funcd's data plane, at 127.0.0.1:8081. The data plane listens on a free port of funcd's choosing unless FUNCD_DATA_PLANE_ADDR sets one, since server.dataPlaneAddr defaults to 127.0.0.1:0, so a server started with the Job page's command must start again with this one before the calls below:

$ FUNCD_DATA_DIR="$(mktemp -d)" FUNCD_DATA_PLANE_ADDR=127.0.0.1:8081 FUNCD_PYTHON="$(command -v python3.14)" funcd --memory

Its API listens at 127.0.0.1:8080 and accepts the built-in token funcd-dev-token in the namespace default. funcd runs the TypeScript code with the Node.js on its PATH. funcd --memory keeps nothing when it stops, so the steps below apply the Job's report.yaml again.

The handler names the run after the month, report-<month>, or pyreport-<month> in Python. A start under a name that a run of the bound Job holds, with the same input, answers that run's name and starts nothing, so a second call for one month starts no second run. The run passes the checks of a run that a person starts with funcdctl job run: its input must fit job.payloadLimit and the Job's contract, as Handle a refused start or read explains.

The handler, src/main.ts:

// planner: a Function that starts a run of the Job report for the month of its event and reads the run back, through
// the job binding of its Function (funcd ADR-0349). The run is named after the month, so a start sent again for one
// month, with its input, returns the run that the first start made instead of starting a second one.
//
// Authored in TypeScript against @funcd-dev/shim; `yarn build` (vite.config.ts) bundles it to planner.mjs.
import type { CloudEvent, FunctionContext } from '@funcd-dev/shim';

// #region types
export interface PlanInput {
  /** The month to report on, as YYYY-MM. */
  month: string;
}
export interface PlanOutput {
  /** The name of the run of report. */
  run: string;
  /** The run's phase when the planner read it back. */
  phase: string;
}
// #endregion types

// #region handler
export async function handle(ctx: FunctionContext, event: CloudEvent<PlanInput>): Promise<PlanOutput> {
  const month = event.data?.month ?? '';
  const run = await ctx.job.start('report', { month }, `report-${month}`);
  const { phase } = await ctx.job.get('report', run);
  return { run, phase };
}
// #endregion handler

Its contract, in planner.contract.yaml:

runtime: nodejs22
input:
  type: object
  additionalProperties: false
  properties:
    month:
      type: string
  required:
    - month
output:
  type: object
  additionalProperties: false
  properties:
    run:
      type: string
    phase:
      type: string
  required:
    - run
    - phase

The Function planner, in planner.yaml:

# planner: a Function that starts a run of the Job report and reads it back, through its job binding.
apiVersion: funcd.io/v1alpha1
kind: Function
metadata:
  name: planner
  namespace: default
  resourceGroup: rg1
spec:
  runtime: nodejs22
  handler: handle
  image: oci-layout:///mnt/funcd-deps/registry:planner   # funcdctl push planner.mjs (built from src/main.ts)
  bindings:
    job:
      - alias: report   # the name that the handler passes to ctx.job
        job: report     # the Job of the namespace that it may start and read

Clone the repository, or reuse the clone of the Job page's example. In examples/job-report, push the Job's build, as the Job page does:

$ git clone https://github.com/pyvvo/funcd-typescript
$ cd funcd-typescript/examples/job-report
$ funcdctl push report.mjs "oci-layout://$PWD/registry:report"
oci-layout:///home/dev/funcd-typescript/examples/job-report/registry:report@sha256:aff19a7ace15032acd1aea667bd124dfd5e408ae0bd30bced659559cb86735e8

Replace image in report.yaml with the reference that the push prints, and apply it. Then push the Function's committed build planner.mjs with its contract beside it:

$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token apply -f report.yaml
applied Job/report
$ cd ../job-planner
$ funcdctl push planner.mjs "oci-layout://$PWD/registry:planner"
oci-layout:///home/dev/funcd-typescript/examples/job-planner/registry:planner@sha256:8696a1f3e12024256b8f21f7281c1e552353f621ee9e031da31dbb0b4de328b4

Replace image in planner.yaml with the reference that the push prints. Apply the Function, repeat get until its status.phase is Idle or Ready, and call it twice for one month on the data plane. Then repeat funcdctl job describe until the run's phase is Succeeded:

$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token apply -f planner.yaml
applied Function/planner
$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token get function planner -n default -o json
$ curl -s -X POST -H 'content-type: application/json' -d '{"month":"2026-09"}' http://127.0.0.1:8081/function/planner
{"run":"report-2026-09","phase":"Running"}
$ curl -s -X POST -H 'content-type: application/json' -d '{"month":"2026-09"}' http://127.0.0.1:8081/function/planner
{"run":"report-2026-09","phase":"Running"}
$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token job describe report-2026-09
RUN report-2026-09   job: report   phase: Succeeded
revision: report-v1   digest: sha256:aff19a7ace15032acd1aea667bd124dfd5e408ae0bd30bced659559cb86735e8
started by: Function::"default/planner"
started: 2026-11-24T02:53:01.326Z   ended: 2026-11-24T02:53:04.443Z   duration: 3s117ms
result: {"month":"2026-09","rows":30}
trace: 7a092419a7543ad3562f4c2f7db1511f
full logs: funcdctl job logs report-2026-09
$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token job runs report
NAME
report-2026-09

The two calls started one run. The phase that a call answers is the run's phase when the handler read it, such as Pending or Running. The run's started by is the Function, Function::"default/planner".

Read a run back

ctx.job.get answers a JobRunState, a type that @funcd-dev/shim exports: the run's name, phase and reason, its startedAt and endedAt, and, once the phase is Succeeded, its result, each of the last three null until set. In Python, context.job.get answers a dict, JobRunState in funcd_shim, with the keys name, phase, reason, started_at, ended_at and result, each of the last three None until set. ctx.job.start and ctx.job.get never wait for a run: a Function's call has a deadline, and a run may take hours. A Function that needs a run's end reads the run again in a later call, such as one that a schedule or a later event makes.

A Function reads only the runs of the Jobs that it binds: a run of another Job reads as not found, and so does a run that job.retention removed.

Start one run when an event arrives more than once

A Function that funcd calls at least once, as a Sensor's action, can be called twice for one event, as Read the event in the handler says, and each call that runs ctx.job.start without a name starts a run of its own. With a name as the third argument, made from the event, the second call answers the first call's run and starts nothing. The name is a run's name: a DNS-1123 label of at most 63 characters, else the start is refused with 400. funcd refuses a start under a name that another run of the namespace holds, of another Job or with another input, with 409.

A start answers the run of its name whatever the run's phase: once that run has ended, Failed or Cancelled included, the start answers it, with its phase, and starts nothing. A new run under that name can start only once job.retention removes the old one, so a start that must run the work again takes another name.

Handle a refused start or read

When funcd refuses a start or a read, ctx.job.start and ctx.job.get reject with a JobError, and raise one in Python. In TypeScript it is an Error whose name is JobError, a type that @funcd-dev/shim exports, with status, the HTTP status, and problem, funcd's problem, whose detail says why. In Python, JobError is a RuntimeError that funcd_shim exports, with status and problem, a dict with the key detail. A 409 from a start, for example, means that another run holds the name; the Handler API lists each status with its cause.

A start whose input does not match the Job's input schema is refused with 422 and the body {"error": …, "details": […]} that a Function's call answers for an input off its contract; its JobError keeps that body as its detail. funcd checks a start's input only when the Job holds the contract of its current spec. A start whose input is off the contract therefore succeeds before funcd first checks the Job, or after an edit until funcd checks it again, as the Job page's Status says, and its run ends Failed with InputSchemaMismatch when it starts.

Access

A Function needs no role to start the runs of a Job that it binds: each entry of spec.bindings.job grants it job::start and job::read on its Job, and nothing on any other Job. A call on an alias that the Function does not bind is refused with 403, and a Job's own spec cannot bind a Job. The binding grants through this built-in policy:

Built-in Job binding policy
@id("job-start-binding")
permit(principal, action == Action::"job::start", resource)
  when { principal has jobBindings && principal.jobBindings.contains(resource) };

@id("job-read-binding")
permit(principal, action == Action::"job::read", resource)
  when { principal has jobBindings && principal.jobBindings.contains(resource) };

A namespace Policy that forbids one of the actions revokes it, and the start or the read is refused with 403. This Policy forbids planner to start runs of report:

apiVersion: funcd.io/v1alpha1
kind: Policy
metadata:
  name: no-planner-start
  namespace: default
  resourceGroup: rg1
spec:
  cedar: |
    forbid(
      principal == Function::"default/planner",
      action == Action::"job::start",
      resource == Job::"default/report"
    );

Save it as forbid-start.yaml, apply it, and call planner again. Its start is refused with 403, and planner, which does not catch the JobError, answers its own call with 500 and the error:

$ funcdctl --server http://127.0.0.1:8080 --token funcd-dev-token apply -f forbid-start.yaml
applied Policy/no-planner-start
$ curl -s -X POST -H 'content-type: application/json' -d '{"month":"2026-11"}' http://127.0.0.1:8081/function/planner
{"error":"context.job.start(\"report\") failed: 403 funcd.jobGrants.Grant: default/planner may not job::start on Job \"report\": cedar: forbidden by policy default/no-planner-start#0"}

Changes and deletion

  • Deleting the Job leaves the binding in place: a start on its alias is refused with 404 until a Job of that name exists again, and the runs it already had stay readable until job.retention removes them.

Examples

On this page