各 SDK 的活动选项
用一种语言编写的工作流可以调用由另一种语言的 Worker 执行的活动。PHP、Rust 和 Python SDK 对部分活动选项的命名和单位不同,但每个选项设置的都是 schedule_activity 命令中的同一个字段。无论由哪个 SDK 发送,Server 都以相同方式应用该字段。
各 SDK 中的同一个活动调用
下面的每个调用都在 payments 队列上运行 payments.capture。它最多尝试 5 次,重试前分别等待 1、2、4 和 8 秒,CardDeclined 失败从不重试,并把单次尝试限制为 30 秒、整个活动限制为 10 分钟。
$receipt = $context->activity('payments.capture', [$orderId], [
'queue' => 'payments',
'start_to_close_timeout' => 30,
'schedule_to_close_timeout' => '10 minutes',
'retry_policy' => [
'max_attempts' => 5,
'backoff_seconds' => [1, 2, 4, 8],
'non_retryable_error_types' => ['CardDeclined'],
],
]);
use std::time::Duration;
use durable_workflow::{ActivityOptions, ActivityRetryPolicy};
let receipt: String = ctx
.activity_typed_with_options(
"payments.capture",
ActivityOptions::new()
.task_queue("payments")
.start_to_close_timeout(Duration::from_secs(30))
.schedule_to_close_timeout(Duration::from_secs(10 * 60))
.retry_policy(
ActivityRetryPolicy::new(5)
.backoff_intervals([1, 2, 4, 8].map(Duration::from_secs))
.non_retryable_error_type("CardDeclined"),
),
order_id,
)
.await?;
from durable_workflow import ActivityRetryPolicy
receipt = yield ctx.schedule_activity(
"payments.capture",
[order_id],
queue="payments",
start_to_close_timeout=30,
schedule_to_close_timeout=600,
retry_policy=ActivityRetryPolicy(
max_attempts=5,
backoff_seconds=[1, 2, 4, 8],
non_retryable_error_types=["CardDeclined"],
),
)
选项对照
PHP 的选项是传给 $context->activity() 的选项数组中的键。Rust 的选项是 ActivityOptions 和 ActivityRetryPolicy 的方法,传给 ctx.activity_with_options 或 ctx.activity_typed_with_options。Python 的选项是 ctx.schedule_activity() 的关键字参数,重试字段位于 ActivityRetryPolicy 上。每个选项都设置最后一列中的 schedule_activity 字段。
| 选项 | PHP | Rust | Python | Server 字段 |
|---|---|---|---|---|
| 任务队列 | 'queue' => 'payments' | .task_queue("payments") | queue="payments" | queue |
| 尝试次数(含首次) | 'retry_policy' => ['max_attempts' => 5] | ActivityRetryPolicy::new(5) | ActivityRetryPolicy(max_attempts=5) | retry_policy.max_attempts |
| 重试前的等待时间 | retry_policy 中的 'backoff_seconds' => [1, 2, 4],整数秒 | 使用 Duration 值的 .backoff_intervals([..]),或 .exponential_backoff(initial, coefficient, maximum) | backoff_seconds=[1, 2, 4],整数秒,或 ActivityRetryPolicy.exponential(..) | retry_policy.backoff_seconds |
| 不重试的失败类型 | retry_policy 中的 'non_retryable_error_types' => [..] | .non_retryable_error_types([..]) 或 .non_retryable_error_type(..) | non_retryable_error_types=[..] | retry_policy.non_retryable_error_types |
| 单次尝试从开始到结束 | 'start_to_close_timeout' => 30 | .start_to_close_timeout(Duration) | start_to_close_timeout=30 | start_to_close_timeout |
| 尝试开始前在队列中的等待 | 'schedule_to_start_timeout' => 60 | .schedule_to_start_timeout(Duration) | schedule_to_start_timeout=60 | schedule_to_start_timeout |
| 整个活动(含所有尝试) | 'schedule_to_close_timeout' => 600 | .schedule_to_close_timeout(Duration) | schedule_to_close_timeout=600 | schedule_to_close_timeout |
| 两次心跳之间的最长间隔 | 'heartbeat_timeout' => 15 | .heartbeat_timeout(Duration) | heartbeat_timeout=15 | heartbeat_timeout |
队列选项在 PHP 和 Python 中名为 queue,在 Rust 中名为 task_queue。三者设置的都是同一个 queue 字段。
单位
Server 以整数秒记录每个超时和每个重试前的等待时间。
- PHP 的超时可以是整数或浮点数秒、
'10 minutes'这样的字符串,或DateInterval、CarbonInterval。秒以外的时长形式需要 PHP SDK 3.9.0 或更高版本。每个值都向上取整为整数秒,活动超时至少为 1 秒。backoff_seconds列出整数秒。 - Rust 的超时和等待时间是
std::time::Duration值。每个值都向上取整为整数秒,因此Duration::from_millis(100)会变成 1 秒。超时必须大于零。 - Python 的超时是整数秒,至少为 1 秒。
backoff_seconds列出整数秒。ActivityRetryPolicy.exponential(..)以及initial_interval_seconds和maximum_interval_seconds字段接受小数秒,生成的每个等待时间都会向上取整为整数秒。
max_attempts 计算所有尝试,包括首次尝试。重试次数多于等待时间的个数时,最后一个等待时间会重复使用:backoff_seconds 为 [1, 2] 且尝试 5 次时,依次等待 1、2、2、2 秒。Server 在调度活动时记录该策略,即使之后工作流代码发生变化,重试仍沿用记录下来的策略。
相关页面
- PHP SDK 时长
- Rust SDK 活动重试等待时间
- Python SDK
- Worker 协议命令类型
- 注册 Laravel 桥接的处理器,让 PHP Worker 处理其他语言调用的活动