Skip to content

Commit

Permalink
fix: boot函数当start发生错误时不执行stop的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 7, 2024
1 parent 772c1d4 commit b102d4c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/utils/boot/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package boot

import (
"context"
"errors"
"flag"
"fmt"
"github.com/tbxark/sphere/pkg/log"
Expand Down Expand Up @@ -81,22 +82,24 @@ func Run[T any](ver string, conf *T, logConf *log.Options, builder func(*T) (*Ap
}
}()

var errs []error

// Wait for shutdown signal or application error
select {
case <-quit:
log.Debug("Received shutdown signal")
case e := <-errChan:
log.Error("Application error", logfields.Error(e))
return fmt.Errorf("application error: %w", e)
errs = append(errs, e)
}

// Close application
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if e := app.Stop(ctx); e != nil {
return fmt.Errorf("failed to close application: %w", e)
errs = append(errs, e)
}

return nil
return errors.Join(errs...)
}

0 comments on commit b102d4c

Please sign in to comment.