@chronos.sh/sdk

中文文档
0.5.2发布于 2026/11/08MITTypeScriptESM + CJS零依赖近 30 天下载 3945

Chronos JavaScript/TypeScript SDK

原始使用文档
文档目录9 节
  1. Install
  2. Quickstart
  3. Create schedules and jobs
  4. Monitor a job that runs elsewhere
  5. Check the API key
  6. Wait for a job
  7. Graceful shutdown
  8. Documentation
  9. License

@chronos.sh/sdk

Official Node.js SDK for Chronos — reliable job scheduling as a service.

import { Chronos } from '@chronos.sh/sdk';

const chronos = new Chronos({ apiKey: process.env.CHRONOS_API_KEY! });

chronos.worker.handle('send-email', async (ctx) => {
  await sendEmail(ctx.payload);
  return { sent: true };
});

await chronos.worker.start();

Zero dependencies. Runs anywhere with fetch — Node.js 20+, Bun, Deno.

Install

npm install @chronos.sh/sdk

Quickstart

import { Chronos } from '@chronos.sh/sdk';

const chronos = new Chronos({ apiKey: process.env.CHRONOS_API_KEY! });

chronos.worker.handle<{ to: string }>('send-email', async (ctx) => {
  await sendEmail(ctx.payload.to);
  return { sent: true };
});

await chronos.worker.start();

worker.start() long-polls for jobs and dispatches them to registered handlers. It runs until you call worker.stop(). If any request gets a 401, the worker stops and the returned promise rejects with ChronosAuthenticationError, so a misconfigured or revoked key surfaces immediately instead of retrying forever. A restricted key needs the workers:run scope to run a worker. Without it, the worker stops and the returned promise rejects with a ChronosApiError whose code is insufficient_scope. Worker routes accept API keys only. Passing another credential as apiKey, such as an OAuth token, stops the worker the same way, with code set to credential_not_accepted.

Create schedules and jobs

The same client manages schedules, jobs, executions and monitors, and reads the API key's workspace:

await chronos.schedules.create({
  name: 'Nightly report',
  handler: 'send-report',
  cron: '0 2 * * *',
  timezone: 'Europe/London',
});

await chronos.jobs.create({
  name: 'Welcome email',
  handler: 'send-email',
  delay: { value: 10, unit: 'minute' },
  payload: { to: '[email protected]' },
});

for await (const job of chronos.jobs.iterate({ status: 'failed' })) {
  console.log(job.name, job.last_error);
}

Fields use the API's snake_case names. list() returns one page as { data, paging }, and iterate() walks every page. Reads retry on network errors, timeouts, 429 and 5xx responses. Writes retry only on 429, so a lost response never sends a create twice. A response body larger than 64 MiB is never retried. Set maxRequestRetries to change the retry count.

Monitor a job that runs elsewhere

A monitor watches a job that runs outside Chronos, such as a cron job on your own server. Create one and give its ping_url to the job:

const monitor = await chronos.monitors.create({
  name: 'Nightly backup',
  cron: '0 2 * * *',
});

monitor.ping_url is a secret. Keep it out of logs, and store it where the job reads its other credentials.

The job reports with one HTTP request and needs no SDK: curl -fsS "$PING_URL" on success, or curl -fsS "$PING_URL/fail" on failure. listPings() and iteratePings() read what it reported.

Chronos alerts when the job reports a failure, or when no success or fail ping arrives within grace_seconds (default 300) of when it was due.

Check the API key

To check an API key at startup, call account.get(). It returns the workspace the key belongs to, and throws ChronosAuthenticationError if the key is unknown or revoked:

const workspace = await chronos.account.get();
console.log(`Connected to ${workspace.name}`);

Wait for a job

To block until a job finishes, call jobs.wait(). It waits a second between checks and returns the job once it is completed, failed, cancelled or skipped. A failed job is returned, not thrown. After 60 seconds it throws ChronosWaitTimeoutError, whose job holds the last state seen. It rethrows any other error from a check, such as ChronosApiError for an unknown id. To change either default, set pollIntervalMs or maxWaitMs:

const created = await chronos.jobs.create({ name: 'Welcome email', handler: 'send-email' });
const job = await chronos.jobs.wait(created.id, { maxWaitMs: 120_000 });
if (job.status === 'failed') {
  console.error(job.last_error);
}

A pull job finishes only while a worker with its handler is running.

Graceful shutdown

The SDK does not install signal handlers — your application owns its process lifecycle. Wire SIGTERM/SIGINT to worker.stop() so in-flight jobs and their result reports finish before the process exits:

let stopping = false;
for (const sig of ['SIGTERM', 'SIGINT'] as const) {
  process.on(sig, () => {
    if (stopping) process.exit(1);
    stopping = true;
    chronos.worker.stop().then(() => process.exit(143));
  });
}

A second signal force-exits as an escape hatch. This matters for any long-running worker: standalone scripts, Docker containers, systemd services, PM2, cloud VMs, or CI runners.

Documentation

Chronos documentation

License

MIT

版本与发布记录

@chronos.sh/sdk 共发布 46 个版本,首次发布于 2026/05/06;过去 12 个月发布了 46 个版本。

版本发布日期距今
0.5.2latest2026/11/081 天前
0.5.3-canary.4eee61ccanary2026/11/081 天前
0.5.2-canary.cf9612b2026/11/081 天前
0.5.12026/11/072 天前
0.5.2-canary.80ac4fb2026/11/072 天前
0.5.1-canary.fe8dfac2026/11/072 天前

关于 @chronos.sh/sdk 的常见问题

@chronos.sh/sdk 是什么?

Chronos JavaScript/TypeScript SDK

如何安装 @chronos.sh/sdk?

在项目目录运行 npm install @chronos.sh/sdk,或使用 pnpm add / yarn add / bun add。

@chronos.sh/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chronos.sh/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chronos.sh/sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chronos.sh/sdk 可以商用吗?

@chronos.sh/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chronos.sh/sdk 的下载量有多少?

根据 npm 官方下载统计,@chronos.sh/sdk 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,945 次,日均约 132 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chronos.sh/sdk 还在维护吗?

最新版本 0.5.2 发布于 2026/11/08(1 天前);至今共发布 46 个版本,其中过去 12 个月发布了 46 个;首次发布于 2026/05/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。