From a4895028b7ed33d3ca5c63af8f0ea5c6d1dfe0c3 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Fri, 30 Jun 2023 16:44:42 +0100 Subject: [PATCH] Run initial command in a go routine Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- commands/watch.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/commands/watch.go b/commands/watch.go index a6ab122a..80012af1 100644 --- a/commands/watch.go +++ b/commands/watch.go @@ -92,17 +92,20 @@ func watchLoop(cmd *cobra.Command, args []string, onChange func(cmd *cobra.Comma bounce := debounce.New(1500 * time.Millisecond) - // An initial build is usually done on first load with - // live reloaders - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - canceller.Set(ctx, cancel) - - if err := onChange(cmd, args, ctx); err != nil { - fmt.Println("Error rebuilding: ", err) - os.Exit(1) - } + go func() { + // An initial build is usually done on first load with + // live reloaders + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + canceller.Set(ctx, cancel) + + if err := onChange(cmd, args, ctx); err != nil { + fmt.Println("Error rebuilding: ", err) + os.Exit(1) + } + }() + log.Printf("[Watch] Started") for { select { case event, ok := <-watcher.Events: @@ -161,11 +164,12 @@ func watchLoop(cmd *cobra.Command, args []string, onChange func(cmd *cobra.Comma } bounce(func() { - log.Printf("[Watch] Cancel previous build") + log.Printf("[Watch] Cancelling") canceller.Cancel() - log.Printf("[Watch] Cancelled previous build") - ctx, cancel = context.WithCancel(context.Background()) + + log.Printf("[Watch] Cancelled") + ctx, cancel := context.WithCancel(context.Background()) canceller.Set(ctx, cancel) // Assign --filter to "" for all functions if we can't determine the