Files
admin 860fb66442
Build way-node / Test, Build, Push (push) Failing after 14s
feat: way-node — 完整更名与 Gitea CI
- Go module: github.com/cedar2025/xboard-node → github.com/viaeon/way-node(307 处 import 同步)
- cmd/xboard-node/ → cmd/way-node/
- install.sh: APP_NAME / INSTALL_ROOT(/etc/way-node) / BINARY_PATH / SERVICE_NAME 全部更名,
  下载源与 Documentaion 指向 git.viaeon.com/admin/way-node
- Makefile / Dockerfile 产物与镜像路径更名
- README 全篇重写链接
- 新增 .gitea/workflows/build.yml:容器内跑测试 + 交叉编译 + 构建镜像推 git.viaeon.com/admin/way-node
  构建全程在 golang:1.26 容器里,不依赖 runner 宿主装 Go
- 禁用 .github/workflows/ci.yml(依赖 ghcr.io 与 GITHUB_TOKEN,Gitea 上跑不通)

保留:xbctl 工具名(不含旧品牌)、go.sum 里上游依赖哈希
2026-11-27 11:41:30 +08:00

161 lines
3.1 KiB
Go

package config
import (
"context"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/viaeon/way-node/internal/nlog"
"github.com/fsnotify/fsnotify"
)
// Watcher reloads the config file on change (debounced) and calls onChange.
type Watcher struct {
path string
debounce time.Duration
onChange func(*Config)
onChangeRoot func(*RootConfig)
watcher *fsnotify.Watcher
stopOnce sync.Once
stopCh chan struct{}
stopped atomic.Bool
}
// WatchConfig watches path; invalid reloads are logged and ignored.
// Stop via Stop() or cancel ctx.
func WatchConfig(ctx context.Context, path string, onChange func(*Config)) (*Watcher, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
fsw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// Watch parent dir so rename/atomic saves are visible.
dir := filepath.Dir(absPath)
if err := fsw.Add(dir); err != nil {
fsw.Close()
return nil, err
}
w := &Watcher{
path: absPath,
debounce: 1 * time.Second,
onChange: onChange,
watcher: fsw,
stopCh: make(chan struct{}),
}
go w.loop(ctx)
nlog.Core().Info("config watcher started", "path", absPath)
return w, nil
}
// WatchConfigRoot watches path and reloads the root config model.
func WatchConfigRoot(ctx context.Context, path string, onChange func(*RootConfig)) (*Watcher, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
fsw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
dir := filepath.Dir(absPath)
if err := fsw.Add(dir); err != nil {
fsw.Close()
return nil, err
}
w := &Watcher{
path: absPath,
debounce: 1 * time.Second,
onChangeRoot: onChange,
watcher: fsw,
stopCh: make(chan struct{}),
}
go w.loop(ctx)
nlog.Core().Info("config watcher started", "path", absPath)
return w, nil
}
func (w *Watcher) loop(ctx context.Context) {
var timer *time.Timer
defer func() {
if timer != nil {
timer.Stop()
}
w.watcher.Close()
}()
for {
select {
case <-ctx.Done():
return
case <-w.stopCh:
return
case ev, ok := <-w.watcher.Events:
if !ok {
return
}
if filepath.Clean(ev.Name) != w.path {
continue
}
if !ev.Has(fsnotify.Write) && !ev.Has(fsnotify.Create) {
continue
}
if timer == nil {
timer = time.AfterFunc(w.debounce, w.reload)
} else {
timer.Reset(w.debounce)
}
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
nlog.Core().Warn("config watcher error", "error", err)
}
}
}
func (w *Watcher) reload() {
if w.stopped.Load() {
return
}
if w.onChangeRoot != nil {
root, err := LoadRoot(w.path)
if err != nil {
nlog.Core().Error("config reload failed, keeping current config", "error", err)
return
}
nlog.Core().Info("config reloaded successfully")
w.onChangeRoot(root)
return
}
cfg, err := Load(w.path)
if err != nil {
nlog.Core().Error("config reload failed, keeping current config", "error", err)
return
}
nlog.Core().Info("config reloaded successfully")
w.onChange(cfg)
}
func (w *Watcher) Stop() {
w.stopped.Store(true)
w.stopOnce.Do(func() { close(w.stopCh) })
}