Skip to content

Commit

Permalink
chore: 优化Application封装
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 7, 2024
1 parent 42fecd6 commit 393a286
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 64 deletions.
12 changes: 4 additions & 8 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ func main() {

func newApplication(dash *api.Web, initialize *task.DashInitialize, cleaner *task.ConnectCleaner) *boot.Application {
return boot.NewApplication(
[]boot.Runnable{
dash,
initialize,
},
[]boot.Closeable{
dash,
cleaner,
})
dash,
initialize,
cleaner,
)
}
18 changes: 6 additions & 12 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ func main() {

func newApplication(dash *dash.Web, api *api.Web, docs *docs.Web, initialize *task.DashInitialize, cleaner *task.ConnectCleaner) *boot.Application {
return boot.NewApplication(
[]boot.Runnable{
dash,
api,
docs,
initialize,
},
[]boot.Closeable{
dash,
api,
docs,
cleaner,
})
dash,
api,
docs,
initialize,
cleaner,
)
}
8 changes: 1 addition & 7 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ func main() {
}

func newApplication(app *bot.Bot) *boot.Application {
return boot.NewApplication(
[]boot.Runnable{
app,
},
[]boot.Closeable{
app,
})
return boot.NewApplication(app)
}
11 changes: 4 additions & 7 deletions cmd/dash/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ func main() {

func newApplication(dash *dash.Web, cleaner *task.ConnectCleaner, initDash *task.DashInitialize) *boot.Application {
return boot.NewApplication(
[]boot.Runnable{
initDash,
dash,
},
[]boot.Closeable{
cleaner,
})
initDash,
dash,
cleaner,
)
}
6 changes: 5 additions & 1 deletion internal/biz/task/conn_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func (c *ConnectCleaner) Identifier() string {
return "connect_cleaner"
}

func (c *ConnectCleaner) Close(ctx context.Context) error {
func (c *ConnectCleaner) Start(ctx context.Context) error {
return nil
}

func (c *ConnectCleaner) Stop(ctx context.Context) error {
group := errgroup.Group{}
group.Go(c.db.Close)
return group.Wait()
Expand Down
6 changes: 5 additions & 1 deletion internal/biz/task/dash_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (i *DashInitialize) Identifier() string {
return "initialize"
}

func (i *DashInitialize) Run(ctx context.Context) error {
func (i *DashInitialize) Start(ctx context.Context) error {
key := "did_init"
return dao.WithTxEx(ctx, i.db.Client, func(ctx context.Context, client *ent.Client) error {
exist, err := client.KeyValueStore.Query().Where(keyvaluestore.KeyEQ(key)).Exist(ctx)
Expand All @@ -55,3 +55,7 @@ func (i *DashInitialize) Run(ctx context.Context) error {
return nil
})
}

func (i *DashInitialize) Stop(ctx context.Context) error {
return nil
}
4 changes: 2 additions & 2 deletions internal/server/api/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (w *Web) Identifier() string {
return "api"
}

func (w *Web) Run(ctx context.Context) error {
func (w *Web) Start(ctx context.Context) error {
jwtAuthorizer := jwtauth.NewJwtAuth[authorizer.RBACClaims[int64]](w.config.JWT)

zapLogger := log.ZapLogger().With(logfields.String("module", "api"))
Expand Down Expand Up @@ -70,6 +70,6 @@ func (w *Web) Run(ctx context.Context) error {
return ginx.Start(ctx, w.server, 30*time.Second)
}

func (w *Web) Close(ctx context.Context) error {
func (w *Web) Stop(ctx context.Context) error {
return ginx.Close(ctx, w.server)
}
4 changes: 2 additions & 2 deletions internal/server/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (b *Bot) initBot(t *bot.Bot) error {
return nil
}

func (b *Bot) Run(ctx context.Context) error {
func (b *Bot) Start(ctx context.Context) error {
return b.Bot.Run(ctx, b.initBot)
}

func (b *Bot) Close(ctx context.Context) error {
func (b *Bot) Stop(ctx context.Context) error {
return b.Bot.Close(ctx)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/server/dash/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (w *Web) Identifier() string {
return "dash"
}

func (w *Web) Run(ctx context.Context) error {
func (w *Web) Start(ctx context.Context) error {

jwtAuthorizer := jwtauth.NewJwtAuth[authorizer.RBACClaims[int64]](w.config.AuthJWT)
jwtRefresher := jwtauth.NewJwtAuth[authorizer.RBACClaims[int64]](w.config.RefreshJWT)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (w *Web) Run(ctx context.Context) error {
return ginx.Start(ctx, w.server, 30*time.Second)
}

func (w *Web) Close(ctx context.Context) error {
func (w *Web) Stop(ctx context.Context) error {
return ginx.Close(ctx, w.server)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/service/docs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (w *Web) Identifier() string {
return "docs"
}

func (w *Web) Run(ctx context.Context) error {
func (w *Web) Start(ctx context.Context) error {
engine := gin.Default()
cors.Setup(engine, []string{"*"})

Expand All @@ -62,7 +62,7 @@ func (w *Web) Run(ctx context.Context) error {
return ginx.Start(ctx, w.server, 30*time.Second)
}

func (w *Web) Close(ctx context.Context) error {
func (w *Web) Stop(ctx context.Context) error {
return ginx.Close(ctx, w.server)
}

Expand Down
34 changes: 14 additions & 20 deletions pkg/utils/boot/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,27 @@ import (
"golang.org/x/sync/errgroup"
)

type Runnable interface {
type Task interface {
Identifier() string
Run(ctx context.Context) error
}

type Closeable interface {
Identifier() string
Close(ctx context.Context) error
Start(ctx context.Context) error
Stop(ctx context.Context) error
}

type Application struct {
runner []Runnable
closer []Closeable
tasks []Task
}

func NewApplication(tasks []Runnable, cleaners []Closeable) *Application {
func NewApplication(tasks ...Task) *Application {
return &Application{
runner: tasks,
closer: cleaners,
tasks: tasks,
}
}

func (a *Application) Run(ctx context.Context) error {
wg, ctx := errgroup.WithContext(ctx)
for _, item := range a.runner {
log.Infof("runner %s start", item.Identifier())
runner := item
for _, task := range a.tasks {
log.Infof("runner %s start", task.Identifier())
runner := task
wg.Go(func() error {
defer func() {
if r := recover(); r != nil {
Expand All @@ -44,7 +38,7 @@ func (a *Application) Run(ctx context.Context) error {
)
}
}()
if err := runner.Run(ctx); err != nil {
if err := runner.Start(ctx); err != nil {
log.Errorw(
"runner error",
logfields.String("runner", runner.Identifier()),
Expand All @@ -60,9 +54,9 @@ func (a *Application) Run(ctx context.Context) error {

func (a *Application) Close(ctx context.Context) error {
wg := errgroup.Group{}
for _, item := range a.closer {
log.Infof("closer %s start", item.Identifier())
closer := item
for _, task := range a.tasks {
log.Infof("closer %s start", task.Identifier())
closer := task
wg.Go(func() error {
defer func() {
if r := recover(); r != nil {
Expand All @@ -73,7 +67,7 @@ func (a *Application) Close(ctx context.Context) error {
)
}
}()
if err := closer.Close(ctx); err != nil {
if err := closer.Stop(ctx); err != nil {
log.Errorw(
"closer error",
logfields.String("closer", closer.Identifier()),
Expand Down

0 comments on commit 393a286

Please sign in to comment.