工作流状态
running() 观察句柄已加载的状态。观察另一个队列 Worker 正在执行的工作流前,应调用 refresh()。请使用有时限的等待,并在两次读取之间暂停:
$deadline = hrtime(true) + 20_000_000_000;
do {
$workflow->refresh();
if (! $workflow->running()) {
break;
}
usleep(100_000);
} while (hrtime(true) < $deadline);
if ($workflow->running()) {
throw new RuntimeException('Workflow is still running; observation deadline expired.');
}
if (! $workflow->completed()) {
throw new RuntimeException('Workflow ended with status '.$workflow->status());
}
$result = $workflow->output();
观察超时只会停止当前调用方的等待,不会取消工作流。保留工作流 ID,以便其他观察者加载当前状态。读取成功输出之前,必须处理终态失败、取消或终止。
通过 Workflow\V2\WorkflowStub::load($workflowId) 获取的实例句柄在刷新时跟随当前运行,包括跨越 continueAsNew()。通过 loadRun() 或 loadSelection() 获取的句柄持续观察所选运行,其完成可能意味着已继续到后继运行。运行选择和关闭详情见工作流 API。
状态值
status() 方法返回字符串状态:
reserved
pending
running
waiting
cancelled
terminated
completed
failed
reserved表示已创建实例 ID,但首个启动命令尚未被接受pending表示运行存在,且有工作可供认领running表示某个工作流任务正由 Worker 持有有效租约waiting表示运行正在等待活动、定时器或具名信号等持久恢复来源cancelled表示已接受的引擎级取消命令关闭了当前运行terminated表示已接受的引擎级终止命令强制关闭了当前运行completed、failed、cancelled和terminated为运行终态
运行使用 continueAsNew() 时,旧运行以 status = completed 和 closed_reason = continued 结束。Waterline 将该运行保留在已完成分类中,同时展示准确的 closed_reason,让运维人员知道实例已推进到新运行。
running() 对 pending、running 和 waiting 返回 true。
状态机
以下为工作流状态机。