49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
func (s *Projects) withNodeMaintenance(ctx context.Context, operation func() error) error {
|
|
return s.withNodeMaintenanceWait(ctx, 0, operation)
|
|
}
|
|
|
|
func (s *Projects) withNodeMaintenanceWait(ctx context.Context, wait time.Duration, operation func() error) error {
|
|
gate := s.nodeGate(ctx)
|
|
deadline := time.Now().Add(wait)
|
|
for !gate.TryLock() {
|
|
if wait == 0 || !time.Now().Before(deadline) {
|
|
return errors.New("节点正在执行主机操作或环境维护,请稍后重试")
|
|
}
|
|
timer := time.NewTimer(50 * time.Millisecond)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
defer gate.Unlock()
|
|
blocked, err := s.store.RuntimeMutationBlocked(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if blocked {
|
|
return errors.New("请先完成当前节点的运行环境维护任务")
|
|
}
|
|
return operation()
|
|
}
|
|
func (s *Projects) ReconcileNodeIdentities(ctx context.Context) error {
|
|
return s.withNodeMaintenanceWait(ctx, 10*time.Second, func() error { return s.ReconcileIdentities(ctx) })
|
|
}
|
|
func (s *Projects) ApplyHostSubscription(ctx context.Context, id, plan uint64, expiry *time.Time) error {
|
|
return s.withNodeMaintenance(ctx, func() error {
|
|
if err := s.store.SetHostSubscription(ctx, id, plan, expiry); err != nil {
|
|
return err
|
|
}
|
|
return s.ReconcileIdentities(ctx)
|
|
})
|
|
}
|