@@ -35,6 +35,7 @@ import (
35
35
"path/filepath"
36
36
"strconv"
37
37
"strings"
38
+ "sync"
38
39
"time"
39
40
40
41
"github.com/go-logr/glogr"
@@ -307,7 +308,12 @@ func main() {
307
308
308
309
// This is a dumb liveliness check endpoint. Currently this checks
309
310
// nothing and will always return 200 if the process is live.
310
- mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {})
311
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
312
+ if ! getRepoReady () {
313
+ http .Error (w , "repo is not ready" , http .StatusServiceUnavailable )
314
+ }
315
+ // Otherwise success
316
+ })
311
317
http .Serve (ln , mux )
312
318
}()
313
319
}
@@ -454,6 +460,22 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) (string, e
454
460
return oldWorktreePath , nil
455
461
}
456
462
463
+ // repoReady indicates that the repo has been cloned and synced.
464
+ var readyLock sync.Mutex
465
+ var repoReady = false
466
+
467
+ func getRepoReady () bool {
468
+ readyLock .Lock ()
469
+ defer readyLock .Unlock ()
470
+ return repoReady
471
+ }
472
+
473
+ func setRepoReady () {
474
+ readyLock .Lock ()
475
+ defer readyLock .Unlock ()
476
+ repoReady = true
477
+ }
478
+
457
479
// addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree
458
480
func addWorktreeAndSwap (ctx context.Context , gitRoot , dest , branch , rev string , depth int , hash string ) error {
459
481
log .V (0 ).Info ("syncing git" , "rev" , rev , "hash" , hash )
@@ -525,9 +547,12 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string,
525
547
}
526
548
527
549
// Flip the symlink.
528
- if oldWorktree , err := updateSymlink (ctx , gitRoot , dest , worktreePath ); err != nil {
550
+ oldWorktree , err := updateSymlink (ctx , gitRoot , dest , worktreePath )
551
+ if err != nil {
529
552
return err
530
- } else if oldWorktree != "" {
553
+ }
554
+ setRepoReady ()
555
+ if oldWorktree != "" {
531
556
// Clean up previous worktree
532
557
log .V (1 ).Info ("removing old worktree" , "path" , oldWorktree )
533
558
if err := os .RemoveAll (oldWorktree ); err != nil {
0 commit comments