TryRun is the error-returning variant of Run. Synchronous polling, no internal goroutines, so it is safe to call from inside an assert.EventuallyWithT closure.
(ctx context.Context, httpClient *http.Client, httpPort int, workflowName string, input any, timeout time.Duration)
| 76 | // internal goroutines, so it is safe to call from inside an |
| 77 | // assert.EventuallyWithT closure. |
| 78 | func TryRun(ctx context.Context, httpClient *http.Client, httpPort int, workflowName string, input any, timeout time.Duration) (Status, error) { |
| 79 | var status Status |
| 80 | |
| 81 | instanceID, err := startInstance(ctx, httpClient, httpPort, workflowName, input) |
| 82 | if err != nil { |
| 83 | return status, err |
| 84 | } |
| 85 | |
| 86 | statusURL := fmt.Sprintf("http://localhost:%d/v1.0-beta1/workflows/dapr/%s", httpPort, instanceID) |
| 87 | deadline := time.Now().Add(timeout) |
| 88 | for { |
| 89 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, statusURL, nil) |
| 90 | if err != nil { |
| 91 | return status, fmt.Errorf("build status request: %w", err) |
| 92 | } |
| 93 | resp, err := httpClient.Do(req) |
| 94 | if err != nil { |
| 95 | return status, fmt.Errorf("get status: %w", err) |
| 96 | } |
| 97 | decErr := json.NewDecoder(resp.Body).Decode(&status) |
| 98 | resp.Body.Close() |
| 99 | if decErr != nil { |
| 100 | return status, fmt.Errorf("decode status: %w", decErr) |
| 101 | } |
| 102 | if IsTerminal(status.RuntimeStatus) { |
| 103 | return status, nil |
| 104 | } |
| 105 | if time.Now().After(deadline) { |
| 106 | return status, fmt.Errorf("workflow %q did not reach terminal status within %s (last: %q)", |
| 107 | workflowName, timeout, status.RuntimeStatus) |
| 108 | } |
| 109 | select { |
| 110 | case <-ctx.Done(): |
| 111 | return status, ctx.Err() |
| 112 | case <-time.After(50 * time.Millisecond): |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // startInstance POSTs to the dapr workflow start API and returns the new |
| 118 | // instance ID. |