/
poller.go
286 lines (242 loc) · 6.02 KB
/
poller.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package poll
import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/net/context"
"github.com/golang/glog"
"github.com/pkg/sftp"
)
type (
Config struct {
// Directory for downloaded files
TempDir string
// True to only show commands w/o executing them
DryRun bool
// URL:port of the sftp server to poll
HostPort string
// ssh configuration
Ssh *ssh.ClientConfig
}
Poller interface {
io.Closer
Updates() <-chan *Update
}
// Update represents either a poller downloaded artefact,
// or describes a remote file entry
Update struct {
Path string
Timestamp time.Time
}
localUpdates []*Update
updatesByName struct {
localUpdates
}
poller struct {
// Remote path to poll/fetch from
root string
// Channel poller sends updates on
updatesChan chan *Update
config *Config
client *sftp.Client
conn *ssh.Client
// State poller maintains
snapshot map[string]time.Time
resumes map[string]int64
}
)
func New(snapshot map[string]time.Time, ctx context.Context, config *Config, remoteRoot string) (Poller, error) {
sshConn, err := ssh.Dial("tcp", config.HostPort, config.Ssh)
if err != nil {
glog.Fatalf("cannot ssh.Dial: %s", err)
return nil, err
}
client, err := sftp.NewClient(sshConn)
if err != nil {
sshConn.Close()
glog.Fatalf("cannot create sftp client: %s", err)
return nil, err
}
p := &poller{
root: remoteRoot,
config: config,
conn: sshConn,
client: client,
updatesChan: make(chan *Update),
snapshot: snapshot,
resumes: make(map[string]int64),
}
go p.loop(ctx)
glog.V(2).Infoln("poller: started")
return p, nil
}
func (p *poller) Close() error {
if p.conn != nil {
p.conn.Close()
p.conn = nil
}
if p.client != nil {
p.client.Close()
p.client = nil
}
return nil
}
func (p *poller) Updates() <-chan *Update {
return p.updatesChan
}
func (p *poller) loop(ctx context.Context) {
const POLL_TIMEOUT = time.Minute * 5
var pollTimeout = POLL_TIMEOUT
if err := p.fetch(); err != nil {
glog.Errorf("poller error: %v", err)
pollTimeout = 1
}
L:
for {
select {
case <-time.After(pollTimeout * time.Second):
pollTimeout = POLL_TIMEOUT // reset
for {
if err := p.fetch(); err != nil {
if isErrorEOF(err) {
if err = p.reconnect(); err != nil {
glog.Errorf("poller reconnect error: %v", err)
break L
} else {
// TODO: use backoff for reconnects
time.Sleep(1e9)
continue
}
} else {
glog.Errorf("poller error: %v", err)
}
}
break
}
case <-ctx.Done():
glog.V(2).Infoln("poller: cancelled")
break L
}
}
glog.V(2).Infoln("poller: exit loop")
close(p.updatesChan)
}
func (p *poller) reconnect() error {
glog.V(2).Infoln("poller: reconnect")
p.conn.Close()
p.conn = nil
p.client.Close()
p.client = nil
conn, err := ssh.Dial("tcp", p.config.HostPort, p.config.Ssh)
if err != nil {
glog.Fatalf("cannot ssh.Dial: %s", err)
return err
}
client, err := sftp.NewClient(conn)
if err != nil {
conn.Close()
return err
}
p.conn = conn
p.client = client
return nil
}
func (p *poller) fetch() error {
entries, err := p.client.ReadDir(p.root)
if err != nil {
return err
}
var updates localUpdates
for _, entry := range entries {
if !requiredFileMask(entry.Name()) {
continue
}
timestamp, ok := p.snapshot[entry.Name()]
if !ok || timestamp.Before(entry.ModTime()) {
glog.Infof(`poller: update for "%s"`, entry.Name())
// p.snapshot[entry.Name()] = entry.ModTime()
updates = append(updates, &Update{
Path: entry.Name(),
Timestamp: entry.ModTime(),
})
}
}
if len(updates) > 0 {
glog.V(2).Infof("poller: %d updates", len(updates))
sort.Sort(updatesByName{updates})
for _, update := range updates {
updatePath := p.client.Join(p.root, update.Path)
var notification *Update
notification, err = p.download(updatePath, update.Timestamp)
if err != nil {
glog.Errorf(`poller: cannot download "%s" (giving up): %v`, updatePath, err)
break
}
p.updatesChan <- notification
p.snapshot[update.Path] = update.Timestamp
}
} else {
glog.Infoln("poller: nothing to do")
}
return err
}
func (p *poller) download(path string, time time.Time) (*Update, error) {
file, err := p.client.Open(path)
if err != nil {
return nil, err
}
fileName := filepath.Base(path)
localPath := filepath.Join(p.config.TempDir, fileName)
if !p.config.DryRun {
var fileOut *os.File
var err error
// FIXME: make sure that if the file has been completely downloaded
// its p.resumes entry gets removed
if offset, ok := p.resumes[localPath]; ok {
fileOut, err = os.OpenFile(localPath, os.O_WRONLY|os.O_APPEND, 0600)
if err == nil {
glog.Infof("restoring a resume point at %d for %s", offset, localPath)
if n, err := file.Seek(offset, os.SEEK_SET); err != nil {
glog.Errorf("cannot seek(%d) at %d: %v", offset, n, err)
}
}
} else {
fileOut, err = os.Create(localPath)
}
if err != nil {
return nil, err
}
defer fileOut.Close()
if n, err := io.Copy(fileOut, file); err != nil {
// TODO: note the offset of incomplete Copy, and resume after reconnect
glog.Infof("%s: saving a resume point for %d bytes", localPath, n)
p.resumes[localPath] += n
return nil, err
} else {
delete(p.resumes, localPath)
}
}
return &Update{Path: localPath, Timestamp: time}, nil
}
func requiredFileMask(name string) bool {
return strings.HasSuffix(name, ".xml")
}
func isErrorEOF(err error) bool {
return err == io.EOF || err == io.ErrUnexpectedEOF
}
func (u updatesByName) Len() int { return len(u.localUpdates) }
func (u updatesByName) Swap(i, j int) {
u.localUpdates[i], u.localUpdates[j] = u.localUpdates[j], u.localUpdates[i]
}
func (u updatesByName) Less(i, j int) bool { return u.localUpdates[i].Path < u.localUpdates[j].Path }
func (u localUpdates) String() string {
result := make([]string, len(u))
for i, update := range u {
result[i] = update.Path
}
return strings.Join(result, ",")
}