Automatisch Delay 延时应用完全指南:Delay for 与 Delay until 动作的配置、原理与执行细节
Automatisch Delay 延时应用完全指南Delay for 与 Delay until 动作的配置、原理与执行细节【免费下载链接】automatischThe open source Zapier alternative. Build workflow automation without spending time and money.项目地址: https://gitcode.com/GitHub_Trending/au/automatisch本篇技术指南聚焦 Automatisch 内置的Delay延时应用讲解其两大动作 Delay for延时一段时间与 Delay until延时到指定时刻的配置方式、参数含义并结合仓库源码剖析延时动作在流程引擎中的真实执行原理后台调度而非阻塞等待。读完本文你将掌握如何在 Automatisch 工作流中精准插入延时步骤并理解其底层毫秒换算与调度机制。Delay 应用是什么无需任何连接的内置应用在 Automatisch 中Delay 是一个随产品内置built-in的应用它与 GitHub、Slack、Gmail 等需要 OAuth 凭据或 API Token 的外部应用不同Delay 不需要与任何外部服务通信因此使用它无需任何额外的连接步骤。这一点在官方文档中有明确说明Delay is a built-in app shipped with Automatisch, and it doesnt need to talk with any other external service to run. So there are no additional steps to use the Delay app. It can be used only as an action and it delays the execution of the next action by a specified amount of time. —— 见 connection.md从源码层面看这一特性体现在 Delay 应用定义 中export default defineApp({ name: Delay, key: delay, iconUrl: {BASE_URL}/apps/delay/assets/favicon.svg, authDocUrl: {DOCS_URL}/apps/delay/connection, supportsConnections: false, // 不支持连接即无需任何凭据 baseUrl: , apiBaseUrl: , primaryColor: #001F52, actions, });其中supportsConnections: false表示该应用不需要、也不支持连接Connection所以在流程编辑器中添加 Delay 动作时不会出现“选择连接”的步骤。注意Delay 只能作为动作Action使用不能作为触发器Trigger这与defineApp只声明了actions、未声明triggers的事实一致。同时Delay 应用的动作清单注册在 actions/index.js总共导出两个动作delayForDelay for与delayUntilDelay until也正是文档 actions.md frontmatter 中列出的两项动作名称key描述Delay fordelayForDelays the execution of the next action by a specified amount of time.将下一个动作的执行延时一段指定的时间Delay untildelayUntilDelays the execution of the next action until a specified date.将下一个动作的执行延时到指定的日期时刻动作一Delay for——按指定时长延时Delay for 用于将流程暂停一段相对时长例如 5 分钟、2 小时、3 天其完整定义位于 actions/delay-for/index.js。参数说明该动作包含两个必填参数参数key类型必填说明Delay for unitdelayForUnit下拉框dropdown是延时单位可选minutes分钟、hours小时、days天、weeks周Delay for valuedelayForValue字符串string是延时的数值填写数字例如1、2、3两个参数均声明了variables: true意味着它们支持变量引用即可以用上一个动作的输出动态填充数值例如根据表单提交的天数来动态延时。源码实现async run($) { const { delayForUnit, delayForValue } $.step.parameters; const dataItem { delayForUnit, delayForValue, }; $.setActionItem({ raw: dataItem }); }从run的实现可见动作本身并不执行任何“sleep”它只负责读取用户填写的delayForUnit与delayForValue将其作为dataItem通过$.setActionItem记录下来真正的延时调度发生在流程引擎层见下文“底层原理”一节。实际使用示例发送欢迎邮件 10 分钟后再发送一条使用提醒 → 单位选minutes数值填10。触发工作流后等待 1 周再做周报汇总 → 单位选weeks数值填1。结合变量使用例如delayForValue中引用前序步骤输出的{{1.record.remindDays}}即可按记录中的天数动态延时。动作二Delay until——延时到指定时刻Delay until 用于将流程延时到某个具体的日期时刻例如 2026-12-18其完整定义位于 actions/delay-until/index.js。参数说明该动作仅有一个必填参数参数key类型必填说明Delay until (Date)delayUntil字符串string是延时至的日期格式如2022-12-18同样支持变量源码中的字段描述为Delay until the date. E.g. 2022-12-18即期望一个可被 JavaScriptDate解析的日期字符串。源码实现async run($) { const { delayUntil } $.step.parameters; const dataItem { delayUntil, }; $.setActionItem({ raw: dataItem }); }与 Delay for 一样动作本身只采集参数并写入dataItem延时计算与调度交由引擎完成。底层原理延时动作如何被真正执行理解 Automatisch 的 Delay 实现关键在于延时并不是让当前进程阻塞等待而是将流程的“剩余部分”调度到后台并携带一个延时时间。这一点可以从流程引擎的步骤迭代逻辑中看到完整证据。引擎中的延时分支在 engine/steps/iterate.js 中引擎在依次处理每个动作步骤时遇到 Delay 动作会走如下分支if ( !testRun actionStep.appKey delay !workSynchronously !triggeredByMcp ) { const nextStepId await actionStep.getNextStep(); if (!nextStepId) { return; } await Engine.runInBackground({ flowId: flow.id, resumeStepId: nextStepId.id, resumeExecutionId: executionId, delay: delayAsMilliseconds(actionStep.key, computedParameters), }); return; }该分支的语义可拆解为只有非测试运行!testRun时才会真正进行后台调度测试运行Test run会直接按顺序执行不会等待延时避免测试时长时间挂起。只针对appKey delay的步骤生效。workSynchronously为真时如 Webhook、Forms 触发器开启了“同步执行”的场景不会走延时分支。通过actionStep.getNextStep()找到延时步骤的下一个步骤将其与当前executionId一并交给Engine.runInBackground并传入计算好的delay毫秒。调度完成后立即return当前一次迭代结束流程剩余步骤会在后台队列中、延时到期后从nextStepId继续执行。也就是说从运行效果上看“下一个动作被延时了”但实现上是通过任务队列后台调度 延迟投递完成的这也解释了为什么 Delay 步骤会保存执行步骤processActionStep已先行写入 execution / executionStep 数据随后再挂起调度。毫秒换算两个辅助函数Delay 的时长计算集中在 helpers/delay-as-milliseconds.js它会根据动作的eventKey分发到两个换算函数const delayAsMilliseconds (eventKey, computedParameters) { let delayDuration 0; if (eventKey delayFor) { const { delayForUnit, delayForValue } computedParameters; delayDuration delayForAsMilliseconds(delayForUnit, Number(delayForValue)); } else if (eventKey delayUntil) { const { delayUntil } computedParameters; delayDuration delayUntilAsMilliseconds(delayUntil); } return delayDuration; };Delay for 的换算定义在 helpers/delay-for-as-milliseconds.jsconst delayAsMilliseconds (delayForUnit, delayForValue) { switch (delayForUnit) { case minutes: return delayForValue * 60 * 1000; case hours: return delayForValue * 60 * 60 * 1000; case days: return delayForValue * 24 * 60 * 60 * 1000; case weeks: return delayForValue * 7 * 24 * 60 * 60 * 1000; default: return 0; } };即分钟按 60s、小时按 3600s、天按 86400s、周按 7 天换算为毫秒若单位不在枚举范围内则返回0不延时。注意这里是以整数乘法实现因此非整数数值如1.5也能得到相应的毫秒数。Delay until 的换算定义在 helpers/delay-until-as-milliseconds.jsconst delayUntilAsMilliseconds (delayUntil) { const delayUntilDate new Date(delayUntil); const now new Date(); return delayUntilDate.getTime() - now.getTime(); };即以“目标时刻 − 当前时刻”的毫秒差值作为延时。由此可以推断两个行为边界若填写的日期早于当前时间差值将为负数此时队列仍会按该值调度实际效果接近于立即或尽快继续执行日期字符串的解析遵循 JavaScript 标准Date构造规则推荐使用YYYY-MM-DD或YYYY-MM-DD HH:mm:ss这类明确的格式以保证时区与解析结果符合预期。使用场景与注意事项结合官方文档与源码归纳 Delay 的典型使用场景与使用限制典型场景发送确认邮件/通知后延时一段时间再执行后续提醒或跟进动作将定时如 Cron/Scheduler 触发的流程“错峰”执行避免高峰集中请求在需要等待外部系统数据就绪的场合延时后重试轮询类操作预约类业务在指定日期前将流程中的某个动作安排到目标时刻执行。注意事项Delay 只能用作动作不能用作触发器且不需要也无法配置连接supportsConnections: false。在**测试运行Test run**模式下延时分支会被跳过延时动作不会真正挂起后续步骤便于快速验证。当流程由 Webhook 或 Forms 触发且开启了workSynchronously同步执行时延时调度分支同样不会生效。若 Delay 是流程的最后一个步骤getNextStep()返回空引擎会直接结束不会做多余的调度。从源码结构看延时的实现依赖后台任务队列Engine.runInBackground因此延时精度与底层队列的调度机制相关适合分钟级及以上的业务场景。小结Automatisch 的 Delay 应用用两个极简的动作覆盖了延时场景的全部需求Delay for面向“相对时长”分钟/小时/天/周Delay until面向“绝对时刻”指定日期。它无需连接、配置轻量且所有参数均支持变量动态填充而在底层流程引擎通过毫秒换算 后台队列调度的方式实现“暂停”使延时动作既不阻塞进程又能保证流程从正确的位置继续执行。掌握这两个动作与上述执行边界即可在自动化流程中稳妥地插入任意时长的等待逻辑。【免费下载链接】automatischThe open source Zapier alternative. Build workflow automation without spending time and money.项目地址: https://gitcode.com/GitHub_Trending/au/automatisch创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考