|
1 | 1 | import type { Backend } from "./backend.js"; |
2 | 2 | import { type BackoffPolicy, computeBackoffDelayMs } from "./core/backoff.js"; |
| 3 | +import { parseDuration } from "./core/duration.js"; |
3 | 4 | import type { WorkflowRun } from "./core/workflow.js"; |
4 | 5 | import { executeWorkflow } from "./execution.js"; |
5 | 6 | import { WorkflowRegistry } from "./registry.js"; |
@@ -325,5 +326,39 @@ export function resolveRetryPolicy( |
325 | 326 | partial?: Partial<RetryPolicy>, |
326 | 327 | ): RetryPolicy { |
327 | 328 | if (!partial) return DEFAULT_WORKFLOW_RETRY_POLICY; |
328 | | - return { ...DEFAULT_WORKFLOW_RETRY_POLICY, ...partial }; |
| 329 | + |
| 330 | + const merged = { ...DEFAULT_WORKFLOW_RETRY_POLICY, ...partial }; |
| 331 | + return { |
| 332 | + initialInterval: resolveDuration( |
| 333 | + merged.initialInterval, |
| 334 | + DEFAULT_WORKFLOW_RETRY_POLICY.initialInterval, |
| 335 | + ), |
| 336 | + backoffCoefficient: |
| 337 | + Number.isFinite(merged.backoffCoefficient) && |
| 338 | + merged.backoffCoefficient > 0 |
| 339 | + ? merged.backoffCoefficient |
| 340 | + : DEFAULT_WORKFLOW_RETRY_POLICY.backoffCoefficient, |
| 341 | + maximumInterval: resolveDuration( |
| 342 | + merged.maximumInterval, |
| 343 | + DEFAULT_WORKFLOW_RETRY_POLICY.maximumInterval, |
| 344 | + ), |
| 345 | + maximumAttempts: |
| 346 | + Number.isInteger(merged.maximumAttempts) && merged.maximumAttempts >= 0 |
| 347 | + ? merged.maximumAttempts |
| 348 | + : DEFAULT_WORKFLOW_RETRY_POLICY.maximumAttempts, |
| 349 | + }; |
| 350 | +} |
| 351 | + |
| 352 | +/** |
| 353 | + * Return a duration string when it parses to a positive value, otherwise fallback. |
| 354 | + * @param value - Duration string to validate |
| 355 | + * @param fallback - Default duration string to use when invalid |
| 356 | + * @returns Valid duration string |
| 357 | + */ |
| 358 | +function resolveDuration( |
| 359 | + value: RetryPolicy["initialInterval"], |
| 360 | + fallback: RetryPolicy["initialInterval"], |
| 361 | +): RetryPolicy["initialInterval"] { |
| 362 | + const parsed = parseDuration(value); |
| 363 | + return parsed.ok && parsed.value > 0 ? value : fallback; |
329 | 364 | } |
0 commit comments