examples/blog: add cleanup (#845)

* add cleanup
pull/846/head
Windfarer 4 years ago committed by GitHub
parent 46acad3400
commit fc539738f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      examples/blog/cmd/blog/main.go
  2. 2
      examples/blog/cmd/blog/wire.go
  3. 10
      examples/blog/cmd/blog/wire_gen.go
  4. 18
      examples/blog/internal/data/data.go

@ -2,9 +2,10 @@ package main
import ( import (
"flag" "flag"
"os"
"go.opentelemetry.io/otel/exporters/trace/jaeger" "go.opentelemetry.io/otel/exporters/trace/jaeger"
"go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace"
"os"
"github.com/go-kratos/kratos/examples/blog/internal/conf" "github.com/go-kratos/kratos/examples/blog/internal/conf"
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
@ -75,10 +76,11 @@ func main() {
} }
defer flush() defer flush()
app, err := initApp(bc.Server, bc.Data, tp, logger) app, cleanup, err := initApp(bc.Server, bc.Data, tp, logger)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer cleanup()
// start and wait for stop signal // start and wait for stop signal
if err := app.Run(); err != nil { if err := app.Run(); err != nil {

@ -17,6 +17,6 @@ import (
) )
// initApp init kratos application. // initApp init kratos application.
func initApp(*conf.Server, *conf.Data, trace.TracerProvider, log.Logger) (*kratos.App, error) { func initApp(*conf.Server, *conf.Data, trace.TracerProvider, log.Logger) (*kratos.App, func(), error) {
panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp)) panic(wire.Build(server.ProviderSet, data.ProviderSet, biz.ProviderSet, service.ProviderSet, newApp))
} }

@ -19,10 +19,10 @@ import (
// Injectors from wire.go: // Injectors from wire.go:
// initApp init kratos application. // initApp init kratos application.
func initApp(confServer *conf.Server, confData *conf.Data, tracerProvider trace.TracerProvider, logger log.Logger) (*kratos.App, error) { func initApp(confServer *conf.Server, confData *conf.Data, tracerProvider trace.TracerProvider, logger log.Logger) (*kratos.App, func(), error) {
dataData, err := data.NewData(confData, logger) dataData, cleanup, err := data.NewData(confData, logger)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
articleRepo := data.NewArticleRepo(dataData, logger) articleRepo := data.NewArticleRepo(dataData, logger)
articleUsecase := biz.NewArticleUsecase(articleRepo, logger) articleUsecase := biz.NewArticleUsecase(articleRepo, logger)
@ -30,5 +30,7 @@ func initApp(confServer *conf.Server, confData *conf.Data, tracerProvider trace.
httpServer := server.NewHTTPServer(confServer, tracerProvider, blogService) httpServer := server.NewHTTPServer(confServer, tracerProvider, blogService)
grpcServer := server.NewGRPCServer(confServer, tracerProvider, blogService) grpcServer := server.NewGRPCServer(confServer, tracerProvider, blogService)
app := newApp(logger, httpServer, grpcServer) app := newApp(logger, httpServer, grpcServer)
return app, nil return app, func() {
cleanup()
}, nil
} }

@ -2,6 +2,7 @@ package data
import ( import (
"context" "context"
"github.com/go-kratos/kratos/examples/blog/internal/conf" "github.com/go-kratos/kratos/examples/blog/internal/conf"
"github.com/go-kratos/kratos/examples/blog/internal/data/ent" "github.com/go-kratos/kratos/examples/blog/internal/data/ent"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
@ -23,7 +24,7 @@ type Data struct {
} }
// NewData . // NewData .
func NewData(conf *conf.Data, logger log.Logger) (*Data, error) { func NewData(conf *conf.Data, logger log.Logger) (*Data, func(), error) {
log := log.NewHelper("data", logger) log := log.NewHelper("data", logger)
client, err := ent.Open( client, err := ent.Open(
conf.Database.Driver, conf.Database.Driver,
@ -31,12 +32,12 @@ func NewData(conf *conf.Data, logger log.Logger) (*Data, error) {
) )
if err != nil { if err != nil {
log.Errorf("failed opening connection to sqlite: %v", err) log.Errorf("failed opening connection to sqlite: %v", err)
return nil, err return nil, nil, err
} }
// Run the auto migration tool. // Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil { if err := client.Schema.Create(context.Background()); err != nil {
log.Errorf("failed creating schema resources: %v", err) log.Errorf("failed creating schema resources: %v", err)
return nil, err return nil, nil, err
} }
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(&redis.Options{
@ -48,8 +49,17 @@ func NewData(conf *conf.Data, logger log.Logger) (*Data, error) {
ReadTimeout: conf.Redis.ReadTimeout.AsDuration(), ReadTimeout: conf.Redis.ReadTimeout.AsDuration(),
}) })
rdb.AddHook(redisotel.TracingHook{}) rdb.AddHook(redisotel.TracingHook{})
return &Data{ d := &Data{
db: client, db: client,
rdb: rdb, rdb: rdb,
}
return d, func() {
log.Info("message", "closing the data resources")
if err := d.db.Close(); err != nil {
log.Error(err)
}
if err := d.rdb.Close(); err != nil {
log.Error(err)
}
}, nil }, nil
} }

Loading…
Cancel
Save