forked from newrelic/nri-flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
60 lines (51 loc) · 1.4 KB
/
function.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package nriflex
import (
"context"
"github.com/newrelic/nri-flex/internal/load"
"github.com/newrelic/nri-flex/internal/runtime"
"net/http"
"sync"
)
// nri-flex.main is not called by GCP, so everything has to happen here
// Singleton, we only want to set this once
var r runtime.Instance
var log = load.Logrus
// Target Function endpoint if you want to use HTTP with the Cloud Scheduler
func FlexHTTP(w http.ResponseWriter, req *http.Request) {
log.Debugf("FlexHTTP: enter")
run()
log.Debugf("FlexHTTP: exit")
}
type PubSubMessage struct {
Data []byte `json:"data"`
}
// Target Function endpoint if you want to use Pub/Sub with the Cloud Scheduler
func FlexPubSub(ctx context.Context, m PubSubMessage) error {
log.Debugf("FlexPubSub: enter")
run()
log.Debugf("FlexPubSub: exit")
return nil
}
// Do the actual, common, work here
func run() {
log.Debugf("nriflex.run: enter")
// One time only init
var once sync.Once
once.Do(func() {
log.Debugf("nriflex.run: once.Do: enter")
// Generate the Function runtime singleton
r = runtime.GetFlexRuntime()
log.Debugf("nriflex.run: once.Do: exit")
})
runtime.CommonPreInit()
err := runtime.RunFlex(r)
if err != nil {
load.Logrus.WithError(err).Fatal("flex: failed to run runtime")
}
runtime.CommonPostInit()
log.Debugf("nriflex.run: exit")
}