59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
//go:build linux
|
|
|
|
package buildstep
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
)
|
|
|
|
// watchSettings wakes the puller each time its settings file is written or replaced (inotify on the file's directory, so
|
|
// a rename over it counts): the event the node-engine's refill of the file is, which a parked puller at capacity 0
|
|
// otherwise never sees. No timer: it blocks on the kernel's event. A watch that cannot start is said once.
|
|
func (p *Puller) watchSettings(ctx context.Context) {
|
|
if p.SettingsFile == "" {
|
|
return
|
|
}
|
|
fd, err := syscall.InotifyInit1(syscall.IN_CLOEXEC | syscall.IN_NONBLOCK)
|
|
if err != nil {
|
|
p.logf("the settings file %s cannot be watched, so a refill of it wakes the puller only at another event: %v", p.SettingsFile, err)
|
|
return
|
|
}
|
|
f := os.NewFile(uintptr(fd), "inotify")
|
|
defer f.Close()
|
|
dir, base := filepath.Split(p.SettingsFile)
|
|
if _, err := syscall.InotifyAddWatch(fd, filepath.Clean(dir), syscall.IN_CLOSE_WRITE|syscall.IN_MOVED_TO|syscall.IN_CREATE); err != nil {
|
|
p.logf("the settings file %s cannot be watched, so a refill of it wakes the puller only at another event: %v", p.SettingsFile, err)
|
|
return
|
|
}
|
|
go func() { <-ctx.Done(); f.Close() }()
|
|
buf := make([]byte, 4096)
|
|
for {
|
|
n, err := f.Read(buf)
|
|
if err != nil {
|
|
return
|
|
}
|
|
hit := false
|
|
for off := 0; off+16 <= n; {
|
|
l := int(binary.LittleEndian.Uint32(buf[off+12:]))
|
|
name := buf[off+16 : off+16+l]
|
|
for i, c := range name {
|
|
if c == 0 {
|
|
name = name[:i]
|
|
break
|
|
}
|
|
}
|
|
if string(name) == base {
|
|
hit = true
|
|
}
|
|
off += 16 + l
|
|
}
|
|
if hit {
|
|
p.Wake()
|
|
}
|
|
}
|
|
}
|