From 2e23e882bb87675332c9a0e15a20edb956248d2f Mon Sep 17 00:00:00 2001 From: d1slike Date: Fri, 3 May 2024 18:24:07 +0300 Subject: [PATCH] Implement lifecycle --- assembly/locator.go | 12 ++--- main.go | 2 +- migrations/20240503120009_init.sql | 3 ++ service/startup/service.go | 80 ++++++++++++++++++++++++++---- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index 55e9d26..325304b 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -28,9 +28,9 @@ func NewLocator(logger log.Logger, db db.DB) Locator { } type Config struct { - grpcMux *grpc.Mux - httpMux http.Handler - etpSrv *etp.Server + GrpcMux *grpc.Mux + HttpMux http.Handler + EtpSrv *etp.Server } func (l Locator) Config() Config { @@ -49,8 +49,8 @@ func (l Locator) Config() Config { httpMux := routes.HttpHandler(etpSrv) return Config{ - grpcMux: grpcMux, - etpSrv: etpSrv, - httpMux: httpMux, + GrpcMux: grpcMux, + EtpSrv: etpSrv, + HttpMux: httpMux, } } diff --git a/main.go b/main.go index caa7d2f..8e77fa4 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ func main() { startup := startup.New(boot) app.AddRunners(startup) - app.AddClosers(startup) + app.AddClosers(startup.Closers()...) shutdown.On(func() { logger.Info(app.Context(), "starting shutdown") diff --git a/migrations/20240503120009_init.sql b/migrations/20240503120009_init.sql index 58befe1..f821fbf 100644 --- a/migrations/20240503120009_init.sql +++ b/migrations/20240503120009_init.sql @@ -53,3 +53,6 @@ create index IX_isp_config_service__config_schema__module_id on isp_config_servi -- +goose Down drop table isp_config_service__module; +drop table isp_config_service__config; +drop table isp_config_service__config_history; +drop table isp_config_service__config_schema; diff --git a/service/startup/service.go b/service/startup/service.go index a4ee2a3..e673540 100644 --- a/service/startup/service.go +++ b/service/startup/service.go @@ -7,11 +7,16 @@ import ( "github.com/pkg/errors" "github.com/pressly/goose/v3" + "github.com/txix-open/etp/v3" + "github.com/txix-open/isp-kit/app" "github.com/txix-open/isp-kit/bootstrap" "github.com/txix-open/isp-kit/dbx/migration" + "github.com/txix-open/isp-kit/grpc" + "github.com/txix-open/isp-kit/http" "github.com/txix-open/isp-kit/http/httpclix" "github.com/txix-open/isp-kit/log" "isp-config-service/assembly" + "isp-config-service/conf" "isp-config-service/service/rqlite" "isp-config-service/service/rqlite/db" "isp-config-service/service/rqlite/goose_store" @@ -22,22 +27,36 @@ const ( ) type Service struct { - boot *bootstrap.Bootstrap - logger log.Logger - rqlite *rqlite.Rqlite + boot *bootstrap.Bootstrap + rqlite *rqlite.Rqlite + grpcSrv *grpc.Server + httpSrv *http.Server + logger log.Logger + + //initialized in Run + etpSrv *etp.Server } func New(boot *bootstrap.Bootstrap) Service { rqlite := rqlite.New(boot.App.Config()) return Service{ - boot: boot, - logger: boot.App.Logger(), - rqlite: rqlite, + boot: boot, + rqlite: rqlite, + logger: boot.App.Logger(), + httpSrv: http.NewServer(boot.App.Logger()), + grpcSrv: grpc.DefaultServer(), } } func (s Service) Run(ctx context.Context) error { + localConfig := conf.Local{} + err := s.boot.App.Config().Read(&localConfig) + if err != nil { + return errors.WithMessage(err, "read local config") + } + go func() { + s.logger.Debug(ctx, "running embedded rqlite...") err := s.rqlite.Run(ctx) if err != nil { s.boot.Fatal(errors.WithMessage(err, "run embedded rqlite")) @@ -46,7 +65,7 @@ func (s Service) Run(ctx context.Context) error { time.Sleep(1 * time.Second) //optimistically wait for store initialization s.logger.Debug(ctx, fmt.Sprintf("waiting for cluster startup for %s...", waitForLeaderTimeout)) - err := s.rqlite.WaitForLeader(waitForLeaderTimeout) + err = s.rqlite.WaitForLeader(waitForLeaderTimeout) if err != nil { return errors.WithMessage(err, "wait for leader") } @@ -66,14 +85,53 @@ func (s Service) Run(ctx context.Context) error { return errors.WithMessage(err, "dial to embedded rqlite") } - _ = assembly.NewLocator(s.logger, db) + config := assembly.NewLocator(s.logger, db).Config() + s.etpSrv = config.EtpSrv + s.grpcSrv.Upgrade(config.GrpcMux) + s.httpSrv.Upgrade(config.HttpMux) + + go func() { + s.logger.Debug(ctx, fmt.Sprintf("starting grpc server on %s", s.boot.BindingAddress)) + err := s.grpcSrv.ListenAndServe(s.boot.BindingAddress) + if err != nil { + s.boot.Fatal(errors.WithMessage(err, "start grpc server")) + } + }() + + go func() { + httpPort := localConfig.ConfigServiceAddress.Port + s.logger.Debug(ctx, fmt.Sprintf("starting http server on 0.0.0.0:%s", httpPort)) + err := s.httpSrv.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", httpPort)) + if err != nil { + s.boot.Fatal(errors.WithMessage(err, "start http server")) + } + }() + time.Sleep(1 * time.Second) return nil } -func (s Service) Close() error { - _ = s.rqlite.Close() - return nil +func (s Service) Closers() []app.Closer { + return []app.Closer{ + app.CloserFunc(func() error { + s.grpcSrv.Shutdown() + return nil + }), + app.CloserFunc(func() error { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + return s.httpSrv.Shutdown(ctx) + }), + app.CloserFunc(func() error { + if s.etpSrv == nil { + return nil + } + s.etpSrv.OnDisconnect(nil) + s.etpSrv.Shutdown() + return nil + }), + s.rqlite, + } } func (s Service) leaderStartup(ctx context.Context) error {