How-to: submit a serverless job asynchronously and poll it to completion
This is a worked task for long-running jobs. The reference pages
(rp doc serverless run, rp doc serverless status) document the individual
flags; this page shows the two commands composed into a submit-and-wait loop.
For the synchronous one-shot path, see
Submit a job to a serverless endpoint with a JSON input
payload.
Goal: queue a job with --async, capture its job id, and drive it to a
terminal state from a script — without holding one HTTP request open for the
whole run.
Steps
- Queue the job.
--asyncreturns immediately; the confirmation goes to stderr and the job id is printed on stdout, so capture it directly:
$ JOB=$(rp serverless run end_abc --input '{"prompt":"hi"}' --async)
- Check the job whenever you like.
rp serverless status <endpoint-id> <jobId>prints the job payload and exits with the job's terminal-status code:
$ rp serverless status end_abc "$JOB"
- Poll to completion. The exit code alone cannot drive the loop — both
COMPLETEDand the in-flight states exit 0 — so read.statusuntil it is terminal, then use the exit code as the verdict:
$ while :; do
out=$(rp serverless status end_abc "$JOB" --json)
s=$(printf '%s' "$out" | jq -r '.status')
case "$s" in
COMPLETED | FAILED | CANCELLED | TIMED_OUT) break ;;
esac
sleep 10
done
$ rp serverless status end_abc "$JOB" >/dev/null && echo done || echo "job failed: $s"
Notes
- Exit-code contract for
rp serverless status:0when the job isCOMPLETEDor still in flight (IN_QUEUE/IN_PROGRESS);1for the terminal failuresFAILED,CANCELLED, andTIMED_OUT. The payload still prints on failure, so the job's error is visible. - Both commands take the endpoint id and the job id verbatim; neither
resolves names.
--jsononstatusprints the raw response for scripting. - The submission rides the data plane (
POST /{id}/run);--timeout <s>onrunbounds that submission request (default300), not the job's total runtime. - To watch what a job did to a worker — or debug one that never starts — see Debug one serverless worker through its logs.