更新UI
This commit is contained in:
@@ -134,7 +134,7 @@ func (s *Service) Stop() {
|
||||
}
|
||||
|
||||
func (s *Service) loop() {
|
||||
ticker := time.NewTicker(time.Duration(s.cfg.SourceCheckSeconds) * time.Second)
|
||||
ticker := time.NewTicker(20 * time.Second)
|
||||
defer ticker.Stop()
|
||||
s.CheckDue(context.Background())
|
||||
for {
|
||||
@@ -269,6 +269,7 @@ func (s *Service) Catalog(includeHidden bool) (map[string]any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
histories, _ := s.store.SourceHealthHistory(sourceIDs(items), 16)
|
||||
categories := map[string]map[string]any{}
|
||||
for _, item := range items {
|
||||
cat, ok := categories[item.CategoryID]
|
||||
@@ -319,6 +320,7 @@ func (s *Service) Catalog(includeHidden bool) (map[string]any, error) {
|
||||
"lastError": item.LastError,
|
||||
"consecutiveFailure": item.ConsecutiveFailure,
|
||||
"meta": parseHealthMeta(item.LastError),
|
||||
"history": histories[item.ID],
|
||||
},
|
||||
}
|
||||
applyResolvedFields(sub, item.LastError)
|
||||
@@ -340,6 +342,7 @@ func (s *Service) Endpoints(includeHidden bool) ([]map[string]any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
histories, _ := s.store.SourceHealthHistory(sourceIDs(items), 16)
|
||||
out := []map[string]any{}
|
||||
for _, item := range items {
|
||||
var formats []string
|
||||
@@ -377,6 +380,7 @@ func (s *Service) Endpoints(includeHidden bool) ([]map[string]any, error) {
|
||||
"last_error": item.LastError,
|
||||
"consecutiveFailure": item.ConsecutiveFailure,
|
||||
"meta": parseHealthMeta(item.LastError),
|
||||
"history": histories[item.ID],
|
||||
},
|
||||
}
|
||||
applyResolvedFields(endpoint, item.LastError)
|
||||
@@ -385,23 +389,48 @@ func (s *Service) Endpoints(includeHidden bool) ([]map[string]any, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func sourceIDs(items []db.Source) []int64 {
|
||||
out := make([]int64, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item.ID > 0 {
|
||||
out = append(out, item.ID)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Service) CheckDue(ctx context.Context) {
|
||||
items, err := s.store.ListSources(true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
enabled := make([]db.Source, 0, len(items))
|
||||
for _, item := range items {
|
||||
if !item.Enabled {
|
||||
continue
|
||||
}
|
||||
if item.LastCheckedAt != "" {
|
||||
if last, err := time.Parse(time.RFC3339, item.LastCheckedAt); err == nil && now.Sub(last) < time.Duration(item.CheckIntervalSec)*time.Second {
|
||||
continue
|
||||
}
|
||||
}
|
||||
_ = s.CheckOne(ctx, item)
|
||||
enabled = append(enabled, item)
|
||||
}
|
||||
if len(enabled) == 0 {
|
||||
return
|
||||
}
|
||||
const concurrency = 4
|
||||
work := make(chan db.Source)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < concurrency; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for item := range work {
|
||||
_ = s.CheckOne(ctx, item)
|
||||
}
|
||||
}()
|
||||
}
|
||||
for _, item := range enabled {
|
||||
work <- item
|
||||
}
|
||||
close(work)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (s *Service) QueueCheckAll() CheckJob {
|
||||
|
||||
Reference in New Issue
Block a user