/
poll_ingest.go
83 lines (65 loc) · 2.12 KB
/
poll_ingest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package activities
import (
"context"
"errors"
"time"
"github.com/cenkalti/backoff/v4"
temporalsdk_activity "go.temporal.io/sdk/activity"
"github.com/artefactual-labs/enduro/internal/pipeline"
"github.com/artefactual-labs/enduro/internal/temporal"
)
type PollIngestActivity struct {
pipelineRegistry *pipeline.Registry
}
func NewPollIngestActivity(pipelineRegistry *pipeline.Registry) *PollIngestActivity {
return &PollIngestActivity{pipelineRegistry: pipelineRegistry}
}
type PollIngestActivityParams struct {
PipelineName string
SIPID string
}
func (a *PollIngestActivity) Execute(ctx context.Context, params *PollIngestActivityParams) (time.Time, error) {
logger := temporalsdk_activity.GetLogger(ctx)
p, err := a.pipelineRegistry.ByName(params.PipelineName)
if err != nil {
return time.Time{}, temporal.NewNonRetryableError(err)
}
amc := p.Client()
deadline := defaultMaxElapsedTime
if retryDeadline := p.Config().RetryDeadline; retryDeadline != nil {
deadline = *retryDeadline
}
backoffStrategy := backoff.WithContext(backoffStrategy, ctx)
lastRetryableError := time.Time{}
err = backoff.RetryNotify(
func() (err error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
err = pipeline.IngestStatus(ctx, amc.Ingest, params.SIPID)
// Abandon when we see a non-retryable error.
if errors.Is(err, pipeline.ErrStatusNonRetryable) {
return backoff.Permanent(temporal.NewNonRetryableError(err))
}
// Looking good, keep polling.
if errors.Is(err, pipeline.ErrStatusInProgress) {
lastRetryableError = time.Time{} // Reset.
return err
}
if err != nil {
logger.Error("Failed to look up Ingest status.", "error", err)
}
// Retry unless the deadline was exceeded.
if lastRetryableError.IsZero() {
lastRetryableError = clock.Now()
} else if clock.Since(lastRetryableError) > deadline {
return backoff.Permanent(temporal.NewNonRetryableError(err))
}
return err
},
backoffStrategy,
func(err error, duration time.Duration) {
temporalsdk_activity.RecordHeartbeat(ctx, err.Error())
},
)
return clock.Now().UTC(), err
}