This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
85 lines (72 loc) · 2.52 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
package main
import (
"context"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/kelseyhightower/envconfig"
keptnapi "github.com/keptn/go-utils/pkg/api/utils"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
logger "github.com/sirupsen/logrus"
"os"
"os/signal"
"sync"
"syscall"
)
// JMeterServiceName is the name of the JMeter Keptn Service
const JMeterServiceName = "jmeter-service"
type envConfig struct {
// Port on which to listen for cloudevents
Port int `envconfig:"RCV_PORT" default:"8080"`
Path string `envconfig:"RCV_PATH" default:"/"`
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
}
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
logger.Fatalf("Failed to process env var: %s", err)
}
logger.SetLevel(logger.InfoLevel)
if os.Getenv(env.LogLevel) != "" {
logLevel, err := logger.ParseLevel(os.Getenv(env.LogLevel))
if err != nil {
logger.WithError(err).Error("could not parse log level provided by 'LOG_LEVEL' env var")
} else {
logger.SetLevel(logLevel)
}
}
ctx := getGracefulContext()
p, err := cloudevents.NewHTTP(cloudevents.WithPath(env.Path), cloudevents.WithPort(env.Port), cloudevents.WithGetHandlerFunc(keptnapi.HealthEndpointHandler))
if err != nil {
logger.Fatalf("Failed to create cloud event client: %v", err)
}
c, err := cloudevents.NewClient(p)
if err != nil {
logger.Fatalf("Failed to create cloud event client: %v", err)
}
eventSender, err := keptnv2.NewHTTPEventSender("")
if err != nil {
logger.Fatalf("Failed to create event sender: %v", err)
}
eventHandler := &EventHandler{testRunner: NewTestRunner(eventSender)}
logger.Fatal(c.StartReceiver(ctx, eventHandler.handleEvent))
}
//getGracefulContext returns a context with cancel and a waitgroup to sync handlers before shutdown
func getGracefulContext() context.Context {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
wg := &sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), gracefulShutdownKey, wg))
ctx = cloudevents.WithEncodingStructured(ctx)
ctx = context.WithValue(ctx, testRunnerQuit, ch)
go func() {
<-ch
// In case of SIGINT or SIGTERM Jmeter needs to stop and send an error event
// a quit channel is preferred to ctx.Done to avoid context being closed
// too early by the cloudevents StartReceiver
close(ch)
logger.Fatal("Container termination triggered, starting graceful shutdown")
wg.Wait()
logger.Fatal("cancelling context")
cancel()
}()
return ctx
}