-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
144 lines (114 loc) · 4.06 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
// Copyright (c) Rishabh Gupta 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/openfaas-incubator/connector-sdk/types"
"github.com/openfaas/faas/gateway/requests"
cfunction "github.com/zeerorg/cron-connector/types"
)
func main() {
creds := types.GetCredentials()
config, err := getControllerConfig()
if err != nil {
panic(err)
}
controller := types.NewController(creds, config)
cronScheduler := cfunction.NewScheduler()
topic := "cron-function"
interval := time.Second * 10
cronScheduler.Start()
err = startFunctionProbe(interval, topic, controller, cronScheduler, controller.Invoker)
if err != nil {
panic(err)
}
}
func getControllerConfig() (*types.ControllerConfig, error) {
gURL, ok := os.LookupEnv("gateway_url")
if !ok {
return nil, errors.New("Gateway URL not set")
}
return &types.ControllerConfig{
RebuildInterval: time.Millisecond * 1000,
GatewayURL: gURL,
PrintResponse: true,
}, nil
}
func startFunctionProbe(interval time.Duration, topic string, c *types.Controller, cronScheduler *cfunction.Scheduler, invoker *types.Invoker) error {
runningFuncs := make(cfunction.ScheduledFunctions, 0)
lookupBuilder := cfunction.FunctionLookupBuilder{
GatewayURL: c.Config.GatewayURL,
Client: types.MakeClient(c.Config.UpstreamTimeout),
Credentials: c.Credentials,
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
<-ticker.C
functions, err := lookupBuilder.GetFunctions()
if err != nil {
return errors.New(fmt.Sprint("Couldn't fetch Functions due to: ", err))
}
newCronFunctions := RequestsToCronFunctions(functions, topic)
addFuncs, deleteFuncs := GetNewAndDeleteFuncs(newCronFunctions, runningFuncs)
for _, function := range deleteFuncs {
cronScheduler.Remove(function)
log.Print("deleted function ", function.Function.Name)
}
newScheduledFuncs := make(cfunction.ScheduledFunctions, 0)
for _, function := range addFuncs {
f, err := cronScheduler.AddCronFunction(function, invoker)
if err != nil {
log.Fatal("could not add function ", function.Name)
}
newScheduledFuncs = append(newScheduledFuncs, f)
log.Print("added function ", function.Name)
}
runningFuncs = UpdateScheduledFunctions(runningFuncs, newScheduledFuncs, deleteFuncs)
}
}
// RequestsToCronFunctions converts an array of requests.Function object to CronFunction, ignoring those that cannot be converted
func RequestsToCronFunctions(functions []requests.Function, topic string) cfunction.CronFunctions {
newCronFuncs := make(cfunction.CronFunctions, 0)
for _, function := range functions {
cF, err := cfunction.ToCronFunction(function, topic)
if err != nil {
continue
}
newCronFuncs = append(newCronFuncs, cF)
}
return newCronFuncs
}
// GetNewAndDeleteFuncs takes new functions and running cron functions and returns functions that need to be added and that need to be deleted
func GetNewAndDeleteFuncs(newFuncs cfunction.CronFunctions, oldFuncs cfunction.ScheduledFunctions) (cfunction.CronFunctions, cfunction.ScheduledFunctions) {
addFuncs := make(cfunction.CronFunctions, 0)
deleteFuncs := make(cfunction.ScheduledFunctions, 0)
for _, function := range newFuncs {
if !oldFuncs.Contains(&function) {
addFuncs = append(addFuncs, function)
}
}
for _, function := range oldFuncs {
if !newFuncs.Contains(&function.Function) {
deleteFuncs = append(deleteFuncs, function)
}
}
return addFuncs, deleteFuncs
}
// UpdateScheduledFunctions updates the scheduled function with added functions and removes deleted functions
func UpdateScheduledFunctions(running, added, deleted cfunction.ScheduledFunctions) cfunction.ScheduledFunctions {
updatedSchedule := make(cfunction.ScheduledFunctions, 0)
for _, function := range running {
if !deleted.Contains(&function.Function) {
updatedSchedule = append(updatedSchedule, function)
}
}
for _, function := range added {
updatedSchedule = append(updatedSchedule, function)
}
return updatedSchedule
}