-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
63 lines (58 loc) · 1.25 KB
/
worker.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
package gobulk
import (
"context"
"log"
"time"
)
// worker is an entity to fulfill data download tasks.
type worker struct {
read func(container *Container) (map[string][]byte, error)
requests chan *workerRequest
results chan *workerResponse
stop chan struct{}
}
// run runs the worker.
func (w *worker) run() {
for {
select {
case <-w.stop:
return
case req, ok := <-w.requests:
if !ok {
return
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
t := time.NewTicker(5 * time.Minute)
for {
select {
case <-ctx.Done():
return
case <-t.C:
log.Printf("worker download is in progress: %s %s", req.container.InputRepository, req.container.InputIdentifier)
}
}
}()
d, err := w.read(req.container)
w.results <- &workerResponse{
id: req.id,
response: &readResponse{
data: d,
err: err,
},
}
cancel()
}
}
}
// workerRequest contains data needed to build a request and distinguish the
// corresponding container identity.
type workerRequest struct {
container *Container
id string
}
// workerResponse contains data regarding a single workerRequest result.
type workerResponse struct {
response *readResponse
id string
}