Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
170 lines (161 loc) · 4.74 KB
/
Copy pathhandler.go
File metadata and controls
170 lines (161 loc) · 4.74 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os/exec"
"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 Result) {
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 <- Result{Message: res, ThreadId: newCommand.ThreadId}
case <-time.After(30 * time.Second):
resultChannel <- Result{Message: "The operation took more than 30 seconds and timed out, sorry :(", ThreadId: newCommand.ThreadId}
}
}()
}
}
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.Println(err)
return
}
defer func() {
if resp != nil {
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:
msg := fmt.Sprintf("The modifier '%v' is not handled\n", command.Target)
log.Println(msg)
resultChannel <- msg
}
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.Println(err)
return
}
defer func() {
if resp != nil {
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:
msg := fmt.Sprintf("The modifier '%v' is not handled\n", command.Target)
log.Println(msg)
resultChannel <- msg
}
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)
}
case CommandFortune:
log.Println("I will handle the 'fortune' command")
fortuneCookie, err := exec.Command("fortune").Output()
if err != nil {
log.Println(err)
resultChannel <- "No cookie for you!"
return
}
fortuneCookieString := string(fortuneCookie)
cowsaying, err := exec.Command("cowsay", fortuneCookieString).Output()
if err != nil {
log.Println(err)
resultChannel <- fortuneCookieString
return
}
resultChannel <- fmt.Sprintf("```\n%s\n```", string(cowsaying))
default:
log.Printf("The command '%+v' is not handled\n", command)
}
}