forked from vmware-archive/atc
/
sqldb_eventsource.go
172 lines (137 loc) · 2.69 KB
/
sqldb_eventsource.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
package db
import (
"encoding/json"
"sync"
"github.com/concourse/atc"
"github.com/concourse/atc/event"
)
func newSQLDBBuildEventSource(
buildID int,
table string,
conn Conn,
notifier Notifier,
from uint,
) *sqldbBuildEventSource {
wg := new(sync.WaitGroup)
source := &sqldbBuildEventSource{
buildID: buildID,
table: table,
conn: conn,
notifier: notifier,
events: make(chan event.Envelope, 2000),
stop: make(chan struct{}),
wg: wg,
}
wg.Add(1)
go source.collectEvents(from)
return source
}
type sqldbBuildEventSource struct {
buildID int
table string
conn Conn
notifier Notifier
events chan event.Envelope
stop chan struct{}
err error
wg *sync.WaitGroup
}
func (source *sqldbBuildEventSource) Next() (event.Envelope, error) {
select {
case e, ok := <-source.events:
if !ok {
return event.Envelope{}, source.err
}
return e, nil
}
}
func (source *sqldbBuildEventSource) Close() error {
select {
case <-source.stop:
return nil
default:
close(source.stop)
}
source.wg.Wait()
return source.notifier.Close()
}
func (source *sqldbBuildEventSource) collectEvents(cursor uint) {
defer source.wg.Done()
var batchSize = cap(source.events)
for {
select {
case <-source.stop:
source.err = ErrBuildEventStreamClosed
close(source.events)
return
default:
}
completed := false
err := source.conn.QueryRow(`
SELECT builds.completed
FROM builds
WHERE builds.id = $1
`, source.buildID).Scan(&completed)
if err != nil {
source.err = err
close(source.events)
return
}
rows, err := source.conn.Query(`
SELECT type, version, payload
FROM `+source.table+`
WHERE build_id = $1
ORDER BY event_id ASC
OFFSET $2
LIMIT $3
`, source.buildID, cursor, batchSize)
if err != nil {
source.err = err
close(source.events)
return
}
rowsReturned := 0
for rows.Next() {
rowsReturned++
cursor++
var t, v, p string
err := rows.Scan(&t, &v, &p)
if err != nil {
rows.Close()
source.err = err
close(source.events)
return
}
data := json.RawMessage(p)
ev := event.Envelope{
Data: &data,
Event: atc.EventType(t),
Version: atc.EventVersion(v),
}
select {
case source.events <- ev:
case <-source.stop:
rows.Close()
source.err = ErrBuildEventStreamClosed
close(source.events)
return
}
}
if rowsReturned == batchSize {
// still more events
continue
}
if completed {
source.err = ErrEndOfBuildEventStream
close(source.events)
return
}
select {
case <-source.notifier.Notify():
case <-source.stop:
source.err = ErrBuildEventStreamClosed
close(source.events)
return
}
}
}