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 19:05:31 +01:00
parent bf47c8645d
commit 32f148ea95
8 changed files with 44 additions and 26 deletions

View File

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

View File

@ -12,7 +12,7 @@ TARGET=${1:-latest}
export DOCKER_BUILDKIT=1 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..." echo "Building target $IMAGE_PREFIX images..."
if [ $TARGET = "latest" ] if [ $TARGET = "latest" ]

View File

@ -5,10 +5,12 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"git.pbiernat.dev/egommerce/catalog-service/internal/app/config" "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/database"
"git.pbiernat.dev/egommerce/catalog-service/internal/app/event"
"git.pbiernat.dev/egommerce/catalog-service/internal/app/server" "git.pbiernat.dev/egommerce/catalog-service/internal/app/server"
"git.pbiernat.dev/egommerce/go-api-pkg/fluentd" "git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
amqp "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" amqp "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq"
@ -103,7 +105,7 @@ func main() {
forever := make(chan struct{}) forever := make(chan struct{})
go func() { go func() {
sigint := make(chan os.Signal, 1) 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 <-sigint
logger.Log("Worker %s stopped working...\n", c.GetAppFullName()) logger.Log("Worker %s stopped working...\n", c.GetAppFullName())
@ -120,20 +122,17 @@ func main() {
continue continue
} }
event := fmt.Sprintf("%s", msg["event"]) eName := fmt.Sprintf("%s", msg["event"])
data := (msg["data"]).(map[string]interface{}) data := (msg["data"]).(map[string]interface{})
logger.Log("Message<%s>: %s\n", event, data) logger.Log("Message<%s>: %s\n", eName, data)
// switch true { switch true {
// case strings.Contains(event, amqp.EVENT_PRODUCT_ADD): case strings.Contains(eName, event.EVENT_WAREHOUSE_STOCK_UPDATED): // todo: first create such service ;)
// // create new basket and add product to it // update product available qty
// logger.Log("Event: %s", amqp.EVENT_PRODUCT_ADD) logger.Log("Event: %s", event.EVENT_WAREHOUSE_STOCK_UPDATED)
// case strings.Contains(event, amqp.EVENT_PRODUCT_REMOVE): }
// // remove product from basket
// logger.Log("Event: %s", amqp.EVENT_PRODUCT_REMOVE)
// }
logger.Log("ACK: %s", event) logger.Log("ACK: %s", eName)
d.Ack(false) d.Ack(false)
} }
}() }()

View File

@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/catalog-service
go 1.18 go 1.18
require ( 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/gofiber/fiber/v2 v2.40.1
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.17.2 github.com/jackc/pgx/v4 v4.17.2

View File

@ -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.32 h1:ArB/n30m927WMAM4u51guH+qR0Lu4NGyYnYdi7OhlzY=
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/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.33 h1:1tm+pvUeS6OZLvHmLM3BwFS0Ty/eA3jDRuB60OicosA=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.30/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8= git.pbiernat.dev/egommerce/go-api-pkg v0.0.33/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.34 h1:UO1x6O+cyU7yYYbDCDyhhAypuf4QGIXcmWcBEEjLuYM=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.31/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8= 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/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/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= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=

View File

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

View File

@ -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"`
}

View File

@ -72,7 +72,7 @@ func (s *Server) Start() {
func (s *Server) StartWithGracefulShutdown(forever chan struct{}) { func (s *Server) StartWithGracefulShutdown(forever chan struct{}) {
go func() { go func() {
sigint := make(chan os.Signal, 1) 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 <-sigint
if err := s.gracefulShutdown(); err != nil { if err := s.gracefulShutdown(); err != nil {
@ -96,7 +96,6 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
} }
return hdr.RequestID, nil return hdr.RequestID, nil
} }
func (s *Server) gracefulShutdown() error { func (s *Server) gracefulShutdown() error {