feat: 使用 errgroup 并发处理 HTTP 服务和作业启动
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
"go.ipao.vip/atom/container"
|
"go.ipao.vip/atom/container"
|
||||||
"go.ipao.vip/atom/contracts"
|
"go.ipao.vip/atom/contracts"
|
||||||
|
|
||||||
|
"github.com/go-pay/errgroup"
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/gofiber/fiber/v3/middleware/favicon"
|
"github.com/gofiber/fiber/v3/middleware/favicon"
|
||||||
"github.com/rogeecn/fabfile"
|
"github.com/rogeecn/fabfile"
|
||||||
@@ -93,7 +94,23 @@ func Serve(cmd *cobra.Command, args []string) error {
|
|||||||
svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin))
|
svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin))
|
||||||
svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat))
|
svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat))
|
||||||
|
|
||||||
return svc.Http.Serve(ctx)
|
var eg errgroup.Group
|
||||||
|
|
||||||
|
eg.Go(func(ctx context.Context) error {
|
||||||
|
// river job
|
||||||
|
if err := svc.Job.Start(ctx); err != nil {
|
||||||
|
log.WithError(err).Error("job start failed")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer svc.Job.StopAndCancel(ctx)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
eg.Go(func(ctx context.Context) error {
|
||||||
|
return svc.Http.Serve(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
return eg.Wait()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user