-
Notifications
You must be signed in to change notification settings - Fork 7
/
mega_distributed_funcs.go
60 lines (53 loc) · 1.63 KB
/
mega_distributed_funcs.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
package main
import "log"
func FindServer(req string) (server Server, index int) {
for index, server = range Config.Servers {
if server.ID == req {
break
}
}
return
}
func RespondToDispatcher(server Server) {
request := make(map[string]interface{})
request["req"] = server
serverJson := mResponse(request)
endpoint := Endpoint{Method: "POST", Data: serverJson, Headers: AuthorizeHeader, Path: UpdateServerPath}
responseCode := Req(Dispatcher, endpoint)
if responseCode != 200 {
log.Println("Could not respond to dispatcher.")
}
}
func DispatchtoWorker(server Server) {
request := make(map[string]interface{})
request["req"] = server.ID
serverJson := mResponse(request)
endpoint := Endpoint{Method: "POST", Data: serverJson, Headers: AuthorizeHeader, Path: ProcessServerPath}
responseCode := Req(Worker, endpoint)
if responseCode != 200 {
log.Println("Could not dispatch task to worker. Reverting to this instance.")
server, index := FindServer(server.ID)
Process(server, index)
}
}
func SelfAnnounce(addr string) {
request := make(map[string]interface{})
request["req"] = WorkerAddressPort
// if left empty default values
// from mega_config will be used.
if addr != "" {
Dispatcher.Host = addr
}
serverJson := mResponse(request)
endpoint := Endpoint{Method: "POST", Data: serverJson, Headers: AuthorizeHeader, Path: RegisterServerPath}
responseCode := Req(Dispatcher, endpoint)
if responseCode != 200 {
log.Println("Could not reach your dispatcher.")
}
}
func RegisterWorker(addr string) {
GL.Lock.Lock()
Worker = Server{Host: addr}
log.Println("Worker location address set to :", addr)
GL.Lock.Unlock()
}