-
Notifications
You must be signed in to change notification settings - Fork 2
/
gosd.go
215 lines (188 loc) · 5.06 KB
/
gosd.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// To run:
// go get github.com/githubnemo/CompileDaemon
// CompileDaemon -command="./gosd"
package gosd
import "os"
import "fmt"
import "time"
import "strings"
import "strconv"
import "syscall"
import "os/signal"
type Driver interface {
Start(name, url string) string
Get() (map[string]string, error)
Delete(currentName string)
}
type ServiceCacheEntry struct {
Name string
URL string
}
type Updater struct {
Name string
State string // expired or updated
TTL time.Time
Driver Driver
ServiceCacheEntries []ServiceCacheEntry
}
type ServiceMap struct {
Index int
URLs []string
}
type Settings struct {
TryRefreshAmount int
TryFindServiceAmount int
TryFindServiceDelay time.Duration
}
var ServiceSettings Settings
var ServiceMaps map[string]ServiceMap
var ServiceUpdater Updater
var LastCronTime time.Time
func AddServiceManually(name, url string) {
serviceCacheEntry := ServiceCacheEntry{
Name: name + "-" + time.Now().Format("20060102150405.99999999"),
URL: url,
}
ServiceUpdater.ServiceCacheEntries = append(ServiceUpdater.ServiceCacheEntries, serviceCacheEntry)
recalculateServiceMaps(ServiceUpdater)
}
func UpdateByCron() {
if LastCronTime.Add(120 * time.Minute).Before(time.Now()) {
Get()
}
}
func WaitClosing() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Finishing service: " + ServiceUpdater.Name)
Finish(ServiceUpdater.Name)
os.Exit(1)
}()
}
func IterateServiceRoute(serviceBaseName string) string {
attemptsNumber := ServiceSettings.TryFindServiceAmount
for attemptsNumber > 0 {
url := getNextURLForService(serviceBaseName)
if url != "" {
return url
}
Get()
attemptsNumber--
time.Sleep(ServiceSettings.TryFindServiceDelay)
}
return ""
}
func Finish(currentName string) {
// delete
Delete(currentName)
}
func Delete(currentName string) {
ServiceUpdater.Driver.Delete(currentName)
Get()
}
func DeleteServiceWithURL(url string) {
for _,serviceCacheEntry := range ServiceUpdater.ServiceCacheEntries {
if serviceCacheEntry.URL == url {
Delete(serviceCacheEntry.Name)
}
}
}
func Start(name, url string, driver Driver) string {
// start settings
ServiceSettings.TryRefreshAmount = 3
ServiceSettings.TryFindServiceAmount = 5
ServiceSettings.TryFindServiceDelay = 3 * time.Second
if os.Getenv("gosdTryRefreshAmount") != "" {
param,_ := strconv.Atoi(os.Getenv("gosdTryRefreshAmount"))
ServiceSettings.TryRefreshAmount = param
}
if os.Getenv("gosdTryFindServiceAmount") != "" {
param,_ := strconv.Atoi(os.Getenv("gosdTryFindServiceAmount"))
ServiceSettings.TryRefreshAmount = param
}
if os.Getenv("gosdTryFindServiceDelay") != "" {
param,_ := strconv.Atoi(os.Getenv("gosdTryFindServiceDelay"))
ServiceSettings.TryFindServiceDelay = time.Duration(param) * time.Second
}
// start cron
LastCronTime = time.Now()
// start
currentName := driver.Start(name, url)
ServiceUpdater = Updater{
Name: currentName,
State: "expired",
TTL: time.Now(),
Driver: driver,
}
// force service to unregister on closing
WaitClosing()
return currentName
}
func Get() {
val := tryRefreshForNTimes(ServiceSettings.TryRefreshAmount)
ServiceUpdater.ServiceCacheEntries = []ServiceCacheEntry{}
for key,value := range val {
// populate Updater
serviceCacheEntry := ServiceCacheEntry{
Name: key,
URL: value,
}
ServiceUpdater.ServiceCacheEntries = append(ServiceUpdater.ServiceCacheEntries, serviceCacheEntry)
}
recalculateServiceMaps(ServiceUpdater)
}
func getNextURLForService(baseName string) string {
serviceMaps, exists := ServiceMaps[baseName]
if !exists {
return ""
}
if serviceMaps.Index >= len(ServiceMaps[baseName].URLs) {
serviceMaps.Index = 0
}
serviceMaps.Index++
ServiceMaps[baseName] = serviceMaps
return ServiceMaps[baseName].URLs[(serviceMaps.Index - 1)]
}
func recalculateServiceMaps(updater Updater) {
if len(updater.ServiceCacheEntries) == 0 {
return
}
serviceMaps := map[string]ServiceMap{}
for _,serviceCacheEntry := range updater.ServiceCacheEntries {
serviceMap := getServiceMap(getServiceBaseName(serviceCacheEntry.Name), serviceMaps)
serviceMap.URLs = append(serviceMap.URLs, serviceCacheEntry.URL)
serviceMaps[getServiceBaseName(serviceCacheEntry.Name)] = serviceMap
}
ServiceMaps = serviceMaps
}
func getServiceMap(baseName string, serviceMaps map[string]ServiceMap) ServiceMap {
for key,serviceMap := range serviceMaps {
if key == baseName {
return serviceMap
}
}
return ServiceMap{
Index: 0,
URLs: []string{},
}
}
func getServiceBaseName(name string) string {
if index := strings.LastIndex(name, "-"); index > -1 {
return name[0:index]
}
return name
}
func tryRefreshForNTimes(n int) map[string]string {
for n > 0 {
val, err := ServiceUpdater.Driver.Get()
if err != nil {
n--
} else {
return val
}
}
return map[string]string{}
}