fix: 启动时检测provider文件是否过期,强制更新

This commit is contained in:
Skyxim 2022-05-26 21:05:00 +08:00
parent 2ebc0383b5
commit a71fd3b4df
2 changed files with 58 additions and 29 deletions

View file

@ -26,6 +26,7 @@ type fetcher struct {
done chan struct{}
hash [16]byte
parser parser
interval time.Duration
onUpdate func(any)
}
@ -56,6 +57,12 @@ func (f *fetcher) Initial() (any, error) {
modTime := stat.ModTime()
f.updatedAt = &modTime
isLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
defer func() {
log.Infoln("[Provider] %s's proxies not updated for a long time")
go f.update()
}()
}
} else {
buf, err = f.vehicle.Read()
}
@ -137,21 +144,11 @@ func (f *fetcher) pullLoop() {
for {
select {
case <-f.ticker.C:
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
same, err := f.update()
if same || err != nil {
continue
}
if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
continue
}
log.Infoln("[Provider] %s's proxies update", f.Name())
if f.onUpdate != nil {
f.onUpdate(elm)
}
case <-f.done:
f.ticker.Stop()
return
@ -159,6 +156,23 @@ func (f *fetcher) pullLoop() {
}
}
func (f *fetcher) update() (same bool, err error) {
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
}
if same {
log.Debugln("[Provider] %s's proxies doesn't change", f.Name())
}
if f.onUpdate != nil {
f.onUpdate(elm)
}
log.Infoln("[Provider] %s's proxies update", f.Name())
return
}
func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)
@ -184,5 +198,6 @@ func newFetcher(name string, interval time.Duration, vehicle types.Vehicle, pars
parser: parser,
done: make(chan struct{}, 1),
onUpdate: onUpdate,
interval: interval,
}
}

View file

@ -27,6 +27,7 @@ type fetcher struct {
hash [16]byte
parser parser
onUpdate func(interface{}) error
interval time.Duration
}
func (f *fetcher) Name() string {
@ -55,6 +56,12 @@ func (f *fetcher) Initial() (interface{}, error) {
modTime := stat.ModTime()
f.updatedAt = &modTime
hasLocal = true
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
defer func() {
log.Infoln("[Provider] %s's rules not updated for a long time", f.Name())
go f.update()
}()
}
} else {
buf, err = f.vehicle.Read()
}
@ -144,6 +151,7 @@ func newFetcher(name string, interval time.Duration, vehicle P.Vehicle, parser p
parser: parser,
done: make(chan struct{}, 1),
onUpdate: onUpdate,
interval: interval,
}
}
@ -163,28 +171,34 @@ func (f *fetcher) pullLoop() {
for {
select {
case <-f.ticker.C:
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
same, err := f.update()
if same || err != nil {
continue
}
if same {
log.Debugln("[Provider] %s's rules doesn't change", f.Name())
continue
}
log.Infoln("[Provider] %s's rules update", f.Name())
if f.onUpdate != nil {
err := f.onUpdate(elm)
if err != nil {
log.Infoln("[Provider] %s update failed", f.Name())
}
}
case <-f.done:
f.ticker.Stop()
return
}
}
}
func (f *fetcher) update() (same bool, err error) {
elm, same, err := f.Update()
if err != nil {
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
}
if same {
log.Debugln("[Provider] %s's rules doesn't change", f.Name())
}
log.Infoln("[Provider] %s's rules update", f.Name())
if f.onUpdate != nil {
err := f.onUpdate(elm)
if err != nil {
log.Infoln("[Provider] %s update failed", f.Name())
}
}
return
}