SetRepository sets the repository (nil is allowed and indicates server that is not connected to the repository).
(ctx context.Context, rep repo.Repository)
| 600 | // SetRepository sets the repository (nil is allowed and indicates server that is not |
| 601 | // connected to the repository). |
| 602 | func (s *Server) SetRepository(ctx context.Context, rep repo.Repository) error { |
| 603 | s.serverMutex.Lock() |
| 604 | defer s.serverMutex.Unlock() |
| 605 | |
| 606 | if s.rep == rep { |
| 607 | // nothing to do |
| 608 | return nil |
| 609 | } |
| 610 | |
| 611 | if s.rep != nil { |
| 612 | // stop previous scheduler asynchronously to avoid deadlock when |
| 613 | // scheduler is inside s.getSchedulerItems which needs a lock, which we're holding right now. |
| 614 | go s.sched.Stop() |
| 615 | |
| 616 | s.sched = nil |
| 617 | |
| 618 | s.unmountAllLocked(ctx) |
| 619 | |
| 620 | // close previous source managers |
| 621 | userLog(ctx).Debug("stopping all source managers") |
| 622 | s.stopAllSourceManagersLocked(ctx) |
| 623 | userLog(ctx).Debug("stopped all source managers") |
| 624 | |
| 625 | if err := s.rep.Close(ctx); err != nil { |
| 626 | return errors.Wrap(err, "unable to close previous repository") |
| 627 | } |
| 628 | |
| 629 | // stop maintenance manager |
| 630 | if s.maint != nil { |
| 631 | s.maint.stop(ctx) |
| 632 | s.maint = nil |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | s.rep = rep |
| 637 | if s.rep == nil { |
| 638 | return nil |
| 639 | } |
| 640 | |
| 641 | if err := s.syncSourcesLocked(ctx); err != nil { |
| 642 | s.stopAllSourceManagersLocked(ctx) |
| 643 | s.rep = nil |
| 644 | |
| 645 | return err |
| 646 | } |
| 647 | |
| 648 | s.maint = maybeStartMaintenanceManager(ctx, s.rep, s, s.options.MinMaintenanceInterval) |
| 649 | |
| 650 | s.sched = scheduler.Start(context.WithoutCancel(ctx), s.getSchedulerItems, scheduler.Options{ |
| 651 | TimeNow: clock.Now, |
| 652 | Debug: s.options.DebugScheduler, |
| 653 | RefreshChannel: s.schedulerRefresh, |
| 654 | }) |
| 655 | |
| 656 | return nil |
| 657 | } |
| 658 | |
| 659 | // +checklocks:s.serverMutex |
no test coverage detected