@@ -307,50 +307,60 @@ func (rm ReposModified) ReposModified(modified types.RepoModified) types.Repos {
307307// sync in the background vs block and call s.syncRepo synchronously.
308308func (s * Syncer ) SyncRepo (ctx context.Context , name api.RepoName , background bool ) (repo * types.Repo , err error ) {
309309 logger := s .Logger .With (log .String ("name" , string (name )), log .Bool ("background" , background ))
310+
311+ logger .Debug ("SyncRepo started" )
312+
310313 tr , ctx := trace .New (ctx , "Syncer.SyncRepo" , string (name ))
311314 defer tr .Finish ()
312315
313316 repo , err = s .Store .RepoStore ().GetByName (ctx , name )
314317 if err != nil && ! errcode .IsNotFound (err ) {
315- return nil , err
318+ return nil , errors . Wrapf ( err , "GetByName failed for %q" , name )
316319 }
317320
318321 codehost := extsvc .CodeHostOf (name , extsvc .PublicCodeHosts ... )
319322 if codehost == nil {
320323 if repo != nil {
321324 return repo , nil
322325 }
326+
327+ logger .Debug ("no associated code host found, skipping" )
323328 return nil , & database.RepoNotFoundErr {Name : name }
324329 }
325330
326331 if repo != nil {
327332 // Only public repos can be individually synced on sourcegraph.com
328333 if repo .Private {
334+ logger .Debug ("repo is private, skipping" )
329335 return nil , & database.RepoNotFoundErr {Name : name }
330336 }
331337 // Don't sync the repo if it's been updated in the past 1 minute.
332338 if s .Now ().Sub (repo .UpdatedAt ) < time .Minute {
339+ logger .Debug ("repo updated recently, skipping" )
333340 return repo , nil
334341 }
335342 }
336343
337344 if background && repo != nil {
345+ logger .Debug ("starting background sync in goroutine" )
338346 go func () {
339347 ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Minute )
340348 defer cancel ()
341349
342350 // We don't care about the return value here, but we still want to ensure that
343351 // only one is in flight at a time.
344- _ , err , shared := s .syncGroup .Do (string (name ), func () (any , error ) {
352+ updatedRepo , err , shared := s .syncGroup .Do (string (name ), func () (any , error ) {
345353 return s .syncRepo (ctx , codehost , name , repo )
346354 })
355+ logger .Debug ("syncGroup completed" , log .String ("updatedRepo" , fmt .Sprintf ("%v" , updatedRepo .(* types.Repo ))))
347356 if err != nil {
348357 logger .Error ("SyncRepo" , log .Error (err ), log .Bool ("shared" , shared ))
349358 }
350359 }()
351360 return repo , nil
352361 }
353362
363+ logger .Debug ("starting foreground sync" )
354364 updatedRepo , err , shared := s .syncGroup .Do (string (name ), func () (any , error ) {
355365 return s .syncRepo (ctx , codehost , name , repo )
356366 })
@@ -396,7 +406,7 @@ func (s *Syncer) syncRepo(
396406
397407 src , err := s .Sourcer (ctx , svc )
398408 if err != nil {
399- return nil , err
409+ return nil , errors . Wrap ( err , "failed to retrieve Sourcer" )
400410 }
401411
402412 rg , ok := src .(RepoGetter )
@@ -411,6 +421,7 @@ func (s *Syncer) syncRepo(
411421
412422 if stored != nil {
413423 defer func () {
424+ s .Logger .Debug ("deferred deletable repo check" )
414425 if isDeleteableRepoError (err ) {
415426 err2 := s .Store .DeleteExternalServiceRepo (ctx , svc , stored .ID )
416427 if err2 != nil {
@@ -422,17 +433,19 @@ func (s *Syncer) syncRepo(
422433 log .Error (err2 ),
423434 )
424435 }
436+ s .Logger .Debug ("external service repo deleted" , log .Int32 ("deleted ID" , int32 (stored .ID )))
425437 s .notifyDeleted (ctx , stored .ID )
426438 }
427439 }()
428440 }
429441
430442 repo , err = rg .GetRepo (ctx , path )
431443 if err != nil {
432- return nil , err
444+ return nil , errors . Wrapf ( err , "failed to get repo with path: %q" , path )
433445 }
434446
435447 if repo .Private {
448+ s .Logger .Debug ("repo is private, skipping" )
436449 return nil , & database.RepoNotFoundErr {Name : name }
437450 }
438451
@@ -646,6 +659,7 @@ func (s *Syncer) SyncExternalService(
646659
647660 results := make (chan SourceResult )
648661 go func () {
662+ s .Logger .Debug ("listing repos asynchronously" )
649663 src .ListRepos (ctx , results )
650664 close (results )
651665 }()
@@ -676,6 +690,8 @@ func (s *Syncer) SyncExternalService(
676690 // Insert or update repos as they are sourced. Keep track of what was seen so we
677691 // can remove anything else at the end.
678692 for res := range results {
693+ logger .Debug ("received result" , log .String ("repo" , fmt .Sprintf ("%v" , res )))
694+
679695 if err := progressRecorder (ctx , syncProgress , false ); err != nil {
680696 logger .Warn ("recording sync progress" , log .Error (err ))
681697 }
@@ -839,15 +855,20 @@ func (s *Syncer) sync(ctx context.Context, svc *types.ExternalService, sourced *
839855 observeDiff (d )
840856 // We must commit the transaction before publishing to s.Synced
841857 // so that gitserver finds the repo in the database.
858+
859+ s .Logger .Debug ("committing transaction" )
842860 err = tx .Done (err )
843861 if err != nil {
862+ s .Logger .Warn ("failed to commit transaction" , log .Error (err ))
844863 return
845864 }
846865
847866 if s .Synced != nil && d .Len () > 0 {
848867 select {
849868 case <- ctx .Done ():
869+ s .Logger .Debug ("sync context done" )
850870 case s .Synced <- d :
871+ s .Logger .Debug ("diff synced" )
851872 }
852873 }
853874 }()
@@ -865,6 +886,8 @@ func (s *Syncer) sync(ctx context.Context, svc *types.ExternalService, sourced *
865886
866887 switch len (stored ) {
867888 case 2 : // Existing repo with a naming conflict
889+ s .Logger .Debug ("naming conflict" )
890+
868891 // Pick this sourced repo to own the name by deleting the other repo. If it still exists, it'll have a different
869892 // name when we source it from the same code host, and it will be re-created.
870893 var conflicting , existing * types.Repo
@@ -884,8 +907,10 @@ func (s *Syncer) sync(ctx context.Context, svc *types.ExternalService, sourced *
884907 // We fallthrough to the next case after removing the conflicting repo in order to update
885908 // the winner (i.e. existing). This works because we mutate stored to contain it, which the case expects.
886909 stored = types.Repos {existing }
910+ s .Logger .Debug ("retrieved stored repo, falling through" , log .String ("stored" , fmt .Sprintf ("%v" , stored )))
887911 fallthrough
888912 case 1 : // Existing repo, update.
913+ s .Logger .Debug ("exisitng repo" )
889914 modified := stored [0 ].Update (sourced )
890915 if modified == types .RepoUnmodified {
891916 d .Unmodified = append (d .Unmodified , stored [0 ])
@@ -898,7 +923,9 @@ func (s *Syncer) sync(ctx context.Context, svc *types.ExternalService, sourced *
898923
899924 * sourced = * stored [0 ]
900925 d .Modified = append (d .Modified , RepoModified {Repo : stored [0 ], Modified : modified })
926+ s .Logger .Debug ("appended to modified repos" )
901927 case 0 : // New repo, create.
928+ s .Logger .Debug ("new repo" )
902929 if ! svc .IsSiteOwned () { // enforce user and org repo limits
903930 siteAdded , err := tx .CountNamespacedRepos (ctx , 0 , 0 )
904931 if err != nil {
@@ -931,10 +958,12 @@ func (s *Syncer) sync(ctx context.Context, svc *types.ExternalService, sourced *
931958 }
932959
933960 d .Added = append (d .Added , sourced )
961+ s .Logger .Debug ("appended to added repos" )
934962 default : // Impossible since we have two separate unique constraints on name and external repo spec
935963 panic ("unreachable" )
936964 }
937965
966+ s .Logger .Debug ("completed" )
938967 return d , nil
939968}
940969
0 commit comments