Repository navigation
Expand file tree
/
Copy pathdefault.go
More file actions
116 lines (101 loc) · 2.69 KB
/
Copy pathdefault.go
File metadata and controls
116 lines (101 loc) · 2.69 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gush
import (
"context"
"sync"
)
var (
defaultClientMu sync.Mutex
defaultClient *Client
)
// DefaultClient returns the process-wide client behind the package-level functions,
// connecting on first use with the settings Dial reads from the environment. A failed
// connection is not remembered, so the next call tries again.
func DefaultClient() (*Client, error) {
defaultClientMu.Lock()
defer defaultClientMu.Unlock()
if defaultClient != nil {
return defaultClient, nil
}
var options Options
connected, err := Dial(options)
if err != nil {
return nil, err
}
defaultClient = connected
return defaultClient, nil
}
// Serve calls Serve on the default client.
func Serve(workers ...*Worker) error {
c, err := DefaultClient()
if err != nil {
return err
}
return c.Serve(workers...)
}
// RunFlow calls RunFlow on the default client.
func RunFlow(ctx context.Context, request FlowRequest, options ...FlowOption) error {
c, err := DefaultClient()
if err != nil {
return err
}
return c.RunFlow(ctx, request, options...)
}
// StartFlow calls StartFlow on the default client.
func StartFlow(ctx context.Context, request FlowRequest, options ...FlowOption) (string, error) {
c, err := DefaultClient()
if err != nil {
return "", err
}
return c.StartFlow(ctx, request, options...)
}
// CancelFlow calls CancelFlow on the default client.
func CancelFlow(ctx context.Context, flowID string) error {
c, err := DefaultClient()
if err != nil {
return err
}
return c.CancelFlow(ctx, flowID)
}
// TerminateFlow calls TerminateFlow on the default client.
func TerminateFlow(ctx context.Context, flowID, reason string) error {
c, err := DefaultClient()
if err != nil {
return err
}
return c.TerminateFlow(ctx, flowID, reason)
}
// GetFlow calls GetFlow on the default client.
func GetFlow(ctx context.Context, flowID string) (*FlowInfo, error) {
c, err := DefaultClient()
if err != nil {
return nil, err
}
return c.GetFlow(ctx, flowID)
}
// Resume calls Resume on the default client.
func Resume(ctx context.Context, resumeToken string, result any, stepErr error) error {
c, err := DefaultClient()
if err != nil {
return err
}
return c.Resume(ctx, resumeToken, result, stepErr)
}
// GetQueueStats calls GetQueueStats on the default client.
func GetQueueStats(ctx context.Context, queue string) (map[QueueType]*QueueStats, error) {
c, err := DefaultClient()
if err != nil {
return nil, err
}
return c.GetQueueStats(ctx, queue)
}
// Close closes the default client if it was connected. A later package-level call
// connects again.
func Close() {
defaultClientMu.Lock()
defer defaultClientMu.Unlock()
if defaultClient == nil {
return
}
defaultClient.Close()
defaultClient = nil
}