depot/go/nix/nixbuild/coordinator.go

79 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-08-23 23:00:44 +00:00
// SPDX-FileCopyrightText: 2023 Luke Granger-Brown <depot@lukegb.com>
//
// SPDX-License-Identifier: Apache-2.0
package nixbuild
import (
"context"
"sort"
"sync"
2024-11-16 15:30:41 +00:00
"git.lukegb.com/lukegb/depot/go/nix/nixdrv"
"git.lukegb.com/lukegb/depot/go/nix/nixstore"
2023-08-23 23:00:44 +00:00
)
type Coordinator struct {
cfg Config
mu sync.Mutex
work map[string]*WorkItem
at *nixstore.ActivityTracker
}
func NewCoordinator(cfg Config, at *nixstore.ActivityTracker) *Coordinator {
return &Coordinator{
cfg: cfg,
work: make(map[string]*WorkItem),
at: at,
}
}
func (c *Coordinator) AddDerivationWork(ctx context.Context, drv *nixdrv.BasicDerivation, drvPath string) *WorkItem {
var outputNames []string
for o := range drv.Outputs {
outputNames = append(outputNames, o)
}
sort.Strings(outputNames)
path := drv.Outputs[outputNames[0]].Path
c.mu.Lock()
defer c.mu.Unlock()
if i := c.work[path]; i != nil {
return i
}
i := &WorkItem{
doneCh: make(chan struct{}),
path: path,
coord: c,
at: c.at,
}
c.work[path] = i
go i.run(ctx)
return i
}
func (c *Coordinator) AddWork(ctx context.Context, path string) *WorkItem {
c.mu.Lock()
defer c.mu.Unlock()
if i := c.work[path]; i != nil {
return i
}
i := &WorkItem{
doneCh: make(chan struct{}),
path: path,
coord: c,
at: c.at,
}
c.work[path] = i
go i.run(ctx)
return i
}