forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
empty_fetch_source.go
122 lines (103 loc) · 2.83 KB
/
empty_fetch_source.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
package resource
import (
"os"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/worker"
)
type emptyFetchSource struct {
logger lager.Logger
worker worker.Worker
cache Cache
container worker.Container
versionedSource VersionedSource
cacheIdentifier CacheIdentifier
containerCreator FetchContainerCreator
resourceOptions ResourceOptions
}
func NewEmptyFetchSource(
logger lager.Logger,
worker worker.Worker,
cacheIdentifier CacheIdentifier,
containerCreator FetchContainerCreator,
resourceOptions ResourceOptions,
) FetchSource {
return &emptyFetchSource{
logger: logger,
worker: worker,
cache: noopCache{},
cacheIdentifier: cacheIdentifier,
containerCreator: containerCreator,
resourceOptions: resourceOptions,
}
}
func (s *emptyFetchSource) IsInitialized() (bool, error) {
return s.cache.IsInitialized()
}
func (s *emptyFetchSource) VersionedSource() VersionedSource {
return s.versionedSource
}
func (s *emptyFetchSource) LockName() (string, error) {
return s.resourceOptions.LockName(s.worker.Name())
}
func (s *emptyFetchSource) Initialize(signals <-chan os.Signal, ready chan<- struct{}) error {
var err error
s.cache, err = s.findOrCreateCacheVolume()
if err != nil {
return err
}
s.container, err = s.containerCreator.CreateWithVolume(string(s.resourceOptions.ResourceType()), s.cache.Volume(), s.worker)
if err != nil {
s.logger.Error("failed-to-create-container", err)
return err
}
s.versionedSource, err = NewResource(s.container).Get(
s.cache.Volume(),
s.resourceOptions.IOConfig(),
s.resourceOptions.Source(),
s.resourceOptions.Params(),
s.resourceOptions.Version(),
signals,
ready,
)
if err == ErrAborted {
s.logger.Error("get-run-resource-aborted", err, lager.Data{"container": s.container.Handle()})
return ErrInterrupted
}
if err != nil {
s.logger.Error("failed-to-fetch-resource", err)
return err
}
err = s.cache.Initialize()
if err != nil {
s.logger.Error("failed-to-initialize-cache", err)
return err
}
return nil
}
func (s *emptyFetchSource) Release(finalTTL *time.Duration) {
if s.cache.Volume() != nil {
s.cache.Volume().Release(finalTTL)
}
if s.container != nil {
s.container.Release(finalTTL)
}
}
func (s *emptyFetchSource) findOrCreateCacheVolume() (Cache, error) {
cachedVolume, cacheFound, err := s.cacheIdentifier.FindOn(s.logger, s.worker)
if err != nil {
s.logger.Error("failed-to-look-for-cache", err)
return nil, err
}
if cacheFound {
s.logger.Debug("found-cache", lager.Data{"volume": cachedVolume.Handle()})
} else {
s.logger.Debug("no-cache-found")
cachedVolume, err = s.cacheIdentifier.CreateOn(s.logger, s.worker)
if err != nil {
s.logger.Error("failed-to-create-cache", err)
return nil, err
}
}
return volumeCache{cachedVolume}, nil
}