mcpField
S
serverpod-scheduling

serverpod-scheduling

github.com/serverpod开发3.3k44 次安装1 天前更新
代码仓库

关于 serverpod-scheduling

用于在 Serverpod 中安排数据库支持的延迟调用或指定 UTC 时间调用的技能,涵盖类型安全 API 生成及集群执行配置。

主要功能

  1. 01延迟调用与定时调用
  2. 02数据库持久化并跨重启保留
  3. 03集群范围内单次执行认领
  4. 04生成类型安全 API
  5. 05执行与并发配置

适用场景

  1. 01延迟执行任务
  2. 02安排 UTC 时间任务
  3. 03重启后保留计划任务

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/serverpod/serverpod --skill serverpod-scheduling

安装后在 Claude Code 中输入/serverpod-scheduling确认已加载。

详细介绍

Serverpod 定时调度(未来调用)

未来调用会在指定时间运行,存储在数据库中,并在重启后保留。集群中的每次调用只会被认领并执行一次。未来调用需要数据库支持;执行由单体/维护角色启动,而非无服务器角色。

定义未来调用

class ExampleFutureCall extends FutureCall {
  Future<void> doWork(Session session) async {
    // Work here.
  }

  Future<void> doOtherWork(Session session, MyModel data) async {
    // Work with data.
  }
}

运行 serverpod generate,以在 pod.futureCalls 上使用类型安全的 API。

支持的参数类型

与端点方法相同,详见 Serverpod 端点。

调度

// Delay
await pod.futureCalls
    .callWithDelay(const Duration(hours: 1))
    .example.doWork();

// At specific time (UTC)
await pod.futureCalls
    .callAtTime(DateTime.utc(2026, 1, 1))
    .example.doOtherWork(myModel);

// With identifier (for cancellation)
await pod.futureCalls
    .callWithDelay(const Duration(hours: 1), identifier: 'my-job-id')
    .example.doWork();

// Recurring task from `Duration` interval with an optional start time
await pod.futureCalls
    .callRecurring()
    .every(const Duration(hours: 1), start: DateTime.now())
    .example.doWork();

// Recurring task from `cron` expression
await pod.futureCalls
    .callRecurring()
    .cron("0 * * * *")
    .example.doWork();

await pod.futureCalls.cancel('my-job-id');  // Cancels all with that identifier

在调用内部处理失败;如果工作最终必须成功,请重新调度。

配置

futureCall:
  enabled: true                   # SERVERPOD_FUTURE_CALL_ENABLED (default true)
  executionEnabled: true          # SERVERPOD_FUTURE_CALL_EXECUTION_ENABLED
  concurrencyLimit: 5             # SERVERPOD_FUTURE_CALL_CONCURRENCY_LIMIT (default 1, <1 maps to unlimited and is not recommended)
  scanInterval: 2000              # SERVERPOD_FUTURE_CALL_SCAN_INTERVAL (ms, default 5000)

futureCallExecutionEnabled: false 仅会停止执行;仍可调度调用。futureCall.enabled: false 会完全禁用未来调用:不会创建未来调用管理器,且 serverpod generate 会省略 pod.futureCalls getter。所有运行模式配置文件中的值必须一致,否则生成会失败。

保持未来调用的幂等性。调用应能容忍重试或重启,而不会重复产生不可逆的副作用。

评论

支持 Markdown