Skip to content

Commit

Permalink
improve on example logging
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Mar 18, 2024
1 parent fe862ad commit cd9eee3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
22 changes: 15 additions & 7 deletions example/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@ import (
)

type PaymentRequest struct {
UserID string
Tickets []string
UserID string `json:"userId"`
Tickets []string `json:"tickets"`
}

func payment(ctx restate.Context, request PaymentRequest) (bool, error) {
type PaymentResponse struct {
ID string `json:"id"`
Price int `json:"price"`
}

func payment(ctx restate.Context, request PaymentRequest) (response PaymentResponse, err error) {
uuid, err := restate.SideEffectAs(ctx, func() (string, error) {
uuid := uuid.New()
return uuid.String(), nil
})

response.ID = uuid

if err != nil {
return false, err
return response, err
}

// We are a uniform shop where everything costs 30 USD
// that is cheaper than the official example :P
price := len(request.Tickets) * 30

response.Price = price
i := 0
success, err := restate.SideEffectAs(ctx, func() (bool, error) {
_, err = restate.SideEffectAs(ctx, func() (bool, error) {
log := log.With().Str("uuid", uuid).Int("price", price).Logger()
if i > 2 {
log.Info().Msg("payment succeeded")
Expand All @@ -41,12 +49,12 @@ func payment(ctx restate.Context, request PaymentRequest) (bool, error) {
})

if err != nil {
return false, err
return response, err
}

// todo: send email

return success, nil
return response, nil
}

var (
Expand Down
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const (

func main() {

zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.InfoLevel)

server := server.NewRestate().
Bind(UserSessionServiceName, UserSession).
Expand Down
9 changes: 4 additions & 5 deletions example/user_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/muhamadazmy/restate-sdk-go"
"github.com/rs/zerolog/log"
)

func addTicket(ctx restate.Context, userId, ticketId string) (bool, error) {
Expand Down Expand Up @@ -73,17 +74,15 @@ func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error)
if len(tickets) == 0 {
return false, nil
}
var success bool

var response PaymentResponse
if err := ctx.Service(CheckoutServiceName).
Method("checkout").
Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &success); err != nil {
Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &response); err != nil {
return false, err
}

if !success {
return false, nil
}
log.Info().Str("id", response.ID).Int("price", response.Price).Msg("payment details")

call := ctx.Service(TicketServiceName).Method("markAsSold")
for _, ticket := range tickets {
Expand Down

0 comments on commit cd9eee3

Please sign in to comment.