MCPcopy Index your code
hub / github.com/kopia/kopia / InitRepositoryAsync

Method InitRepositoryAsync

internal/server/server.go:884–939  ·  view source on GitHub ↗

InitRepositoryAsync starts a task that initializes the repository by invoking the provided callback and initializes the repository when done. The initializer may return nil to indicate there is no repository configured.

(ctx context.Context, mode string, initializer InitRepositoryFunc, wait bool)

Source from the content-addressed store, hash-verified

882// and initializes the repository when done. The initializer may return nil to indicate there
883// is no repository configured.
884func (s *Server) InitRepositoryAsync(ctx context.Context, mode string, initializer InitRepositoryFunc, wait bool) (string, error) {
885 var wg sync.WaitGroup
886
887 var taskID string
888
889 wg.Add(1)
890
891 //nolint:errcheck
892 go s.taskmgr.Run(ctx, "Repository", mode, func(ctx context.Context, ctrl uitask.Controller) error {
893 // we're still holding a lock, until wg.Done(), so no lock around this is needed.
894 taskID = ctrl.CurrentTaskID()
895 s.setInitRepositoryTaskID(taskID)
896 wg.Done()
897
898 defer s.setInitRepositoryTaskID("")
899
900 cctx, cancel := context.WithCancel(ctx)
901 defer cancel()
902
903 ctrl.OnCancel(func() {
904 cancel()
905 })
906
907 // run initializer in cancelable context.
908 rep, err := initializer(cctx)
909
910 if cctx.Err() != nil {
911 // context canceled
912 return errors.New("operation has been canceled")
913 }
914
915 if err != nil {
916 return errors.Wrap(err, "error opening repository")
917 }
918
919 if rep == nil {
920 userLog(ctx).Info("Repository not configured.")
921 }
922
923 if err = s.SetRepository(ctx, rep); err != nil {
924 return errors.Wrap(err, "error connecting to repository")
925 }
926
927 return nil
928 })
929
930 wg.Wait()
931
932 if wait {
933 if ti, ok := s.taskmgr.WaitForTask(ctx, taskID, -1); ok {
934 return taskID, ti.Error
935 }
936 }
937
938 return taskID, nil
939}
940
941// RetryInitRepository wraps provided initialization function with retries until the context gets canceled.

Callers

nothing calls this directly

Calls 11

SetRepositoryMethod · 0.95
ErrMethod · 0.80
InfoMethod · 0.80
WaitForTaskMethod · 0.80
RunMethod · 0.65
CurrentTaskIDMethod · 0.65
DoneMethod · 0.65
OnCancelMethod · 0.65
WaitMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected