forked from qa-dev/jsonwire-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (132 loc) · 4.43 KB
/
Copy pathmain.go
File metadata and controls
155 lines (132 loc) · 4.43 KB
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
package main
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/qa-dev/jsonwire-grid/config"
"github.com/qa-dev/jsonwire-grid/handlers"
"github.com/qa-dev/jsonwire-grid/logger"
"github.com/qa-dev/jsonwire-grid/middleware"
"github.com/qa-dev/jsonwire-grid/pool"
"github.com/qa-dev/jsonwire-grid/pool/capabilities"
poolMetrics "github.com/qa-dev/jsonwire-grid/pool/metrics"
"github.com/qa-dev/jsonwire-grid/utils/metrics"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
cfg := config.New()
err := cfg.LoadFromFile(os.Getenv("CONFIG_PATH"))
if err != nil {
log.Fatalf("Problem in loading config from file, %s", err)
}
err = logger.Init(cfg.Logger)
if err != nil {
log.Fatalf("Problem in init logger, %s", err)
}
middlewareWrap := middleware.NewWrap(log.StandardLogger())
busyNodeDuration, err := time.ParseDuration(cfg.Grid.BusyNodeDuration)
if err != nil {
log.Fatal("Invalid value grid.busy_node_duration in config")
}
reservedNodeDuration, err := time.ParseDuration(cfg.Grid.ReservedDuration)
if err != nil {
log.Fatal("Invalid value grid.reserved_node_duration in config")
}
storageFactory, err := invokeStorageFactory(*cfg)
if err != nil {
log.Fatalf("Can't invoke storage factory, %s", err)
}
storage, err := storageFactory.Create(*cfg)
if err != nil {
log.Fatalf("Can't create storage factory, %s", err)
}
capsComparator := capabilities.NewComparator()
strategyFactoryList, err := invokeStrategyFactoryList(*cfg)
if err != nil {
log.Fatalf("Can't create strategy factory list, %s", err)
}
clientFactory, err := createClient(*cfg)
if err != nil {
log.Fatalf("Create ClientFactory error, %s", err)
}
var strategyList []pool.StrategyInterface
for _, strategyFactory := range strategyFactoryList {
strategy, err := strategyFactory.Create(storage, capsComparator, clientFactory)
if err != nil {
log.Fatalf("Can't create strategy, %s", err)
}
strategyList = append(strategyList, strategy)
}
strategyListStruct := pool.NewStrategyList(strategyList)
poolInstance := pool.NewPool(storage, strategyListStruct)
poolInstance.SetBusyNodeDuration(busyNodeDuration)
poolInstance.SetReservedNodeDuration(reservedNodeDuration)
go func() {
for {
poolInstance.FixNodeStatuses()
time.Sleep(time.Minute * 5) // todo: move to config
}
}()
cache := pool.NewCache(time.Minute * 10) // todo: move to config
go func() {
for {
cache.CleanUp()
time.Sleep(time.Minute) // todo: move to config
}
}()
if cfg.Statsd != nil {
statsdClient, err := metrics.NewStatsd(
cfg.Statsd.Host,
cfg.Statsd.Port,
cfg.Statsd.Protocol,
cfg.Statsd.Prefix,
cfg.Statsd.Enable)
// todo: move to config
poolMetricsSender := poolMetrics.NewSender(statsdClient, poolInstance, time.Second*1, cfg.Statsd.CapabilitiesList, capsComparator)
go poolMetricsSender.SendAll()
if err != nil {
log.Errorf("Statsd create socked error: %s", err)
}
middlewareWrap.Add(middleware.NewStatsd(log.StandardLogger(), statsdClient, true).RegisterMetrics)
}
http.Handle("/wd/hub/session", middlewareWrap.Do(&handlers.CreateSession{Pool: poolInstance, ClientFactory: clientFactory})) //selenium
http.Handle("/session", middlewareWrap.Do(&handlers.CreateSession{Pool: poolInstance, ClientFactory: clientFactory})) //wda
http.Handle("/grid/register", middlewareWrap.Do(&handlers.RegisterNode{Pool: poolInstance}))
http.Handle("/grid/api/proxy", &handlers.APIProxy{Pool: poolInstance})
http.HandleFunc("/_info", heartbeat)
http.Handle("/", middlewareWrap.Do(&handlers.UseSession{Pool: poolInstance, Cache: cache}))
server := &http.Server{Addr: fmt.Sprintf(":%v", cfg.Grid.Port)}
serverError := make(chan error)
go func() {
err = server.ListenAndServe()
if err != nil {
serverError <- err
}
}()
select {
case err = <-serverError:
log.Fatalf("Server error, %s", err)
case <-stop:
}
log.Info("Shutting down the server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) // todo: move to config
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
log.Fatalf("graceful shutdown, %v", err)
}
log.Info("Server gracefully stopped")
}
func heartbeat(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"result": {"ok": true}}`))
if err != nil {
log.Errorf("write response, %v", err)
}
}