-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.go
187 lines (153 loc) · 5.01 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2022 Camunda Services GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"embed"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/camunda-community-hub/zeebe-client-go/v8/pkg/entities"
"github.com/camunda-community-hub/zeebe-client-go/v8/pkg/pb"
"github.com/camunda-community-hub/zeebe-client-go/v8/pkg/worker"
"github.com/camunda-community-hub/zeebe-client-go/v8/pkg/zbc"
)
//go:embed resources
var res embed.FS
const (
kMessageVariable = "message_content"
kJobType = "email"
kWorkerName = "go-get-started"
)
func main() {
shutdownBarrier := make(chan bool, 1)
SetupShutdownBarrier(shutdownBarrier)
client := MustCreateClient()
defer MustCloseClient(client)
MustDeployProcessDefinition(client)
w := MustStartWorker(client)
defer w.Close()
MustStartProcessInstance(client, "Hello from the Go get started")
log.Printf("you can now start sending emails. log into tasklist to start doing so")
log.Printf("use ctrl+c to exit or interrupt the application")
<-shutdownBarrier
}
func SetupShutdownBarrier(done chan bool) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
done <- true
}()
}
func MustReadFile(resourceFile string) []byte {
contents, err := res.ReadFile("resources/" + resourceFile)
if err != nil {
panic(err)
}
return contents
}
func MustCreateClient() zbc.Client {
credentials, err := zbc.NewOAuthCredentialsProvider(&zbc.OAuthProviderConfig{
ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
AuthorizationServerURL: "https://login.cloud.camunda.io/oauth/token",
Audience: "zeebe.camunda.io",
})
if err != nil {
panic(err)
}
config := zbc.ClientConfig{
GatewayAddress: "YOUR_CLUSTER_ID.bru-2.zeebe.camunda.io:26500",
CredentialsProvider: credentials,
}
client, err := zbc.NewClient(&config)
if err != nil {
panic(err)
}
return client
}
func MustCloseClient(client zbc.Client) {
log.Println("closing client")
_ = client.Close()
}
func MustDeployProcessDefinition(client zbc.Client) *pb.ProcessMetadata {
definition := MustReadFile("send-email.bpmn")
command := client.NewDeployResourceCommand().AddResource(definition, "send-email.bpmn")
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
resource, err := command.Send(ctx)
if err != nil {
panic(err)
}
if len(resource.GetDeployments()) < 0 {
panic(errors.New("failed to deploy send-email model; nothing was deployed"))
}
deployment := resource.GetDeployments()[0]
process := deployment.GetProcess()
if process == nil {
panic(errors.New("failed to deploy send-email process; the deployment was successful, but no process was returned"))
}
log.Printf("deployed BPMN process [%s] with key [%d]", process.GetBpmnProcessId(), process.GetProcessDefinitionKey())
return process
}
func MustStartProcessInstance(client zbc.Client, message string) *pb.CreateProcessInstanceResponse {
command, err := client.NewCreateInstanceCommand().
BPMNProcessId("send-email").
LatestVersion().
VariablesFromMap(map[string]interface{}{kMessageVariable: message})
if err != nil {
panic(fmt.Errorf("failed to create process instance command for message [%s]", message))
}
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
process, err := command.Send(ctx)
if err != nil {
panic(err)
}
log.Printf("started process instance [%d] with {\"%s\": \"%s\"}", process.GetProcessInstanceKey(), kMessageVariable, message)
return process
}
func MustStartWorker(client zbc.Client) worker.JobWorker {
w := client.NewJobWorker().
JobType(kJobType).
Handler(HandleJob).
Concurrency(1).
MaxJobsActive(10).
RequestTimeout(1 * time.Second).
PollInterval(1 * time.Second).
Name(kWorkerName).
Open()
log.Printf("started worker [%s] for jobs of type [%s]", kWorkerName, kJobType)
return w
}
func HandleJob(client worker.JobClient, job entities.Job) {
vars, err := job.GetVariablesAsMap()
if err != nil {
log.Printf("failed to get variables for job %d: [%s]", job.Key, err)
return
}
log.Printf("Sending email with message content: %s", vars[kMessageVariable])
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
_, err = client.NewCompleteJobCommand().JobKey(job.Key).Send(ctx)
if err != nil {
log.Printf("failed to complete job with key %d: [%s]", job.Key, err)
}
log.Printf("completed job %d successfully", job.Key)
}