pollForTransactionStatus polls for a transaction until it reaches the expected status or a timeout occurs.
(ctx context.Context, ds database.IDataSource, transactionRef string, expectedStatus string, pollInterval time.Duration, timeoutDuration time.Duration)
| 42 | |
| 43 | // pollForTransactionStatus polls for a transaction until it reaches the expected status or a timeout occurs. |
| 44 | func pollForTransactionStatus(ctx context.Context, ds database.IDataSource, transactionRef string, expectedStatus string, pollInterval time.Duration, timeoutDuration time.Duration) (*model.Transaction, error) { |
| 45 | timeoutCtx, cancel := context.WithTimeout(ctx, timeoutDuration) |
| 46 | defer cancel() |
| 47 | |
| 48 | ticker := time.NewTicker(pollInterval) |
| 49 | defer ticker.Stop() |
| 50 | |
| 51 | for { |
| 52 | select { |
| 53 | case <-timeoutCtx.Done(): |
| 54 | return nil, fmt.Errorf("timed out waiting for transaction ref %s to reach status %s: %w", transactionRef, expectedStatus, timeoutCtx.Err()) |
| 55 | case <-ticker.C: |
| 56 | txn, err := ds.GetTransactionByRef(timeoutCtx, transactionRef) // Use timeoutCtx for the DB call as well |
| 57 | if err != nil { |
| 58 | // If the error is that the transaction is not found, we should continue polling. |
| 59 | // For other errors, the outer timeout will eventually catch persistent issues. |
| 60 | continue |
| 61 | } |
| 62 | // If err is nil, txn is a valid model.Transaction struct. |
| 63 | if txn.Status == expectedStatus { |
| 64 | return &txn, nil // Return a pointer to the transaction |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Helper for Asynq logger to use t.Logf |
| 71 | type testLogger struct { |
no test coverage detected