diff options
Diffstat (limited to 'deps')
-rw-r--r-- | deps/deps.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/deps/deps.go b/deps/deps.go index c2919c9c5..36620c96b 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -94,6 +94,9 @@ type Deps struct { // BuildStartListeners will be notified before a build starts. BuildStartListeners *Listeners + // Resources that gets closed when the build is done or the server shuts down. + BuildClosers *Closers + // Atomic values set during a build. // This is common/global for all sites. BuildState *BuildState @@ -284,6 +287,7 @@ func New(cfg DepsCfg) (*Deps, error) { Site: cfg.Site, FileCaches: fileCaches, BuildStartListeners: &Listeners{}, + BuildClosers: &Closers{}, BuildState: buildState, Running: cfg.Running, Timeout: time.Duration(timeoutms) * time.Millisecond, @@ -297,6 +301,10 @@ func New(cfg DepsCfg) (*Deps, error) { return d, nil } +func (d *Deps) Close() error { + return d.BuildClosers.Close() +} + // ForLanguage creates a copy of the Deps with the language dependent // parts switched out. func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, error) { @@ -399,3 +407,30 @@ func (b *BuildState) Incr() int { func NewBuildState() BuildState { return BuildState{} } + +type Closer interface { + Close() error +} + +type Closers struct { + mu sync.Mutex + cs []Closer +} + +func (cs *Closers) Add(c Closer) { + cs.mu.Lock() + defer cs.mu.Unlock() + cs.cs = append(cs.cs, c) +} + +func (cs *Closers) Close() error { + cs.mu.Lock() + defer cs.mu.Unlock() + for _, c := range cs.cs { + c.Close() + } + + cs.cs = cs.cs[:0] + + return nil +} |