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)
| 882 | // and initializes the repository when done. The initializer may return nil to indicate there |
| 883 | // is no repository configured. |
| 884 | func (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. |
nothing calls this directly
no test coverage detected