forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
191 lines (165 loc) · 4.28 KB
/
queue.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package server
import (
"encoding/json"
"io"
"io/ioutil"
"net"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/drone/drone/common"
"github.com/drone/drone/eventbus"
)
// TODO (bradrydzewski) the callback URL should be signed.
// TODO (bradrydzewski) we shouldn't need to fetch the Repo if specified in the URL path
// TODO (bradrydzewski) use SetRepoLast to update the last repository
// GET /queue/pull
func PollBuild(c *gin.Context) {
queue := ToQueue(c)
store := ToDatastore(c)
agent := &common.Agent{
Addr: c.Request.RemoteAddr,
}
// extact the host port and name and
// replace with the default agent port (1999)
host, _, err := net.SplitHostPort(agent.Addr)
if err == nil {
agent.Addr = host
}
agent.Addr = net.JoinHostPort(agent.Addr, "1999")
log.Infof("agent connected and polling builds at %s", agent.Addr)
work := queue.PullClose(c.Writer)
if work == nil {
c.AbortWithStatus(500)
return
}
// TODO (bradrydzewski) decide how we want to handle a failure here
// still not sure exact behavior we want ...
err = store.SetBuildAgent(work.Repo.FullName, work.Build.Number, agent)
if err != nil {
log.Errorf("error persisting build agent. %s", err)
}
c.JSON(200, work)
// acknowledge work received by the client
queue.Ack(work)
}
// GET /queue/push/:owner/:repo
func PushBuild(c *gin.Context) {
store := ToDatastore(c)
repo := ToRepo(c)
bus := ToBus(c)
in := &common.Build{}
if !c.BindWith(in, binding.JSON) {
return
}
build, err := store.Build(repo.FullName, in.Number)
if err != nil {
c.Fail(404, err)
return
}
if in.State != common.StatePending && in.State != common.StateRunning {
store.DelBuildAgent(repo.FullName, build.Number)
}
build.Duration = in.Duration
build.Started = in.Started
build.Finished = in.Finished
build.State = in.State
err = store.SetBuildState(repo.FullName, build)
if err != nil {
c.Fail(500, err)
return
}
if build.State != common.StatePending && build.State != common.StateRunning {
if repo.Last == nil || build.Number >= repo.Last.Number {
repo.Last = build
store.SetRepo(repo)
}
}
// <-- FIXME
// for some reason the Repo and Build fail to marshal to JSON.
// It has something to do with memory / pointers. So it goes away
// if I just refetch these items. Needs to be fixed in the future,
// but for now should be ok
repo, err = store.Repo(repo.FullName)
if err != nil {
c.Fail(500, err)
return
}
build, err = store.Build(repo.FullName, in.Number)
if err != nil {
c.Fail(404, err)
return
}
// END FIXME -->
msg, err := json.Marshal(build)
if err == nil {
c.String(200, err.Error()) // we can ignore this error
return
}
bus.Send(&eventbus.Event{
Name: repo.FullName,
Kind: eventbus.EventRepo,
Msg: msg,
})
c.Writer.WriteHeader(200)
}
// POST /queue/push/:owner/:repo/:build
func PushTask(c *gin.Context) {
store := ToDatastore(c)
repo := ToRepo(c)
bus := ToBus(c)
num, _ := strconv.Atoi(c.Params.ByName("build"))
in := &common.Task{}
if !c.BindWith(in, binding.JSON) {
return
}
err := store.SetBuildTask(repo.FullName, num, in)
if err != nil {
c.Fail(404, err)
return
}
build, err := store.Build(repo.FullName, num)
if err != nil {
c.Fail(404, err)
return
}
msg, err := json.Marshal(build)
if err == nil {
c.String(200, err.Error()) // we can ignore this error
return
}
bus.Send(&eventbus.Event{
Name: repo.FullName,
Kind: eventbus.EventRepo,
Msg: msg,
})
c.Writer.WriteHeader(200)
}
// POST /queue/push/:owner/:repo/:build/:task/logs
func PushLogs(c *gin.Context) {
store := ToDatastore(c)
repo := ToRepo(c)
bnum, _ := strconv.Atoi(c.Params.ByName("build"))
tnum, _ := strconv.Atoi(c.Params.ByName("task"))
// TODO (bradrydzewski) change this interface to accept an io.Reader
// instead of a byte array so that we can buffer the write and so that
// we avoid unnecessary copies of the data in memory.
logs, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 5000000)) //5MB
defer c.Request.Body.Close()
if err != nil {
c.Fail(500, err)
return
}
err = store.SetLogs(repo.FullName, bnum, tnum, logs)
if err != nil {
c.Fail(500, err)
return
}
c.Writer.WriteHeader(200)
}
func GetQueue(c *gin.Context) {
queue := ToQueue(c)
items := queue.Items()
c.JSON(200, items)
}