-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdd4717
commit a621732
Showing
10 changed files
with
275 additions
and
131 deletions.
There are no files selected for viewing
File renamed without changes.
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"github.com/muhamadazmy/restate-sdk-go" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type PaymentRequest struct { | ||
UserID string | ||
Tickets []string | ||
} | ||
|
||
func payment(ctx restate.Context, request PaymentRequest) (bool, error) { | ||
uuid, err := restate.SideEffectAs(ctx, func() (string, error) { | ||
uuid := uuid.New() | ||
return uuid.String(), nil | ||
}) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// We are a uniform shop where everything costs 30 USD | ||
// that is cheaper than the official example :P | ||
price := len(request.Tickets) * 30 | ||
|
||
i := 0 | ||
success, 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") | ||
return true, nil | ||
} | ||
|
||
log.Error().Msg("payment failed") | ||
i += 1 | ||
return false, fmt.Errorf("failed to pay") | ||
}) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// todo: send email | ||
|
||
return success, nil | ||
} | ||
|
||
var ( | ||
Checkout = restate.NewUnKeyedRouter(). | ||
Handler("checkout", restate.NewUnKeyedHandler(payment)) | ||
) |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/muhamadazmy/restate-sdk-go/server" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
const ( | ||
UserSessionServiceName = "UserSession" | ||
TicketServiceName = "TicketService" | ||
CheckoutServiceName = "Checkout" | ||
) | ||
|
||
func main() { | ||
|
||
zerolog.SetGlobalLevel(zerolog.DebugLevel) | ||
|
||
server := server.NewRestate(). | ||
Bind(UserSessionServiceName, UserSession). | ||
Bind(TicketServiceName, TicketService). | ||
Bind(CheckoutServiceName, Checkout) | ||
|
||
if err := server.Start(context.Background(), ":9080"); err != nil { | ||
log.Error().Err(err).Msg("application exited unexpectedly") | ||
os.Exit(1) | ||
} | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/muhamadazmy/restate-sdk-go" | ||
) | ||
|
||
type TicketStatus int | ||
|
||
const ( | ||
TicketAvailable TicketStatus = 0 | ||
TicketReserved TicketStatus = 1 | ||
TicketSold TicketStatus = 2 | ||
) | ||
|
||
func reserve(ctx restate.Context, _ string, _ restate.Void) (bool, error) { | ||
status, err := restate.GetAs[TicketStatus](ctx, "status") | ||
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) { | ||
return false, err | ||
} | ||
|
||
if status == TicketAvailable { | ||
return true, restate.SetAs(ctx, "status", TicketReserved) | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func unreserve(ctx restate.Context, _ string, _ restate.Void) (void restate.Void, err error) { | ||
status, err := restate.GetAs[TicketStatus](ctx, "status") | ||
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) { | ||
return void, err | ||
} | ||
|
||
if status != TicketSold { | ||
return void, ctx.Clear("status") | ||
} | ||
|
||
return void, nil | ||
} | ||
|
||
func markAsSold(ctx restate.Context, _ string, _ restate.Void) (void restate.Void, err error) { | ||
status, err := restate.GetAs[TicketStatus](ctx, "status") | ||
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) { | ||
return void, err | ||
} | ||
|
||
if status == TicketReserved { | ||
return void, restate.SetAs(ctx, "status", TicketSold) | ||
} | ||
|
||
return void, nil | ||
} | ||
|
||
var ( | ||
TicketService = restate.NewKeyedRouter(). | ||
Handler("reserve", restate.NewKeyedHandler(reserve)). | ||
Handler("unreserve", restate.NewKeyedHandler(unreserve)). | ||
Handler("markAsSold", restate.NewKeyedHandler(markAsSold)) | ||
) |
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,103 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"slices" | ||
"time" | ||
|
||
"github.com/muhamadazmy/restate-sdk-go" | ||
) | ||
|
||
func addTicket(ctx restate.Context, userId, ticketId string) (bool, error) { | ||
|
||
var success bool | ||
if err := ctx.Service(TicketServiceName).Method("reserve").Do(ticketId, userId, &success); err != nil { | ||
return false, err | ||
} | ||
|
||
if !success { | ||
return false, nil | ||
} | ||
|
||
// add ticket to list of tickets | ||
tickets, err := restate.GetAs[[]string](ctx, "tickets") | ||
|
||
if err != nil && errors.Is(err, restate.ErrKeyNotFound) { | ||
return false, err | ||
} | ||
|
||
tickets = append(tickets, ticketId) | ||
|
||
if err := restate.SetAs(ctx, "tickets", tickets); err != nil { | ||
return false, err | ||
} | ||
|
||
if err := ctx.Service(UserSessionServiceName).Method("expireTicket").Send(userId, ticketId, 15*time.Minute); err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func expireTicket(ctx restate.Context, _, ticketId string) (void restate.Void, err error) { | ||
tickets, err := restate.GetAs[[]string](ctx, "tickets") | ||
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) { | ||
return void, err | ||
} | ||
|
||
deleted := false | ||
tickets = slices.DeleteFunc(tickets, func(ticket string) bool { | ||
if ticket == ticketId { | ||
deleted = true | ||
return true | ||
} | ||
return false | ||
}) | ||
if !deleted { | ||
return void, nil | ||
} | ||
|
||
if err := restate.SetAs(ctx, "tickets", tickets); err != nil { | ||
return void, err | ||
} | ||
|
||
return void, ctx.Service(TicketServiceName).Method("unreserve").Send(ticketId, nil, 0) | ||
} | ||
|
||
func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error) { | ||
tickets, err := restate.GetAs[[]string](ctx, "tickets") | ||
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) { | ||
return false, err | ||
} | ||
|
||
if len(tickets) == 0 { | ||
return false, nil | ||
} | ||
var success bool | ||
|
||
if err := ctx.Service(CheckoutServiceName). | ||
Method("checkout"). | ||
Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &success); err != nil { | ||
return false, err | ||
} | ||
|
||
if !success { | ||
return false, nil | ||
} | ||
|
||
call := ctx.Service(TicketServiceName).Method("markAsSold") | ||
for _, ticket := range tickets { | ||
if err := call.Send(ticket, nil, 0); err != nil { | ||
return false, err | ||
} | ||
} | ||
|
||
return true, ctx.Clear("tickets") | ||
} | ||
|
||
var ( | ||
UserSession = restate.NewKeyedRouter(). | ||
Handler("addTicket", restate.NewKeyedHandler(addTicket)). | ||
Handler("expireTicket", restate.NewKeyedHandler(expireTicket)). | ||
Handler("checkout", restate.NewKeyedHandler(checkout)) | ||
) |
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
Oops, something went wrong.