Refactor. Consul TTl fix, gracefull shutdown
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2022-12-02 20:12:02 +01:00
parent 34d04efaf8
commit 5efc8296e5
9 changed files with 37 additions and 21 deletions

View File

@ -10,10 +10,10 @@ ARG BIN_OUTPUT
ARG SVC_NAME
ARG SVC_VER
LABEL author="Piotr Biernat"
LABEL vendor="egommerce"
LABEL service=${SVC_NAME}
LABEL version=${SVC_VER}
LABEL dev.egommerce.image.author="Piotr Biernat"
LABEL dev.egommerce.image.service="api-eventubus"
LABEL dev.egommerce.image.service=${SVC_NAME}
LABEL dev.egommerce.image.version=${SVC_VER}
WORKDIR /
COPY --from=builder $BIN_OUTPUT /app

View File

@ -12,7 +12,7 @@ TARGET=${1:-latest}
export DOCKER_BUILDKIT=1
docker build -t "$BUILDER_IMAGE" -f Dockerfile.builder . >/dev/null 2>&1 && echo "Successfully tagged $BUILDER_IMAGE"
docker build -t "$BUILDER_IMAGE" -f Dockerfile.builder . && echo "Successfully tagged $BUILDER_IMAGE" || echo "Build failed. Exit."
echo "Building target $IMAGE_PREFIX images..."
if [ $TARGET = "latest" ]

View File

@ -11,6 +11,7 @@ import (
"git.pbiernat.dev/egommerce/basket-service/internal/app/config"
"git.pbiernat.dev/egommerce/basket-service/internal/app/database"
def "git.pbiernat.dev/egommerce/basket-service/internal/app/definition"
"git.pbiernat.dev/egommerce/basket-service/internal/app/event"
"git.pbiernat.dev/egommerce/basket-service/internal/app/server"
"git.pbiernat.dev/egommerce/basket-service/internal/app/service"
"git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
@ -91,6 +92,7 @@ func main() {
// os.Exit(1)
// }
// event consume
msgs, err := ebCh.Consume(
c.EventBusQueue, // queue
"", // consumer
@ -108,7 +110,7 @@ func main() {
forever := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-sigint
logger.Log("Worker %s stopped working...\n", c.GetAppFullName())
@ -122,23 +124,20 @@ func main() {
for d := range msgs {
msg, err := amqp.Deserialize(d.Body)
if err != nil {
logger.Log("json error: %v", err)
logger.Log("json error: %v\n", err)
d.Reject(false) // FIXME: how to handle erros in queue...????
continue
}
event := fmt.Sprintf("%s", msg["event"])
eName := fmt.Sprintf("%s", msg["event"])
data := (msg["data"]).(map[string]interface{})
logger.Log("Message<%s>: %s\n", event, data)
logger.Log("Message<%s>: %s\n", eName, data)
basketID := data["basket_id"].(string) // FIXME Check input params!
productID := data["product_id"].(string) // FIXME Check input params!
switch true {
case strings.Contains(event, amqp.EVENT_PRODUCT_ADDED_TO_BASKET):
// create new basket and add product to it
// FIXME CHECK if basket exists
case strings.Contains(eName, event.EVENT_PRODUCT_ADDED_TO_BASKET):
var basket *def.BasketModel
basket, err := bSrvc.FetchFromDB(basketID)
if err != nil {
@ -158,11 +157,12 @@ func main() {
}
logger.Log("Fetched basket: %v", basket)
case strings.Contains(event, amqp.EVENT_PRODUCT_REMOVED_FROM_BASKET):
case strings.Contains(eName, event.EVENT_PRODUCT_REMOVED_FROM_BASKET):
// remove product from basket
logger.Log("Event: %s", event.EVENT_PRODUCT_REMOVED_FROM_BASKET)
}
logger.Log("ACK: %s", event)
logger.Log("ACK: %s", eName)
d.Ack(false)
}
}()

View File

@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/basket-service
go 1.18
require (
git.pbiernat.dev/egommerce/go-api-pkg v0.0.29
git.pbiernat.dev/egommerce/go-api-pkg v0.0.101
github.com/georgysavva/scany/v2 v2.0.0
github.com/gofiber/fiber/v2 v2.40.1
github.com/jackc/pgtype v1.12.0

View File

@ -1,5 +1,13 @@
git.pbiernat.dev/egommerce/go-api-pkg v0.0.29 h1:EG6t3i0P8ENH3eRYPgjVE8UHpnif8UPYA/23e1Nm6n0=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.29/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.32 h1:ArB/n30m927WMAM4u51guH+qR0Lu4NGyYnYdi7OhlzY=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.32/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.33 h1:1tm+pvUeS6OZLvHmLM3BwFS0Ty/eA3jDRuB60OicosA=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.33/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.34 h1:UO1x6O+cyU7yYYbDCDyhhAypuf4QGIXcmWcBEEjLuYM=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.34/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.100 h1:jw4fiGbZTsfJXJpGV+HQiYeMGZ7DMRMoepjuIwY6FIU=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.100/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.101 h1:NZCFAAlC94+LcN1gjrENnWUHvpWgaNksyB2N4Fiy8C4=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.101/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=

View File

@ -1,5 +1,10 @@
package event
const (
EVENT_PRODUCT_ADDED_TO_BASKET = "event.ProductAddedToBasketEvent"
EVENT_PRODUCT_REMOVED_FROM_BASKET = "event.ProductRemovedFromBasketEvent"
)
type ProductAddedToBasketEvent struct {
*Event
ProductID string `json:"product_id"`

View File

@ -1,5 +1,9 @@
package event
const (
EVENT_BASKET_CHECKOUT = "event.BasketCheckoutEvent"
)
type BasketCheckoutEvent struct {
*Event
BasketID string `json:"basket_id"`

View File

@ -72,7 +72,7 @@ func (s *Server) Start() {
func (s *Server) StartWithGracefulShutdown(forever chan struct{}) {
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-sigint
if err := s.gracefulShutdown(); err != nil {
@ -96,7 +96,6 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
}
return hdr.RequestID, nil
}
func (s *Server) gracefulShutdown() error {

View File

@ -61,7 +61,7 @@ func (s *BasketService) Checkout(reqID, basketID string) (string, error) {
s.log.Log("Creating initial order from basket#:%s", basketID)
msg := &event.BasketCheckoutEvent{Event: event.NewEvent(reqID), BasketID: basketID}
amqp.Publish(s.ebCh, "api-events", "basket.order.basketCheckout", msg) // fixme probably redundant in eventName in routingKey
amqp.Publish(s.ebCh, "api-events", "basket.order.basketCheckout", msg)
return basketID, nil
}