fix: 恢复MediaMTX按需媒体路径 (#54)
This commit is contained in:
@@ -24,6 +24,32 @@ func NewService(store Store, process mediamtx.Process, controller mediamtx.Contr
|
||||
return &Service{store: store, process: process, controller: controller, profile: admission.VerifiedProfile, credential: device.ReadRTSPCredential, now: time.Now}
|
||||
}
|
||||
|
||||
// Restore recreates desired routes after MediaMTX starts with its base config.
|
||||
// Individual route failures are persisted as safe business states and do not
|
||||
// prevent the Sense management plane from starting.
|
||||
func (s *Service) Restore(ctx context.Context) error {
|
||||
items, err := s.store.List(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, route := range items {
|
||||
if route.Desired != "running" {
|
||||
continue
|
||||
}
|
||||
if _, reconcileErr := s.Reconcile(ctx, identity.Principal{}, route.ID); reconcileErr != nil {
|
||||
route.Actual = "apply_failed"
|
||||
route.Detail = "媒体路径恢复失败,请检查视频服务"
|
||||
route.Readers = 0
|
||||
route.Version++
|
||||
route.UpdatedAt = s.now().UTC()
|
||||
if saveErr := s.store.Save(ctx, route); saveErr != nil {
|
||||
return saveErr
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) ConfigureProfiles(ctx context.Context, actor identity.Principal, result admission.Result) admission.MediaOutcome {
|
||||
configured := 0
|
||||
ready := 0
|
||||
@@ -108,6 +134,10 @@ func (s *Service) Reconcile(ctx context.Context, actor identity.Principal, id st
|
||||
if err != nil {
|
||||
route.Actual = "unconverged"
|
||||
route.Detail = "尚未取得媒体状态"
|
||||
} else if !status.Exists {
|
||||
route.Actual = "apply_failed"
|
||||
route.Detail = "媒体路径不存在,请重新对账"
|
||||
route.Readers = 0
|
||||
} else if status.Ready {
|
||||
route.Actual = "ready"
|
||||
route.Detail = "上游拉流正常"
|
||||
@@ -142,7 +172,10 @@ func (s *Service) Refresh(ctx context.Context, id string) (Route, error) {
|
||||
}
|
||||
actual := "waiting"
|
||||
detail := "等待播放器连接并按需拉流"
|
||||
if status.Ready {
|
||||
if !status.Exists {
|
||||
actual = "apply_failed"
|
||||
detail = "媒体路径不存在,请重新对账"
|
||||
} else if status.Ready {
|
||||
actual = "ready"
|
||||
detail = "上游拉流正常"
|
||||
}
|
||||
|
||||
@@ -53,8 +53,9 @@ func (f fakeController) Status(context.Context, string) (mediamtx.PathStatus, er
|
||||
return mediamtx.PathStatus{}, errors.New("timeout")
|
||||
}
|
||||
if !f.status.Ready {
|
||||
return mediamtx.PathStatus{Ready: true, Readers: 2}, nil
|
||||
return mediamtx.PathStatus{Exists: true, Ready: true, Readers: 2}, nil
|
||||
}
|
||||
f.status.Exists = true
|
||||
return f.status, nil
|
||||
}
|
||||
|
||||
@@ -155,7 +156,7 @@ func TestRefreshTracksOnDemandReaderWithoutReapplyingRoute(t *testing.T) {
|
||||
if err := store.Save(context.Background(), route); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := NewService(store, &fakeProcess{}, refreshController{status: mediamtx.PathStatus{Name: route.Path, Ready: true, Readers: 1}})
|
||||
service := NewService(store, &fakeProcess{}, refreshController{status: mediamtx.PathStatus{Name: route.Path, Exists: true, Ready: true, Readers: 1}})
|
||||
result, err := service.Refresh(context.Background(), route.ID)
|
||||
if err != nil || result.Actual != "ready" || result.Readers != 1 || result.Detail != "上游拉流正常" {
|
||||
t.Fatalf("result=%#v err=%v", result, err)
|
||||
@@ -166,4 +167,29 @@ func TestRefreshTracksOnDemandReaderWithoutReapplyingRoute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshReportsMissingPathInsteadOfWaiting(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
route := Route{ID: "device:main", Path: "sense_device_main", Desired: "running", Actual: "waiting", Version: 1}
|
||||
if err := store.Save(context.Background(), route); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := NewService(store, &fakeProcess{}, refreshController{status: mediamtx.PathStatus{Name: route.Path, Exists: false}})
|
||||
result, err := service.Refresh(context.Background(), route.ID)
|
||||
if err != nil || result.Actual != "apply_failed" || !strings.Contains(result.Detail, "不存在") {
|
||||
t.Fatalf("result=%#v err=%v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreReconcilesDesiredRoutes(t *testing.T) {
|
||||
process := &fakeProcess{}
|
||||
service, route := preparedService(t, process, fakeController{})
|
||||
if err := service.Restore(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
restored, err := service.store.Get(context.Background(), route.ID)
|
||||
if err != nil || !process.State().Running || restored.Actual != "ready" {
|
||||
t.Fatalf("restored=%#v process=%#v err=%v", restored, process.State(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func rtspReady() rtsp.Result { return rtsp.Result{Status: "ready"} }
|
||||
|
||||
Reference in New Issue
Block a user