/
local.go
217 lines (185 loc) · 4.74 KB
/
local.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
package storage
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/pkg/errors"
"github.com/tevino/abool"
"go.uber.org/zap"
)
const (
srcDirName = "src"
binDirName = "bin"
workDirName = "goplay-builds"
maxCleanTime = time.Second * 10
perm = 0744
)
type cachedFile struct {
io.ReadCloser
useLock *sync.Mutex
}
func (c cachedFile) Read(p []byte) (n int, err error) {
c.useLock.Lock()
defer c.useLock.Unlock()
return c.ReadCloser.Read(p)
}
// LocalStorage is local build artigact storage
type LocalStorage struct {
log *zap.SugaredLogger
useLock *sync.Mutex
dirty *abool.AtomicBool
gcRun *abool.AtomicBool
workDir string
srcDir string
binDir string
}
// NewLocalStorage constructs new local storage
func NewLocalStorage(log *zap.SugaredLogger, baseDir string) (ls *LocalStorage, err error) {
var isDirty bool
logger := log.Named("storage")
workDir := filepath.Join(baseDir, workDirName)
if err = os.MkdirAll(workDir, perm); err != nil {
if !os.IsExist(err) {
return nil, errors.Wrap(err, "failed to create temporary build directory for WASM compiler")
}
}
isDirty, err = isDirDirty(workDir)
if err != nil {
logger.Errorw("failed to check if work dir is dirty", "err", err)
}
if isDirty {
logger.Info("storage directory is not empty and dirty")
}
return &LocalStorage{
log: logger,
workDir: workDir,
useLock: &sync.Mutex{},
dirty: abool.NewBool(isDirty),
gcRun: abool.NewBool(false),
binDir: filepath.Join(workDir, binDirName),
srcDir: filepath.Join(workDir, srcDirName),
}, nil
}
func isDirDirty(dir string) (bool, error) {
items, err := ioutil.ReadDir(dir)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return len(items) > 0, nil
}
func (s LocalStorage) getOutputLocation(id ArtifactID) string {
return filepath.Join(s.binDir, id.Ext(ExtWasm))
}
// HasItem implements storage interface
func (s LocalStorage) HasItem(id ArtifactID) (bool, error) {
s.useLock.Lock()
defer s.useLock.Unlock()
fPath := s.getOutputLocation(id)
_, err := os.Stat(fPath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
// GetItem implements storage interface
func (s LocalStorage) GetItem(id ArtifactID) (io.ReadCloser, error) {
s.useLock.Lock()
defer s.useLock.Unlock()
fPath := s.getOutputLocation(id)
f, err := os.Open(fPath)
if os.IsNotExist(err) {
return nil, ErrNotExists
}
return cachedFile{
ReadCloser: f,
useLock: s.useLock,
}, err
}
// CreateLocationAndDo implements storage interface
func (s LocalStorage) CreateLocationAndDo(id ArtifactID, data []byte, cb Callback) error {
s.useLock.Lock()
defer s.useLock.Unlock()
s.dirty.Set() // mark storage as dirty
tmpSrcDir := filepath.Join(s.srcDir, id.String())
if err := os.MkdirAll(tmpSrcDir, perm); err != nil {
if !os.IsExist(err) {
s.log.Errorw("failed to create a temporary build directory",
"artifact", id.String(),
"dir", tmpSrcDir,
"err", err.Error(),
)
return errors.Wrapf(err, "failed to create temporary build directory")
}
s.log.Debugw("build directory already exists", "artifact", id.String())
}
wasmLocation := s.getOutputLocation(id)
goFileName := id.Ext(ExtGo)
srcFile := filepath.Join(tmpSrcDir, goFileName)
if err := ioutil.WriteFile(srcFile, data, perm); err != nil {
s.log.Errorw(
"failed to save source file",
"artifact", id.String(),
"file", srcFile,
"err", err,
)
return errors.Wrapf(err, "failed to save source file %q", goFileName)
}
return cb(wasmLocation, srcFile)
}
func (s LocalStorage) clean() error {
if !s.dirty.IsSet() {
s.log.Debug("storage is not dirty, skipping")
return nil
}
s.log.Debug("cleanup start")
t := time.AfterFunc(maxCleanTime, func() {
s.log.Warnf("cleanup took more than %.0f seconds!", maxCleanTime.Seconds())
})
s.useLock.Lock()
defer s.useLock.Unlock()
defer t.Stop()
// cleanup sources and binaries
for _, dir := range []string{s.srcDir, s.binDir} {
if err := os.RemoveAll(dir); err != nil {
if os.IsNotExist(err) {
continue
}
return errors.Wrapf(err, "failed to remove %q", dir)
}
s.log.Debugf("cleaner: removed directory %q", dir)
}
s.dirty.UnSet() // remove dirty flag
s.log.Debug("cleaner: cleanup end")
return nil
}
// StartCleaner implements storage interface
func (s LocalStorage) StartCleaner(ctx context.Context, interval time.Duration, wg *sync.WaitGroup) {
s.gcRun.Set()
s.log.Debug("cleaner worker starter")
for {
select {
case <-ctx.Done():
s.log.Debug("context done, cleaner worker stopped")
s.gcRun.UnSet()
if wg != nil {
wg.Done()
}
return
default:
}
<-time.After(interval)
if err := s.clean(); err != nil {
s.log.Error(err)
}
}
}