diff --git a/.env.dist b/.env.dist index 84c5d45..03b6ce0 100644 --- a/.env.dist +++ b/.env.dist @@ -1,4 +1,16 @@ SERVER_ADDR=:80 + +APP_NAME=basket-svc +APP_DOMAIN=basket-svc +APP_PATH_PREFIX=/basket +APP_KV_NAMESPACE=dev.egommerce/service/basket-svc + +LOGGER_ADDR=api-logger:24224 +REGISTRY_ADDR=api-registry:8500 DATABASE_URL=postgres://postgres:12345678@postgres-db:5432/egommerce +CACHE_ADDR=api-cache:6379 +CACHE_PASSWORD=12345678 MONGODB_URL=mongodb://mongodb:12345678@mongo-db:27017 -EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672 \ No newline at end of file +EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672 + + diff --git a/src/cmd/worker/main.go b/src/cmd/worker/main.go index 6bc2717..e9d9fd3 100644 --- a/src/cmd/worker/main.go +++ b/src/cmd/worker/main.go @@ -67,8 +67,7 @@ func main() { } go func(consul *discovery.Service) { - interval := time.Second * 3 - ticker := time.NewTicker(interval) + ticker := time.NewTicker(time.Second * 15) for range ticker.C { updateKVConfig(consul, c) // FIXME: duplicated in internal/app/server/server.go } @@ -76,7 +75,7 @@ func main() { // db conn dbConn, err := database.Connect(c.DbURL) - if err != nil { // fixme: add wait-for-db... + if err != nil { logger.Log("Failed to connect to Database server: %v\n", err) os.Exit(1) } @@ -174,26 +173,23 @@ func main() { qty := int(data["quantity"].(float64)) basket, err := ui.AddProductToBasket(basketSrv, productID, qty, basketID, reqID) - if err != nil { - fmt.Println("worker error: ", err) - logger.Log("%s error: %s", event.EVENT_PRODUCT_ADDED_TO_BASKET, err.Error()) - d.Reject(false) // FIXME: or Nack? how to handle erros in queue... - break + if err == nil { + logger.Log("Product #%s added to basket #%s. ReqID: #%s", productID, basket.ID, reqID) } - - logger.Log("Product #%s added to basket #%s. ReqID: #%s", productID, basket.ID, reqID) case strings.Contains(eName, event.EVENT_PRODUCT_REMOVED_FROM_BASKET): productID := int(data["product_id"].(float64)) qty := int(data["quantity"].(float64)) basket, err := ui.RemoveProductFromBasket(basketSrv, productID, qty, basketID, reqID) - if err != nil { - logger.Log("%s error: %s", event.EVENT_PRODUCT_ADDED_TO_BASKET, err.Error()) - d.Reject(false) // FIXME: or Nack? how to handle erros in queue... - break + if err == nil { + logger.Log("Product #%s removed from basket #%s. ReqID: #%s", productID, basket.ID, reqID) } + } - logger.Log("Product #%s removed from basket #%s. ReqID: #%s", productID, basket.ID, reqID) + if err != nil { + logger.Log("%s error: %s", eName, err.Error()) + d.Reject(false) // FIXME: or Nack? how to handle erros in queue... + return } logger.Log("ACK: %s", eName) diff --git a/src/go.sum b/src/go.sum index 505f01a..af6828c 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,8 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= git.pbiernat.dev/egommerce/api-entities v0.0.26 h1:Avz02GINwuYWOjw1fmZIJ3QgGEIz3a5vRQZNaxxUQIk= git.pbiernat.dev/egommerce/api-entities v0.0.26/go.mod h1:+BXvUcr6Cr6QNpJsW8BUfe1vVILdWDADNE0e3u0lNvU= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.135 h1:qOa6MB6d2/lr0t9c3WWP84rf/T57PNYgizTmuNCDws8= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.135/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= git.pbiernat.dev/egommerce/go-api-pkg v0.0.136 h1:SzJRAkqJKdng/3d0V7o/R0yGh7QaZynPBn/P++on9RA= git.pbiernat.dev/egommerce/go-api-pkg v0.0.136/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/src/internal/app/server/config.go b/src/internal/app/server/config.go index 5213283..1940c7b 100644 --- a/src/internal/app/server/config.go +++ b/src/internal/app/server/config.go @@ -20,9 +20,10 @@ type Config struct { EventBusURL string `json:"eventbus_url"` EventBusExchange string `json:"eventbus_exchange"` EventBusQueue string `json:"eventbus_queue"` - HttpReadTimeout int `json:"http_read_timeout"` - HttpWriteTimeout int `json:"http_write_timeout"` - HttpIdleTimeout int `json:"http_idle_timeout"` + + HttpReadTimeout int `json:"http_read_timeout"` + HttpWriteTimeout int `json:"http_write_timeout"` + HttpIdleTimeout int `json:"http_idle_timeout"` // Fields with json mapping are available trough ConsulKV } diff --git a/src/internal/app/server/server.go b/src/internal/app/server/server.go index ef406e9..9fac46c 100644 --- a/src/internal/app/server/server.go +++ b/src/internal/app/server/server.go @@ -77,8 +77,7 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool, cache *re }(s) go func(s *Server) { // Server metadata cache updater - interval := time.Second * 5 - ticker := time.NewTicker(interval) + ticker := time.NewTicker(time.Second * 5) for range ticker.C { s.cacheMetadata() } @@ -90,10 +89,10 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool, cache *re return s } -func (s *Server) Start() { - err := s.Listen(s.addr) - s.log.Log("Starting error: %v", err) -} +// func (s *Server) Start() { +// err := s.Listen(s.addr) +// s.log.Log("Starting error: %v", err) +// } func (s *Server) StartWithGracefulShutdown(forever chan struct{}) { go func() { @@ -147,7 +146,7 @@ func (s *Server) updateKVConfig() { // FIXME: duplicated in cmd/worker/main.go func (s *Server) cacheMetadata() { ctx := context.Background() - key, address := "internal__"+s.conf.AppName+"__ips", s.conf.AppID // FIXME: key name + key, address := s.getMetadataIPsKey(), s.conf.AppID pos := s.cache.LPos(ctx, key, address, redis.LPosArgs{}).Val() if pos >= 0 { @@ -159,14 +158,17 @@ func (s *Server) cacheMetadata() { func (s *Server) clearMetadataCache() { ctx := context.Background() - key, address := "internal__"+s.conf.AppName+"__ips", s.conf.AppID // FIXME: key name + key, address := s.getMetadataIPsKey(), s.conf.AppID s.cache.LRem(ctx, key, 0, address) } +func (s *Server) getMetadataIPsKey() string { + return "internal__" + s.conf.AppName + "__ips" +} + func (s *Server) gracefulShutdown() error { - s.log.Log("Server is going down...") - s.log.Log("Unregistering service: %s", s.discovery.GetID()) + s.log.Log("Server is going down... Unregistering service: %s", s.discovery.GetID()) s.discovery.Unregister() s.clearMetadataCache() diff --git a/src/internal/app/service/basket.go b/src/internal/app/service/basket.go index 57c8e37..4c26160 100644 --- a/src/internal/app/service/basket.go +++ b/src/internal/app/service/basket.go @@ -94,6 +94,8 @@ func (s *BasketService) AddItem(ctx context.Context, itemID int, basketID string return err } + // update basket updated_at field... + return nil } @@ -103,6 +105,8 @@ func (s *BasketService) RemoveItem(ctx context.Context, itemID int, basketID str return err } + // update basket updated_at field... + return nil } @@ -120,6 +124,8 @@ func (s *BasketService) UpdateItem(ctx context.Context, item *model.BasketItemMo return err } + // update basket updated_at field... + return nil }