-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#update: handle rollback transaction
- Loading branch information
Showing
13 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package message | ||
|
||
type CancelOrderMessage struct { | ||
OrderID string `json:"order_id"` | ||
CancelStatus int `json:"cancel_status"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
internal/subscriber/cancelPurchase/purchase_cancel_orchestrator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package cancelPurchase | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/gofiber/fiber/v2/log" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
"latipe-transaction-service/config" | ||
"latipe-transaction-service/internal/domain/message" | ||
"latipe-transaction-service/internal/service/orderserv" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type PurchaseCancelOrchestratorSubscriber struct { | ||
config *config.Config | ||
orderServ orderserv.OrderService | ||
conn *amqp.Connection | ||
} | ||
|
||
func NewPurchaseCancelOrchestratorSubscriber(cfg *config.Config, | ||
orderServ orderserv.OrderService, conn *amqp.Connection) *PurchaseCancelOrchestratorSubscriber { | ||
return &PurchaseCancelOrchestratorSubscriber{ | ||
config: cfg, | ||
orderServ: orderServ, | ||
conn: conn, | ||
} | ||
} | ||
|
||
func (orch PurchaseCancelOrchestratorSubscriber) ListenPurchaseCancel(wg *sync.WaitGroup) { | ||
channel, err := orch.conn.Channel() | ||
defer channel.Close() | ||
|
||
// define an exchange type "topic" | ||
err = channel.ExchangeDeclare( | ||
orch.config.RabbitMQ.SagaOrderEvent.Exchange, | ||
"topic", | ||
true, | ||
false, | ||
false, | ||
false, | ||
nil, | ||
) | ||
if err != nil { | ||
log.Fatalf("cannot declare exchange: %v", err) | ||
} | ||
|
||
// create queue | ||
q, err := channel.QueueDeclare( | ||
"purchase_cancel_event", | ||
true, | ||
false, | ||
false, | ||
false, | ||
nil, | ||
) | ||
if err != nil { | ||
log.Fatalf("cannot declare queue: %v", err) | ||
} | ||
|
||
err = channel.QueueBind( | ||
q.Name, | ||
orch.config.RabbitMQ.SagaOrderEvent.CancelRoutingKey, | ||
orch.config.RabbitMQ.SagaOrderEvent.Exchange, | ||
false, | ||
nil) | ||
if err != nil { | ||
log.Fatalf("cannot bind exchange: %v", err) | ||
} | ||
|
||
// declaring consumer with its properties over channel opened | ||
msgs, err := channel.Consume( | ||
q.Name, // queue | ||
orch.config.RabbitMQ.ServiceName, // consumer | ||
true, // auto ack | ||
false, // exclusive | ||
false, // no local | ||
false, // no wait | ||
nil, //args | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer wg.Done() | ||
// handle consumed messages from queue | ||
for msg := range msgs { | ||
log.Infof("received order message from: %s", msg.RoutingKey) | ||
if err := orch.handleMessage(&msg); err != nil { | ||
log.Infof("The order cancel failed cause %s", err) | ||
} | ||
} | ||
|
||
log.Infof("message queue has started") | ||
log.Infof("waiting for messages...") | ||
} | ||
|
||
func (orch PurchaseCancelOrchestratorSubscriber) handleMessage(msg *amqp.Delivery) error { | ||
startTime := time.Now() | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
messageDTO := message.CancelOrderMessage{} | ||
|
||
if err := json.Unmarshal(msg.Body, &messageDTO); err != nil { | ||
log.Infof("Parse message to order failed cause: %s", err) | ||
return err | ||
} | ||
|
||
err := orch.orderServ.CancelOrder(ctx, &messageDTO) | ||
if err != nil { | ||
log.Infof("Handling reply message was failed cause: %s", err) | ||
return err | ||
} | ||
|
||
endTime := time.Now() | ||
log.Infof("The orders [checkout_id: %v] was processed successfully - duration:%v", messageDTO.OrderID, endTime.Sub(startTime)) | ||
fmt.Println() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cancelPurchase | ||
|
||
import ( | ||
"latipe-transaction-service/config" | ||
"latipe-transaction-service/internal/service/orderserv" | ||
) | ||
|
||
func MappingRoutingKeyToService(routingKey string, config *config.Config) int { | ||
switch routingKey { | ||
case config.RabbitMQ.SagaOrderProductEvent.ReplyRoutingKey: | ||
return orderserv.PRODUCT_SERVICE | ||
case config.RabbitMQ.SagaOrderDeliveryEvent.ReplyRoutingKey: | ||
return orderserv.DELIVERY_SERVICE | ||
case config.RabbitMQ.SagaOrderPromotionEvent.ReplyRoutingKey: | ||
return orderserv.PROMOTION_SERVICE | ||
case config.RabbitMQ.SagaOrderPaymentEvent.ReplyRoutingKey: | ||
return orderserv.PAYMENT_SERVICE | ||
} | ||
return -1 | ||
} |
Oops, something went wrong.