forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autospotting.go
76 lines (58 loc) · 1.79 KB
/
autospotting.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
// Copyright (c) 2016-2022 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"strconv"
autospotting "github.com/AutoSpotting/AutoSpotting/core"
"github.com/aws/aws-lambda-go/lambda"
)
var as *autospotting.AutoSpotting
var conf autospotting.Config
// Version represents the build version being used
var Version = "number missing"
// SavingsCut is populated at build time and controls the percentage of the savings charged for the stable builds
var SavingsCut = "0"
var eventFile string
func main() {
eventFile = conf.EventFile
if autospotting.RunningFromLambda() {
lambda.Start(Handler)
} else if eventFile != "" {
parseEvent, err := ioutil.ReadFile(eventFile)
if err != nil {
log.Fatal(err)
}
Handler(context.TODO(), parseEvent)
} else {
eventHandler(nil)
}
}
func eventHandler(event *json.RawMessage) {
log.Println("Starting autospotting agent, build ", Version, "charging", SavingsCut, "percent of savings via AWS Marketplace")
log.Printf("Configuration flags: %#v", conf)
as.EventHandler(event)
log.Println("Execution completed, nothing left to do")
}
// this is the equivalent of a main for when running from Lambda, but on Lambda
// the runFromCronEvent() is executed within the handler function every time we have an event
func init() {
as = &autospotting.AutoSpotting{}
sc, err := strconv.ParseFloat(SavingsCut, 64)
if err != nil {
log.Printf("Failed to convert savings cut %s to float\n", SavingsCut)
}
conf = autospotting.Config{
Version: Version,
SavingsCut: sc,
}
autospotting.ParseConfig(&conf)
as.Init(&conf)
}
// Handler implements the AWS Lambda handler interface
func Handler(ctx context.Context, rawEvent json.RawMessage) {
eventHandler(&rawEvent)
}