This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselectWorker.go
130 lines (116 loc) · 3.47 KB
/
selectWorker.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
// This file is part of ezBastion.
// ezBastion is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// ezBastion is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with ezBastion. If not, see <https://www.gnu.org/licenses/>.
package middleware
import (
"errors"
"math/rand"
"net/http"
"sort"
"time"
"github.com/ezbastion/ezb_srv/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func SelectWorker(c *gin.Context) {
tr, _ := c.Get("trace")
trace := tr.(models.EzbLogs)
logg := log.WithFields(log.Fields{
"middleware": "SelectWorker",
"xtrack": trace.Xtrack,
})
logg.Debug("start")
routeType, _ := c.MustGet("routeType").(string)
// routeType := rt.(string)
if routeType == "worker" {
ac, _ := c.Get("action")
action := ac.(models.EzbActions)
workers := action.Workers
nbW := len(workers)
if nbW == 0 {
logg.Error("NO WORKER FOUND FOR ", action.Name)
c.AbortWithError(http.StatusServiceUnavailable, errors.New("#W0001"))
return
}
if nbW == 1 {
worker := workers[0]
if !worker.Enable {
logg.Error("ONLY ONE DISABLE WORKER ", worker.Name, " FOUND FOR ", action.Name)
c.AbortWithError(http.StatusServiceUnavailable, errors.New("#W0002"))
return
}
logg.Debug("one worker found: ", worker.Name, " (", worker.Comment, ")")
c.Set("worker", worker)
// c.Set("action", nil)
trace.Worker = worker.Name
c.Set("trace", trace)
}
if nbW > 1 {
var enableWorkers []models.EzbWorkers
var keys []int
for _, w := range workers {
if w.Enable {
if checksumISok(w.Fqdn, action.Jobs.Path, action.Jobs.Checksum) {
enableWorkers = append(enableWorkers, w)
keys = append(keys, len(enableWorkers))
}
}
}
if len(enableWorkers) == 0 {
logg.Error("ALL WORKERS ARE DISABLE FOR ", action.Name)
c.AbortWithError(http.StatusServiceUnavailable, errors.New("#W0003"))
return
}
// switch on config worker algo
worker := enableWorkers[randomWorker(&enableWorkers)]
logg.Debug("found ", len(enableWorkers), " worker, select ", worker.Name, " random")
c.Set("worker", worker)
c.Set("action", nil)
trace.Worker = worker.Name
c.Set("trace", trace)
// tool.Trace(&trace, c)
c.Next()
}
}
c.Next()
}
func orderBYjobs(keys *[]int, workers *[]models.EzbWorkers) {
var nbj []int
for _, _ = range *workers {
var j int // w.fqdn /healthcheck/jobs -> j int goroutine
nbj = append(nbj, j)
}
sort.Ints(nbj)
keys = &nbj
}
func orderBYload(keys *[]int, workers *[]models.EzbWorkers) {
sort.Ints(*keys)
}
func randomWorker(workers *[]models.EzbWorkers) int {
return rand.Intn(len(*workers) - 1)
}
func orderBYroundrobin(keys *[]int, workers *[]models.EzbWorkers) {
sort.Ints(*keys)
}
func checksumISok(fqdn, path, checksum string) bool {
/*
connect to fqdn /healthcheck/scripts (cache)
type wksScript struct {
Name string `json:name`
Path string `json:path`
Checksum string `json:checksum`
}
*/
return true
}