Implement lifecycle

This commit is contained in:
d1slike committed 2024-05-03 18:24:07 +03:00
1 parent 4e96b7a930
commit 2e23e882bb
4 files changed
+79 -18

No files matched your search

+6 -6
View File
@@ -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,
}
}
+1 -1
View File
@@ -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")
+3
View File
@@ -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;
+69 -11
View File
@@ -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 {