fix: 初验针对修改
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/bsm-sdk/core/middleware"
|
||||
@@ -16,12 +22,33 @@ import (
|
||||
const ServiceKey = "Files"
|
||||
|
||||
func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
config.New(ServiceKey)
|
||||
impl.NewImpl()
|
||||
if err := models.InitData(); err != nil {
|
||||
if err := models.RequireSchemaVersion(impl.DBService, "files"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
jobs.StartPendingUploadCleanup()
|
||||
if err := models.RequireStorageProvider(impl.DBService, impl.StorageService.Provider()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := jobs.StartPendingUploadCleanup(ctx); err != nil {
|
||||
if closeErr := impl.Close(); closeErr != nil {
|
||||
panic(errors.Join(err, closeErr))
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
if err := jobs.StartIntegrity(ctx); err != nil {
|
||||
stop()
|
||||
waitCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
waitErr := jobs.Wait(waitCtx)
|
||||
cancel()
|
||||
if closeErr := impl.Close(); closeErr != nil {
|
||||
panic(errors.Join(err, waitErr, closeErr))
|
||||
}
|
||||
panic(errors.Join(err, waitErr))
|
||||
}
|
||||
|
||||
app := gin.Default()
|
||||
middleware.Mode(app)
|
||||
@@ -30,7 +57,30 @@ func main() {
|
||||
app.HEAD("/", infra.Health)
|
||||
routers.Register(ServiceKey, app)
|
||||
|
||||
if err := app.Run(fmt.Sprintf(":%s", config.Spec.Port)); err != nil {
|
||||
serveErr := serve(ctx, app)
|
||||
stop()
|
||||
waitCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
waitErr := jobs.Wait(waitCtx)
|
||||
cancel()
|
||||
closeErr := impl.Close()
|
||||
if err := errors.Join(serveErr, waitErr, closeErr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func serve(ctx context.Context, app http.Handler) error {
|
||||
server := &http.Server{Addr: config.Spec.Addr, Handler: app, ReadHeaderTimeout: 10 * time.Second}
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- server.ListenAndServe() }()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
return server.Shutdown(shutdownCtx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user