-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
129 lines (113 loc) · 3.6 KB
/
main.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"log"
"os"
"time"
mutexv1 "github.com/cludden/protoc-gen-go-temporal/gen/example/mutex/v1"
"github.com/cludden/protoc-gen-go-temporal/gen/example/mutex/v1/mutexv1xns"
"github.com/google/uuid"
"github.com/urfave/cli/v2"
"go.temporal.io/sdk/client"
tlog "go.temporal.io/sdk/log"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
"google.golang.org/protobuf/types/known/durationpb"
)
type Workflows struct{}
type MutexWorkflow struct {
*mutexv1.MutexWorkflowInput
log tlog.Logger
}
func (w *Workflows) Mutex(ctx workflow.Context, input *mutexv1.MutexWorkflowInput) (mutexv1.MutexWorkflow, error) {
return &MutexWorkflow{input, workflow.GetLogger(ctx)}, nil
}
func (w *MutexWorkflow) Execute(ctx workflow.Context) error {
for {
req := w.AcquireLock.ReceiveAsync()
if req == nil {
w.log.Info("no more signals")
return nil
}
var leaseID string
if err := workflow.SideEffect(ctx, func(ctx workflow.Context) any {
return uuid.NewString()
}).Get(&leaseID); err != nil {
return fmt.Errorf("error generating lease id: %w", err)
}
if err := mutexv1.LockAcquiredExternal(ctx, req.GetWorkflowId(), "", &mutexv1.LockAcquiredInput{
LeaseId: leaseID,
}); err != nil {
return fmt.Errorf("error signaling lock acquired: %w", err)
}
timerCtx, cancelTimer := workflow.WithCancel(ctx)
for done := false; !done; {
workflow.NewSelector(ctx).
AddFuture(workflow.NewTimer(timerCtx, req.GetTimeout().AsDuration()), func(workflow.Future) {
w.log.Info("lease expired")
done = true
}).
AddReceive(w.ReleaseLock.Channel, func(workflow.ReceiveChannel, bool) {
if release := w.ReleaseLock.ReceiveAsync(); release.GetLeaseId() == leaseID {
cancelTimer()
done = true
}
}).
Select(ctx)
}
}
}
type SampleWorkflowWithMutexWorkflow struct {
*mutexv1.SampleWorkflowWithMutexWorkflowInput
log tlog.Logger
}
func (w *Workflows) SampleWorkflowWithMutex(ctx workflow.Context, input *mutexv1.SampleWorkflowWithMutexWorkflowInput) (mutexv1.SampleWorkflowWithMutexWorkflow, error) {
return &SampleWorkflowWithMutexWorkflow{input, workflow.GetLogger(ctx)}, nil
}
func (w *SampleWorkflowWithMutexWorkflow) Execute(ctx workflow.Context) error {
w.log.Info("started", "resourceID", w.Req.GetResourceId())
mutex, err := mutexv1xns.MutexWithAcquireLockAsync(
ctx,
&mutexv1.MutexInput{ResourceId: w.Req.GetResourceId()},
&mutexv1.AcquireLockInput{
WorkflowId: workflow.GetInfo(ctx).WorkflowExecution.ID,
Timeout: durationpb.New(time.Minute * 10),
},
mutexv1xns.NewMutexWorkflowOptions().WithDetached(true),
)
if err != nil {
return err
}
lease, _ := w.LockAcquired.Receive(ctx)
defer func() {
if err := mutex.ReleaseLock(ctx, &mutexv1.ReleaseLockInput{LeaseId: lease.GetLeaseId()}); err != nil {
w.log.Error("failed to release lock", "error", err)
}
w.log.Info("finished")
}()
w.log.Info("resource lock acquired", "leaseID", lease.GetLeaseId())
w.log.Info("critical operation started")
d := w.Req.GetSleep().AsDuration()
if d == 0 {
d = time.Second * 10
}
err = workflow.Sleep(ctx, d)
w.log.Info("critical operation finished")
return err
}
func main() {
app, err := mutexv1.NewExampleCli(
mutexv1.NewExampleCliOptions().WithWorker(func(cmd *cli.Context, c client.Client) (worker.Worker, error) {
w := worker.New(c, mutexv1.ExampleTaskQueue, worker.Options{})
mutexv1.RegisterExampleWorkflows(w, &Workflows{})
mutexv1xns.RegisterExampleActivities(w, mutexv1.NewExampleClient(c))
return w, nil
}),
)
if err != nil {
log.Fatal(err)
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}