Repository navigation
Expand file tree
/
Copy pathvalidation.go
More file actions
60 lines (55 loc) · 1.97 KB
/
Copy pathvalidation.go
File metadata and controls
60 lines (55 loc) · 1.97 KB
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
package dtssample
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/microsoft/durabletask-go/api"
"github.com/microsoft/durabletask-go/durabletaskscheduler"
)
// NewInstanceID gives each sample run its own orchestration namespace.
func NewInstanceID(sample string) api.InstanceID {
return api.InstanceID("sample-" + sample + "-" + uuid.NewString())
}
// RequireCompleted rejects unsuccessful terminal states as well as missing results.
func RequireCompleted(metadata *api.OrchestrationMetadata) error {
if metadata == nil {
return errors.New("sample received no orchestration metadata")
}
if metadata.RuntimeStatus != api.RUNTIME_STATUS_COMPLETED {
return fmt.Errorf("orchestration %s ended in %s: %v",
metadata.InstanceID, metadata.RuntimeStatus, metadata.FailureDetails)
}
return nil
}
// Cleanup terminates and purges only the supplied, sample-owned instance IDs and
// their children. Call it before shutting down the worker. Entity state, schedules,
// export jobs, and storage objects need their own sample-specific cleanup.
func Cleanup(client *durabletaskscheduler.Client, instanceIDs ...api.InstanceID) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var failures []error
for _, id := range instanceIDs {
metadata, err := client.FetchOrchestrationMetadata(ctx, id, api.WithFetchPayloads(false))
if errors.Is(err, api.ErrInstanceNotFound) {
continue
}
if err == nil && metadata == nil {
err = errors.New("missing orchestration metadata")
}
if err == nil && !metadata.IsComplete() {
err = client.TerminateOrchestration(ctx, id)
if err == nil {
_, err = client.WaitForOrchestrationCompletion(ctx, id)
}
}
if err == nil {
err = client.PurgeOrchestrationState(ctx, id, api.WithRecursivePurge(true))
}
if err != nil && !errors.Is(err, api.ErrInstanceNotFound) {
failures = append(failures, fmt.Errorf("clean up %s: %w", id, err))
}
}
return errors.Join(failures...)
}