-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathmain.go
More file actions
146 lines (124 loc) · 3.36 KB
/
main.go
File metadata and controls
146 lines (124 loc) · 3.36 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"fmt"
contextawareencryption "github.com/temporalio/samples-go/context-aware-encryption"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"golang.org/x/sync/errgroup"
"log"
"os"
"os/signal"
"syscall"
"time"
)
type startable interface {
Start(context.Context) error
Shutdown(context.Context)
}
func main() {
ctx, done := context.WithCancel(context.Background())
c := contextawareencryption.MustGetDefaultTemporalClient(ctx, nil)
defer c.Close()
g, ctx := errgroup.WithContext(ctx)
// set up signal listener
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(quit)
startables := []startable{
&Worker{tclient: c},
&App{tclient: c, maxCount: len(contextawareencryption.TenantKeysByOrganization)},
}
for _, s := range startables {
var current = s
g.Go(func() error {
if err := current.Start(ctx); err != nil {
return err
}
return nil
})
}
select {
case <-quit:
break
case <-ctx.Done():
break
}
// shutdown the things
done()
// limit how long we'll wait for
timeoutCtx, timeoutCancel := context.WithTimeout(
context.Background(),
10*time.Second,
)
defer timeoutCancel()
for _, s := range startables {
s.Shutdown(timeoutCtx)
}
// wait for shutdown
if err := g.Wait(); err != nil {
panic("shutdown was not clean" + err.Error())
}
}
type Worker struct {
tclient sdkclient.Client
worker worker.Worker
}
func (w *Worker) Start(ctx context.Context) error {
w.worker = worker.New(w.tclient, "encryption", worker.Options{})
w.worker.RegisterWorkflow(contextawareencryption.TenantWorkflow)
w.worker.RegisterActivity(contextawareencryption.TenantActivity)
return w.worker.Run(worker.InterruptCh())
}
func (w *Worker) Shutdown(ctx context.Context) {
w.worker.Stop()
}
type App struct {
tclient sdkclient.Client
maxCount int
}
func (a *App) Shutdown(ctx context.Context) {
}
func (a *App) Start(ctx context.Context) error {
if a.maxCount == 0 {
return fmt.Errorf("You must at least one run Workflow")
}
dt := time.Now().UTC().String()
count := 0
for tenant, keyId := range contextawareencryption.TenantKeysByOrganization {
wid := fmt.Sprintf("tenant_%s-%s", tenant, dt)
workflowOptions := sdkclient.StartWorkflowOptions{
ID: wid,
TaskQueue: "encryption",
}
// If you are using a ContextPropagator and varying keys per workflow you need to set
// the KeyID to use for this workflow in the context:
fmt.Println(fmt.Sprintf("Setting encryption key for '%s' with value '%s'", tenant, keyId))
ctx = context.WithValue(ctx,
contextawareencryption.PropagateKey,
contextawareencryption.CryptContext{KeyID: keyId})
// The workflow input tenant will be encrypted by the DataConverter before being sent to Temporal
we, err := a.tclient.ExecuteWorkflow(
ctx,
workflowOptions,
contextawareencryption.TenantWorkflow,
"workflowargument for "+tenant,
)
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
// Synchronously wait for the workflow completion.
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable get workflow result", err)
}
log.Println("TenantWorkflow result:", result)
count++
if count >= a.maxCount {
break
}
}
return nil
}