Skip to Content
PlatformCron jobs

Cron jobs

Cron jobs run recurring backend work on a schedule. Starting with the JavaScript SDK version 0.5.0, you can create and manage command or HTTP cron jobs directly from your application code.

These APIs require stackmachine version 0.5.0 or later. Cron jobs created through the SDK use client.apps.cronjobs.

Features

Command and HTTP targets

Create an EXECUTE job to run a command in the app environment, or a FETCH job to call an HTTP path on the app. A job’s target kind is fixed when it is created.

Lifecycle management

Create, retrieve, update, pause, and delete API-sourced cron jobs without leaving the SDK.

Filtering and pagination

List an app’s cron jobs, filter them by EXECUTE or FETCH, sort by newest or oldest, and use the standard SDK pagination helpers.

Execution controls

Set whether a job is enabled and optionally configure retries, maximum schedule drift, and a timeout when creating or updating it.

Common workflow

  1. Choose the app and decide whether the job should execute a command or fetch an HTTP path.
  2. Create the cron job with a name, cron schedule, and exactly one target.
  3. List or retrieve jobs to inspect their current configuration.
  4. Update the schedule or enabled state, or delete an API-sourced job when it is no longer needed.

SDK examples

Create command and HTTP cron jobs

Schedule a nightly cleanup command and a five-minute HTTP health check. Pass exactly one of execute or fetch when creating each job.

cron-jobs.js
1
const cleanup = await client.apps.cronjobs.create({
2
  app: "da_XYZ",
3
  name: "cleanup",
4
  schedule: "0 2 * * *",
5
  execute: {
6
    command: "python cleanup.py --older-than '30 days'",
7
    env: { LOG_LEVEL: "info" },
8
  },
9
});
10
console.log(cleanup.id, cleanup.kind); // EXECUTE
11
 
12
const healthcheck = await client.apps.cronjobs.create({
13
  app: "da_XYZ",
14
  name: "healthcheck",
15
  schedule: "*/5 * * * *",
16
  fetch: {
17
    path: "/health",
18
    method: "GET",
19
    expectStatusCodes: [200],
20
  },
21
});
22
console.log(healthcheck.id, healthcheck.kind); // FETCH

List and retrieve cron jobs

List jobs for an app with optional kind and sort filters, then retrieve one job by ID.

cron-jobs.js
1
const cronJobs = await client.apps.cronjobs
2
  .list({
3
    app: "da_XYZ",
4
    kind: "EXECUTE",
5
    sortBy: "NEWEST",
6
    limit: 25,
7
  })
8
  .autoPagingToArray({ limit: 100 });
9
 
10
for (const job of cronJobs) {
11
  console.log(job.id, job.name, job.schedule, job.enabled);
12
}
13
 
14
const job = await client.apps.cronjobs.retrieve("cronjob_XYZ");
15
console.log(job.kind, job.target);

Update and delete a cron job

Pause a job without deleting it, change its schedule, or permanently delete an API-sourced job.

cron-jobs.js
1
const paused = await client.apps.cronjobs.update("cronjob_XYZ", {
2
  enabled: false,
3
  schedule: "0 3 * * *",
4
});
5
console.log(paused.name, paused.enabled, paused.schedule);
6
 
7
// Permanently delete an API-sourced cron job when it is no longer needed.
8
await client.apps.cronjobs.del("cronjob_XYZ");

Source: stackmachine/sdks  JavaScript examples.