SkybuDocs

Ferdigheter · Apper

Workers and scheduled jobs

Run code with no web address, all the time (a worker) or on a schedule (a job), with the app's variables and database.

skill://apps/workers-and-jobsAgenter leser den med resources/read eller skills_read. Den er skrevet på engelsk, for agentene.

Not all code answers web requests. A worker runs all the time with no address: a queue consumer, a bot, a process that polls. A scheduled job runs a command at set times: a report every morning, a clean-up at night, a reminder. Both run in their own virtual machine with the app's variables and database, like the app itself.

In short

  1. Something that runs all the time: deploy the code as its own app with kind: "worker", and command when it starts differently from the web app.
  2. Something that runs at set times: create_job on the app whose code it is, with five cron fields in Norwegian time and the command.
  3. Follow it with get_app_logs (source: "job" or jobId), list_job_runs and list_app_events.

Workers

A worker is an app of its own. Deploy the same code as the web app under another name, with the kind and the command that starts it:

{
  "name": "deploy_files",
  "arguments": {
    "projectId": "<project id>",
    "name": "queue",
    "kind": "worker",
    "command": "node worker.js",
    "dataClass": "internal",
    "files": [{ "path": "worker.js", "content": "<the file>" }, { "path": "package.json", "content": "<the file>" }]
  }
}

For an app that exists, set_app_hosting with kind: "worker" and command applies from the next deploy.

  • It gets no address, no route, no domains and no previews. It runs one process; when the process exits, Kubernetes starts it again and list_app_events records the crash.
  • A new version goes live once its process has kept running for 10 seconds; until then the old one runs. Old and new run side by side for a moment, so a queue consumer must tolerate a second consumer briefly.
  • It gets the project's database, files and variables like any app in the project, and is not archived for being idle: it never gets web requests.
  • command runs with /bin/sh -c in the built image. Leave it out when the image's own start command is right.

Scheduled jobs

A job belongs to a server or worker app and runs from that app's live version: its image, its variables, its database. A static site cannot have jobs (job_needs_server).

{ "name": "create_job", "arguments": { "appId": "<app id>", "name": "nightly-report", "schedule": "0 3 * * *", "command": "node scripts/report.js" } }
FieldRule
name2–31 characters: a lowercase letter, then lowercase letters, digits and hyphens; unique in the app.
scheduleFive cron fields: minute, hour, day of month, month, day of week, with numbers, *, ranges (1-5), lists (1,15) and steps (*/15). Norwegian time (Europe/Oslo), summer time included. At most 12 runs an hour (job_too_often).
commandUp to 2000 characters, run with /bin/sh -c.
pausedtrue keeps it from running on its schedule; it can still be run by hand.

Examples: 0 3 * * * every night at 03:00, 30 7 * * 1-5 at 07:30 on weekdays, */15 * * * * every quarter hour, 0 9 1 * * at 09:00 on the first of the month.

  • A run may take an hour; then it is stopped. Two runs of the same job never overlap: a run that is due while the previous one still runs is skipped.
  • Each run gets the memory of the app's size and counts against the project's limits while it runs.
  • An app has at most 10 jobs. Jobs run once the app has a live version, and not while the app is archived or the organisation suspended.
  • Exit with 0 for success. Anything else marks the run failed with its exit code.

Run, follow and change

{ "name": "run_job", "arguments": { "jobId": "<job id>" } }
{ "name": "list_job_runs", "arguments": { "jobId": "<job id>" } }
{ "name": "get_app_logs", "arguments": { "appId": "<app id>", "jobId": "<job id>", "since": "1d" } }

run_job starts it now, outside its schedule (job_running while a run is in progress; app_not_running before the app has a live version). list_job_runs gives each run's status (queued, running, succeeded, failed), exitCode and reason (DeadlineExceeded after an hour, OOMKilled when it ran out of memory). list_jobs shows each job's next run and how its last run went. update_job changes the name, schedule or command, or pauses it (paused: true). Agents cannot delete a job: pause it, and the user deletes it in the dashboard or with skybu job delete. Every change and every run by hand is in the audit log.

Things to know

  • Make jobs safe to run twice and safe to skip: a run can be skipped while the app is redeployed or the server restarts, and a person can run one by hand.
  • A job's output belongs in the log; send the results somewhere people read them (email, a table the app shows).
  • For work triggered by a request that takes long, put it on a queue in the database and let a worker take it, rather than a job that runs every few minutes.