Refactor. Consul TTl fix, gracefull shutdown
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
34d04efaf8
commit
5efc8296e5
@ -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
|
||||||
|
@ -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 "Build failed. Exit."
|
||||||
|
|
||||||
echo "Building target $IMAGE_PREFIX images..."
|
echo "Building target $IMAGE_PREFIX images..."
|
||||||
if [ $TARGET = "latest" ]
|
if [ $TARGET = "latest" ]
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"git.pbiernat.dev/egommerce/basket-service/internal/app/config"
|
"git.pbiernat.dev/egommerce/basket-service/internal/app/config"
|
||||||
"git.pbiernat.dev/egommerce/basket-service/internal/app/database"
|
"git.pbiernat.dev/egommerce/basket-service/internal/app/database"
|
||||||
def "git.pbiernat.dev/egommerce/basket-service/internal/app/definition"
|
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/server"
|
||||||
"git.pbiernat.dev/egommerce/basket-service/internal/app/service"
|
"git.pbiernat.dev/egommerce/basket-service/internal/app/service"
|
||||||
"git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
|
"git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
|
||||||
@ -91,6 +92,7 @@ func main() {
|
|||||||
// os.Exit(1)
|
// os.Exit(1)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// event consume
|
||||||
msgs, err := ebCh.Consume(
|
msgs, err := ebCh.Consume(
|
||||||
c.EventBusQueue, // queue
|
c.EventBusQueue, // queue
|
||||||
"", // consumer
|
"", // consumer
|
||||||
@ -108,7 +110,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())
|
||||||
@ -122,23 +124,20 @@ func main() {
|
|||||||
for d := range msgs {
|
for d := range msgs {
|
||||||
msg, err := amqp.Deserialize(d.Body)
|
msg, err := amqp.Deserialize(d.Body)
|
||||||
if err != nil {
|
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...????
|
d.Reject(false) // FIXME: how to handle erros in queue...????
|
||||||
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)
|
||||||
|
|
||||||
basketID := data["basket_id"].(string) // FIXME Check input params!
|
basketID := data["basket_id"].(string) // FIXME Check input params!
|
||||||
productID := data["product_id"].(string) // FIXME Check input params!
|
productID := data["product_id"].(string) // FIXME Check input params!
|
||||||
|
|
||||||
switch true {
|
switch true {
|
||||||
case strings.Contains(event, amqp.EVENT_PRODUCT_ADDED_TO_BASKET):
|
case strings.Contains(eName, event.EVENT_PRODUCT_ADDED_TO_BASKET):
|
||||||
// create new basket and add product to it
|
|
||||||
// FIXME CHECK if basket exists
|
|
||||||
|
|
||||||
var basket *def.BasketModel
|
var basket *def.BasketModel
|
||||||
basket, err := bSrvc.FetchFromDB(basketID)
|
basket, err := bSrvc.FetchFromDB(basketID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -158,11 +157,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
logger.Log("Fetched basket: %v", basket)
|
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
|
// 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)
|
d.Ack(false)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/basket-service
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
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/georgysavva/scany/v2 v2.0.0
|
||||||
github.com/gofiber/fiber/v2 v2.40.1
|
github.com/gofiber/fiber/v2 v2.40.1
|
||||||
github.com/jackc/pgtype v1.12.0
|
github.com/jackc/pgtype v1.12.0
|
||||||
|
12
src/go.sum
12
src/go.sum
@ -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.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.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/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=
|
||||||
|
@ -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"`
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package event
|
package event
|
||||||
|
|
||||||
|
const (
|
||||||
|
EVENT_BASKET_CHECKOUT = "event.BasketCheckoutEvent"
|
||||||
|
)
|
||||||
|
|
||||||
type BasketCheckoutEvent struct {
|
type BasketCheckoutEvent struct {
|
||||||
*Event
|
*Event
|
||||||
BasketID string `json:"basket_id"`
|
BasketID string `json:"basket_id"`
|
||||||
|
@ -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 {
|
||||||
|
@ -61,7 +61,7 @@ func (s *BasketService) Checkout(reqID, basketID string) (string, error) {
|
|||||||
s.log.Log("Creating initial order from basket#:%s", basketID)
|
s.log.Log("Creating initial order from basket#:%s", basketID)
|
||||||
|
|
||||||
msg := &event.BasketCheckoutEvent{Event: event.NewEvent(reqID), BasketID: 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
|
return basketID, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user