forked from cloudfoundry/bosh-io-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
periodic_s3_bucket_importer.go
83 lines (65 loc) · 1.83 KB
/
periodic_s3_bucket_importer.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
package importer
import (
"time"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
bhs3 "github.com/cppforlife/bosh-hub/s3"
bhstemsrepo "github.com/cppforlife/bosh-hub/stemcell/stemsrepo"
)
type PeriodicS3BucketImporter struct {
p time.Duration
stopCh <-chan struct{}
buckets []bhs3.Bucket
stemcellsRepo bhstemsrepo.S3StemcellsRepository
logTag string
logger boshlog.Logger
}
func NewPeriodicS3BucketImporter(
p time.Duration,
stopCh <-chan struct{},
buckets []bhs3.Bucket,
stemcellsRepo bhstemsrepo.S3StemcellsRepository,
logger boshlog.Logger,
) PeriodicS3BucketImporter {
return PeriodicS3BucketImporter{
p: p,
stopCh: stopCh,
buckets: buckets,
stemcellsRepo: stemcellsRepo,
logTag: "PeriodicS3BucketImporter",
logger: logger,
}
}
func (i PeriodicS3BucketImporter) Import() error {
i.logger.Info(i.logTag, "Starting importing S3 bucket stemcells every '%s'", i.p)
for {
select {
case <-time.After(i.p):
i.logger.Debug(i.logTag, "Looking at the bucket")
err := i.importAllStemcells()
if err != nil {
i.logger.Error(i.logTag, "Failed to import bucket: '%s'", err)
}
case <-i.stopCh:
i.logger.Info(i.logTag, "Stopped looking at the bucket")
return nil
}
}
}
func (i PeriodicS3BucketImporter) importAllStemcells() error {
allFiles := []bhs3.File{}
for _, bucket := range i.buckets {
someFiles, err := bucket.Files()
if err != nil {
// Return immediately if it cannot fetch latest files
// from any bucket so that saved stemcells do not get overwritten
return bosherr.WrapError(err, "Getting bucket files")
}
allFiles = append(allFiles, someFiles...)
}
err := i.stemcellsRepo.SaveAll(allFiles)
if err != nil {
return bosherr.WrapError(err, "Saving bucket files in stemcells repo")
}
return nil
}