-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
139 lines (130 loc) · 3.92 KB
/
handler.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"runtime"
"time"
)
var _prsURL *url.URL
var _prsConfig *[]byte
var _prsAPIKey string
var _client *http.Client
// InitCommandHandler initializes a PullRequestService command
// handler, you need to have a URL to the service, a configuration
// definition, its API key, and a HTTP client so you can talk to it.
func InitCommandHandler(prsURL *url.URL, prsConfig *[]byte, prsAPIKey string, client *http.Client) {
_prsURL = prsURL
_prsConfig = prsConfig
_prsAPIKey = prsAPIKey
_client = client
}
// RunCommandHandler It is the top level function that must be called
// as a goroutine to handle a command.
func RunCommandHandler(commandChannel <-chan Command, resultChannel chan string) {
if resultChannel == nil {
panic("Must supply a channel for command results")
}
if commandChannel == nil {
panic("Must supply a channel for commands to arrive on")
}
// handle commands until the end of time
for {
newCommand := <-commandChannel
go func() {
// This is a buffered channel so that a goroutine that has timed out
// has a place to respond to
dedicatedResultChan := make(chan string, 1)
// Fire off handling of command
go handleCommand(newCommand, dedicatedResultChan)
select {
case res := <-dedicatedResultChan:
resultChannel <- res
case <-time.After(30 * time.Second):
resultChannel <- "The operation took more than 30 seconds and timed out, sorry :("
}
}()
}
}
func handleCommand(command Command, resultChannel chan string) {
switch command.Type {
case CommandStart:
switch command.Target {
case CommandTargetPR:
log.Println("I will start processing of pull requests")
startService := &http.Request{}
startService.Method = "POST"
q := _prsURL.Query()
q.Set("apikey", prsAPIKey)
startService.URL = &url.URL{
Host: _prsURL.Host,
Scheme: _prsURL.Scheme,
Opaque: "/host/services",
RawQuery: q.Encode(),
}
startService.Header = map[string][]string{
"Content-Type": {"application/xml"},
}
startService.Body = ioutil.NopCloser(bytes.NewReader(*_prsConfig))
startService.ContentLength = int64(len(*_prsConfig))
resp, err := _client.Do(startService)
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
statusCode := resp.StatusCode
if statusCode >= 200 && statusCode < 300 {
msg := "Successfully started processing pull requests"
log.Println(msg)
resultChannel <- msg
} else {
msg := "Failed to start processing pull requests: " + resp.Status
resultChannel <- msg
}
default:
log.Printf("The modifier '%s' is not handled\n", command.Target)
}
case CommandStop:
switch command.Target {
case CommandTargetPR:
log.Println("I will handle 'stop pr' command")
stopService := &http.Request{}
stopService.Method = "DELETE"
q := _prsURL.Query()
q.Set("apikey", prsAPIKey)
_prsURL.RawQuery = q.Encode()
stopService.URL = _prsURL
resp, err := _client.Do(stopService)
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
statusCode := resp.StatusCode
if statusCode >= 200 && statusCode < 300 {
msg := "Successfully stopped processing pull requests"
log.Println(msg)
resultChannel <- msg
} else {
msg := "Failed to stop processing pull requests: " + resp.Status
log.Println(msg)
resultChannel <- msg
}
default:
log.Printf("The modifier '%s' is not handled\n", command.Target)
}
case CommandStatus:
switch command.Target {
case CommandTargetMtFlow:
log.Println("I will handle 'status mtflow' command")
// get some memory statistics
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
resultChannel <- fmt.Sprintf("I am chugging along, thanks for asking.\n\n# of Goroutines: %v\n# of CPU: %v\nTotal Memory: %v", runtime.NumGoroutine(), runtime.NumCPU(), memStats.Alloc)
}
default:
log.Printf("The command '%+v' is not handled\n", command)
}
}