API Maker

The framework for AI era

Your code

Schedulers

Run your code on a timetable, once per cluster, without a cron server.

A scheduler is TypeScript plus one or more intervals: every few seconds, every day at midnight, or your own cron. With several servers, one process owns each scheduler through a Redis lock, so every run happens once. If that server goes down, another one takes over.

See it live

Step through it, slow it down, or open the full canvas.

api-maker/features/scheduleLive
  • Pickup
  • Redis key
  • Run
  • Server down
Intervals
22 time zones
Owner
—one process
Runs
0logged
Timeout
10min, default

A scheduler is your code and its intervals. Write TypeScript with g, add one or more intervals and press Ctrl + S. It is compiled and saved: no cron server to set up.

How it works

  1. A scheduler is your code and its intervals.

    Write TypeScript with g, add one or more intervals and press Ctrl + S. It is compiled and saved: no cron server to set up.

  2. Presets or cron, in any time zone.

    From every 5 seconds to every year, or your own cron with seconds. Each interval has its time zone and can be switched off.

  3. Many servers, one owner.

    Every process tries to pick schedulers under a Redis lock. The first one starts the jobs and marks the scheduler running, so each run happens once in the cluster.

  4. On time, in the sandbox, logged.

    The owner runs your code in its sandbox with the scheduler timeout, 10 minutes by default, and saves the log of each run. It refreshes its Redis key every minute.

  5. A server goes down, another takes over.

    Its key is not refreshed and expires after 75 seconds. A check every minute marks the scheduler as not running, and another process picks it up, a pickup runs every 5 minutes.

  6. Save, and the next run uses the new code.

    The owner stops its jobs, a run in progress finishes, and the scheduler is picked up again 5 seconds later with the new code. No redeploy.

What you get

Presets for the usual

Every 1, 5, 10, 15 or 30 seconds, every few minutes or hours, every day, working days, every month or every year.

Cron when you need it

Write your own cron expression with seconds. A scheduler can have several intervals, each with its time zone and an on/off switch.

Once, whatever the servers

Processes pick schedulers under a Redis lock. The owner runs them and refreshes its claim every minute.

Failover built in

When the owner stops refreshing its claim, it expires and another process picks the scheduler up.

Everything g offers

Query and update any database, call external APIs, emit events and use your utility classes, like in a custom API.

Every run logged

Runs appear in the logs like API calls, with their output or error when the log profile saves them.

An example

Nightly jobs without extra infrastructure

Expire old carts, send a daily sales report by email, sync prices from a supplier API every 15 minutes. Each one is a scheduler in API Maker, versioned with the rest of your project and deployed with it.

Every night: close carts left open for a dayscheduler.ts
import * as T from 'types';async function main(g: T.IAMGlobal) {    const dayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);    const result = await g.sys.db.updateMany({        instance: 'mongodb', database: 'shop', collection: 'carts',        find: { status: 'open', updatedAt: { $lt: dayAgo } },        updateData: { status: 'expired' },    });    g.logger.log(`${result.updatedRowsCount} carts expired`);}module.exports = main;

Interval: every day, at midnight, Asia/Kolkata. Press Ctrl + S and it is scheduled.

Good to know

  • Schedulers of developer accounts never run in the background. Developers run them by hand from API Maker.
  • A run that passes schedulerTimeoutInMinutes (10 by default) is stopped. Raise it for long jobs.

Questions

What happens to a run when I save new code?

The run in progress finishes. The scheduler is then picked up again with the new code: no restart, no deployment.

Can I pause a scheduler?

Yes. Switch the scheduler off, or switch off one of its intervals.

Where does it run?

In the sandbox of the process that owns it, or on the native process when you allow it.