-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (74 loc) · 1.77 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
package main
import (
"fmt"
"html"
"log"
"net/http"
"strconv"
// "strings"
"time"
)
type ControlMessage struct {
Target string
Count int64
}
func main() {
controlChannel := make(chan ControlMessage)
workerCompleteChan := make(chan bool)
statusPollChannel := make(chan chan bool)
workerActive := false
go admin(controlChannel, statusPollChannel)
for {
select {
case respChan := <-statusPollChannel:
respChan <- workerActive
case msg := <-controlChannel:
workerActive = true
go doStuff(msg, workerCompleteChan)
case status := <-workerCompleteChan:
workerActive = status
}
}
}
func admin(cc chan ControlMessage, statusPollChannel chan chan bool) {
http.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
// hostTokens := strings.Split(r.Host, ":") // ?
r.ParseForm()
count, err := strconv.ParseInt(r.FormValue("count"), 10, 32)
if err != nil {
log.Fatal(err.Error())
return
}
msg := ControlMessage{
Target: r.FormValue("target"),
Count: count,
}
cc <- msg
fmt.Fprintf(w, "Control message issued for Target %s, count %d", html.EscapeString(r.FormValue("target")), count)
})
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
reqChan := make(chan bool)
statusPollChannel <- reqChan
timeout := time.After(time.Second)
select {
case result := <-reqChan:
if result {
fmt.Fprint(w, "ACTIVE")
} else {
fmt.Fprint(w, "INACTIVE")
}
return
case <-timeout:
fmt.Fprint(w, "TIMEOUT")
}
})
log.Fatal(http.ListenAndServe(":1337", nil))
}
// Below is original
func doStuff(msg ControlMessage, workerCompleteChan chan bool) {
for i := int64(0); i < msg.Count; i++ {
time.Sleep(1 * time.Second)
fmt.Printf("%s #%d\n", msg.Target, i+1)
}
workerCompleteChan <- false
}