Cron And Intervals

作者 val-town2d3ec654b6a7無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Use when building a val that runs on a schedule — periodic jobs, recurring tasks, polling, cron jobs, monitoring, alerting. Covers the interval handler signature, cron expressions, the UTC timezone constraint, and the `lastRunAt` pattern for detecting new items since the previous run.

AI 產生的概覽

指導撰寫依 cron 排程執行的 interval val,涵蓋 UTC 時區規則與 lastRunAt 模式。

功能
此技能提供撰寫 interval 類型 val 的說明,讓它依 cron 運算式週期性執行。內容涵蓋 interval 處理函式簽章、cron 運算式必須使用 UTC 時區的限制,以及用 lastRunAt 模式只抓取上次執行後新增項目的做法。也說明如何讀取與更新 interval 設定、何時不需要範本,以及透過手動執行處理函式來驗證變更。
適用情境
適用於建立或修改用於週期性工作、輪詢、監控或告警的排程型 val。適合應依時間觸發而非由請求觸發的工作。
執行需求
不隨附指令碼,僅為說明文件。假定使用支援 interval 類型檔案的 val 平台,並具備 read_interval_settings、write_interval_settings 工具,以及用於手動驗證的 run_file 工具。

Cron / Interval Vals

Interval vals (fileType: "interval") run on a recurring schedule defined by a cron expression. Use them for polling external APIs, sending reminders, running cleanups, generating reports, or any work that should happen on a clock rather than in response to a request.

Basic handler

ts
// Learn more: https://docs.val.town/vals/cron/export default async function (interval: Interval) {  // interval.lastRunAt: Date | undefined  console.log(interval);}

The file must have an export — export default for the handler.

Timezone

Cron expressions run in UTC. Convert any human-readable schedule (e.g. "9am Eastern") to UTC before writing the cron expression. Daylight savings is not handled — pick a UTC time that's close enough year-round.

The lastRunAt pattern

interval.lastRunAt is the timestamp of the previous successful run (or undefined on the first run). Use it to fetch only items created since the last run, instead of re-scanning everything:

ts
export default async function (interval: Interval) {  const since = interval.lastRunAt ?? new Date(Date.now() - 24 * 60 * 60 * 1000);  const newItems = await fetchItemsSince(since);  for (const item of newItems) {    await handle(item);  }}

This makes the val idempotent against missed runs and avoids reprocessing.

Reading and updating the schedule

  • read_interval_settings — fetch the current cron expression and active state of an interval file.
  • write_interval_settings — change the cron expression or pause/resume an interval.

When to skip a template

For simple scheduled jobs, create a new val with a single interval-type file directly — no template needed. Templates are for more complex shapes (dashboards, AI agents, webhook + UI combos).

Verifying changes

After editing an interval val, use run_file to invoke the handler manually instead of waiting for the next scheduled run.

來源與署名

來源:val-town/plugins位於plugin/skills/cron-and-intervals提交2d3ec65

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架