feat(maintenance): add policy executor and config provider
ConfigProvider, PolicyExecutor, DBStatsProvider, scheduler и safety; зависимость robfig/cron/v3. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package maintenance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"evobgp/internal/store"
|
||||
)
|
||||
|
||||
// ConfigProvider caches maintenance policies from store.Backend with hot reload.
|
||||
type ConfigProvider struct {
|
||||
store store.Backend
|
||||
mu sync.RWMutex
|
||||
items []*store.MaintenancePolicy
|
||||
}
|
||||
|
||||
// NewConfigProvider constructs a provider; call Reload before use.
|
||||
func NewConfigProvider(st store.Backend) *ConfigProvider {
|
||||
return &ConfigProvider{store: st}
|
||||
}
|
||||
|
||||
// Reload loads all policies from the database into memory.
|
||||
func (c *ConfigProvider) Reload(ctx context.Context) error {
|
||||
if c == nil || c.store == nil {
|
||||
return nil
|
||||
}
|
||||
_ = ctx
|
||||
items, _, _, err := c.store.ListMaintenancePolicies("", 1000)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cp := make([]*store.MaintenancePolicy, len(items))
|
||||
copy(cp, items)
|
||||
c.mu.Lock()
|
||||
c.items = cp
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Snapshot returns a copy of cached policies.
|
||||
func (c *ConfigProvider) Snapshot() []*store.MaintenancePolicy {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
out := make([]*store.MaintenancePolicy, len(c.items))
|
||||
copy(out, c.items)
|
||||
return out
|
||||
}
|
||||
|
||||
// Get returns one policy by id from cache or store.
|
||||
func (c *ConfigProvider) Get(ctx context.Context, id string) (*store.MaintenancePolicy, error) {
|
||||
if c == nil || c.store == nil {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
c.mu.RLock()
|
||||
for _, p := range c.items {
|
||||
if p.ID == id {
|
||||
cp := *p
|
||||
c.mu.RUnlock()
|
||||
return &cp, nil
|
||||
}
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
return c.store.GetMaintenancePolicy(id)
|
||||
}
|
||||
Reference in New Issue
Block a user