From 32f148ea95c3997beb9fd7df386bd28d3aac040d Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Fri, 2 Dec 2022 19:05:31 +0100 Subject: [PATCH] Refactor. Consul TTl fix, gracefull shutdown --- Dockerfile.target | 8 ++++---- deploy/image-build.sh | 2 +- src/cmd/worker/main.go | 23 +++++++++++------------ src/go.mod | 2 +- src/go.sum | 16 ++++++++++------ src/internal/app/event/basket.go | 5 +++++ src/internal/app/event/warehouse.go | 11 +++++++++++ src/internal/app/server/server.go | 3 +-- 8 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 src/internal/app/event/warehouse.go diff --git a/Dockerfile.target b/Dockerfile.target index 1807b65..b822ac9 100644 --- a/Dockerfile.target +++ b/Dockerfile.target @@ -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 diff --git a/deploy/image-build.sh b/deploy/image-build.sh index 0008b2c..2e4faa6 100755 --- a/deploy/image-build.sh +++ b/deploy/image-build.sh @@ -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 "Building target $IMAGE_PREFIX images..." if [ $TARGET = "latest" ] diff --git a/src/cmd/worker/main.go b/src/cmd/worker/main.go index fda6b69..9cd35ed 100644 --- a/src/cmd/worker/main.go +++ b/src/cmd/worker/main.go @@ -5,10 +5,12 @@ import ( "log" "os" "os/signal" + "strings" "syscall" "git.pbiernat.dev/egommerce/catalog-service/internal/app/config" "git.pbiernat.dev/egommerce/catalog-service/internal/app/database" + "git.pbiernat.dev/egommerce/catalog-service/internal/app/event" "git.pbiernat.dev/egommerce/catalog-service/internal/app/server" "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" amqp "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" @@ -103,7 +105,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()) @@ -120,20 +122,17 @@ func main() { 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) - // switch true { - // case strings.Contains(event, amqp.EVENT_PRODUCT_ADD): - // // create new basket and add product to it - // logger.Log("Event: %s", amqp.EVENT_PRODUCT_ADD) - // case strings.Contains(event, amqp.EVENT_PRODUCT_REMOVE): - // // remove product from basket - // logger.Log("Event: %s", amqp.EVENT_PRODUCT_REMOVE) - // } + switch true { + case strings.Contains(eName, event.EVENT_WAREHOUSE_STOCK_UPDATED): // todo: first create such service ;) + // update product available qty + logger.Log("Event: %s", event.EVENT_WAREHOUSE_STOCK_UPDATED) + } - logger.Log("ACK: %s", event) + logger.Log("ACK: %s", eName) d.Ack(false) } }() diff --git a/src/go.mod b/src/go.mod index f532703..39582b5 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/catalog-service go 1.18 require ( - git.pbiernat.dev/egommerce/go-api-pkg v0.0.31 + git.pbiernat.dev/egommerce/go-api-pkg v0.0.101 github.com/gofiber/fiber/v2 v2.40.1 github.com/google/uuid v1.3.0 github.com/jackc/pgx/v4 v4.17.2 diff --git a/src/go.sum b/src/go.sum index 30396a4..a46cd6b 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,9 +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.30 h1:qRUGkv/TA7vO6FDnKKxZ6CfooQVfp3/PpL3vGf6lwYY= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.30/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.31 h1:TZOdMTp++vws86ZlwtWKmpoik5YN6aOSWRpu9j0FykU= -git.pbiernat.dev/egommerce/go-api-pkg v0.0.31/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= diff --git a/src/internal/app/event/basket.go b/src/internal/app/event/basket.go index 0e8f578..93f91df 100644 --- a/src/internal/app/event/basket.go +++ b/src/internal/app/event/basket.go @@ -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"` diff --git a/src/internal/app/event/warehouse.go b/src/internal/app/event/warehouse.go new file mode 100644 index 0000000..8cd1328 --- /dev/null +++ b/src/internal/app/event/warehouse.go @@ -0,0 +1,11 @@ +package event + +const ( + EVENT_WAREHOUSE_STOCK_UPDATED = "event.WarehouseStockUpdatedEvent" +) + +type WarehouseStockUpdatedEvent struct { + *Event + ProductID string `json:"product_id"` + NewQty int `json:"new_qty"` +} diff --git a/src/internal/app/server/server.go b/src/internal/app/server/server.go index 684d144..3084073 100644 --- a/src/internal/app/server/server.go +++ b/src/internal/app/server/server.go @@ -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 {