This commit is contained in:
parent
9c74b911c3
commit
f66662111e
12
.env.dist
12
.env.dist
@ -1,4 +1,16 @@
|
|||||||
SERVER_ADDR=:80
|
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
|
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
|
MONGODB_URL=mongodb://mongodb:12345678@mongo-db:27017
|
||||||
EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672
|
EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672
|
||||||
|
|
||||||
|
|
||||||
|
@ -67,8 +67,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func(consul *discovery.Service) {
|
go func(consul *discovery.Service) {
|
||||||
interval := time.Second * 3
|
ticker := time.NewTicker(time.Second * 15)
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
updateKVConfig(consul, c) // FIXME: duplicated in internal/app/server/server.go
|
updateKVConfig(consul, c) // FIXME: duplicated in internal/app/server/server.go
|
||||||
}
|
}
|
||||||
@ -76,7 +75,7 @@ func main() {
|
|||||||
|
|
||||||
// db conn
|
// db conn
|
||||||
dbConn, err := database.Connect(c.DbURL)
|
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)
|
logger.Log("Failed to connect to Database server: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -174,26 +173,23 @@ func main() {
|
|||||||
qty := int(data["quantity"].(float64))
|
qty := int(data["quantity"].(float64))
|
||||||
|
|
||||||
basket, err := ui.AddProductToBasket(basketSrv, productID, qty, basketID, reqID)
|
basket, err := ui.AddProductToBasket(basketSrv, productID, qty, basketID, reqID)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
fmt.Println("worker error: ", err)
|
logger.Log("Product #%s added to basket #%s. ReqID: #%s", productID, basket.ID, reqID)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log("Product #%s added to basket #%s. ReqID: #%s", productID, basket.ID, reqID)
|
|
||||||
case strings.Contains(eName, event.EVENT_PRODUCT_REMOVED_FROM_BASKET):
|
case strings.Contains(eName, event.EVENT_PRODUCT_REMOVED_FROM_BASKET):
|
||||||
productID := int(data["product_id"].(float64))
|
productID := int(data["product_id"].(float64))
|
||||||
qty := int(data["quantity"].(float64))
|
qty := int(data["quantity"].(float64))
|
||||||
|
|
||||||
basket, err := ui.RemoveProductFromBasket(basketSrv, productID, qty, basketID, reqID)
|
basket, err := ui.RemoveProductFromBasket(basketSrv, productID, qty, basketID, reqID)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
logger.Log("%s error: %s", event.EVENT_PRODUCT_ADDED_TO_BASKET, err.Error())
|
logger.Log("Product #%s removed from basket #%s. ReqID: #%s", productID, basket.ID, reqID)
|
||||||
d.Reject(false) // FIXME: or Nack? how to handle erros in queue...
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
logger.Log("ACK: %s", eName)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
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 h1:Avz02GINwuYWOjw1fmZIJ3QgGEIz3a5vRQZNaxxUQIk=
|
||||||
git.pbiernat.dev/egommerce/api-entities v0.0.26/go.mod h1:+BXvUcr6Cr6QNpJsW8BUfe1vVILdWDADNE0e3u0lNvU=
|
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 h1:SzJRAkqJKdng/3d0V7o/R0yGh7QaZynPBn/P++on9RA=
|
||||||
git.pbiernat.dev/egommerce/go-api-pkg v0.0.136/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0=
|
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=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
@ -20,9 +20,10 @@ type Config struct {
|
|||||||
EventBusURL string `json:"eventbus_url"`
|
EventBusURL string `json:"eventbus_url"`
|
||||||
EventBusExchange string `json:"eventbus_exchange"`
|
EventBusExchange string `json:"eventbus_exchange"`
|
||||||
EventBusQueue string `json:"eventbus_queue"`
|
EventBusQueue string `json:"eventbus_queue"`
|
||||||
HttpReadTimeout int `json:"http_read_timeout"`
|
|
||||||
HttpWriteTimeout int `json:"http_write_timeout"`
|
HttpReadTimeout int `json:"http_read_timeout"`
|
||||||
HttpIdleTimeout int `json:"http_idle_timeout"`
|
HttpWriteTimeout int `json:"http_write_timeout"`
|
||||||
|
HttpIdleTimeout int `json:"http_idle_timeout"`
|
||||||
// Fields with json mapping are available trough ConsulKV
|
// Fields with json mapping are available trough ConsulKV
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,8 +77,7 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool, cache *re
|
|||||||
}(s)
|
}(s)
|
||||||
|
|
||||||
go func(s *Server) { // Server metadata cache updater
|
go func(s *Server) { // Server metadata cache updater
|
||||||
interval := time.Second * 5
|
ticker := time.NewTicker(time.Second * 5)
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
s.cacheMetadata()
|
s.cacheMetadata()
|
||||||
}
|
}
|
||||||
@ -90,10 +89,10 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool, cache *re
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() {
|
// func (s *Server) Start() {
|
||||||
err := s.Listen(s.addr)
|
// err := s.Listen(s.addr)
|
||||||
s.log.Log("Starting error: %v", err)
|
// s.log.Log("Starting error: %v", err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (s *Server) StartWithGracefulShutdown(forever chan struct{}) {
|
func (s *Server) StartWithGracefulShutdown(forever chan struct{}) {
|
||||||
go func() {
|
go func() {
|
||||||
@ -147,7 +146,7 @@ func (s *Server) updateKVConfig() { // FIXME: duplicated in cmd/worker/main.go
|
|||||||
|
|
||||||
func (s *Server) cacheMetadata() {
|
func (s *Server) cacheMetadata() {
|
||||||
ctx := context.Background()
|
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()
|
pos := s.cache.LPos(ctx, key, address, redis.LPosArgs{}).Val()
|
||||||
if pos >= 0 {
|
if pos >= 0 {
|
||||||
@ -159,14 +158,17 @@ func (s *Server) cacheMetadata() {
|
|||||||
|
|
||||||
func (s *Server) clearMetadataCache() {
|
func (s *Server) clearMetadataCache() {
|
||||||
ctx := context.Background()
|
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)
|
s.cache.LRem(ctx, key, 0, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) getMetadataIPsKey() string {
|
||||||
|
return "internal__" + s.conf.AppName + "__ips"
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) gracefulShutdown() error {
|
func (s *Server) gracefulShutdown() error {
|
||||||
s.log.Log("Server is going down...")
|
s.log.Log("Server is going down... Unregistering service: %s", s.discovery.GetID())
|
||||||
s.log.Log("Unregistering service: %s", s.discovery.GetID())
|
|
||||||
s.discovery.Unregister()
|
s.discovery.Unregister()
|
||||||
s.clearMetadataCache()
|
s.clearMetadataCache()
|
||||||
|
|
||||||
|
@ -94,6 +94,8 @@ func (s *BasketService) AddItem(ctx context.Context, itemID int, basketID string
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update basket updated_at field...
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +105,8 @@ func (s *BasketService) RemoveItem(ctx context.Context, itemID int, basketID str
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update basket updated_at field...
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,6 +124,8 @@ func (s *BasketService) UpdateItem(ctx context.Context, item *model.BasketItemMo
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update basket updated_at field...
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user