Skip to content

Commit

Permalink
Add go callbacks example
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen committed Jan 7, 2025
1 parent 8130535 commit b827956
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
16 changes: 16 additions & 0 deletions go/patterns-use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ Have a look at the logs to see the cancellations of the flight and car booking i
```
</details>

## Durable Webhook Event Processing

This example processes webhook callbacks from a payment provider.
Restate handlers can be used as the target for webhook callbacks.
This turns handlers into durable event processors that ensure the event is processed exactly once.
You don't need to do anything special!

## Scheduling Tasks
This example processes failed payment events from a payment provider.
The service reminds the customer for 3 days to update their payment details, and otherwise escalates to support.

To schedule the reminders, the handler uses Restate's durable timers and delayed calls.
The handler calls itself three times in a row after a delay of one day, and then stops the loop and calls another handler.

Restate tracks the timer across failures, and triggers execution.

## Stateful Actors

This example implements a State Machine with a Virtual Object.
Expand Down
4 changes: 2 additions & 2 deletions go/patterns-use-cases/src/dataupload/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const RESTATE_URL = "http://localhost:8080"
// workflow to send the upload url via email instead.

func upload(id string, email string) error {
slog.Info(fmt.Sprintf("Start upload for %s", id))
slog.Info("Start upload for " + id)

client := &http.Client{Timeout: 5 * time.Second}

Expand Down Expand Up @@ -50,7 +50,7 @@ func upload(id string, email string) error {
}

// ... process result directly ...
slog.Info(fmt.Sprintf("Fast upload: URL was %s", string(body)))
slog.Info("Fast upload: URL was " + string(body))
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions go/patterns-use-cases/src/webhookcallbacks/callbackrouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import restate "github.com/restatedev/sdk-go"

type WebhookCallbackRouter struct{}

// Any handler can be a durable webhook processor that never loses events
// You don't need to do anything special for this. Just point your webhook to the handler endpoint.

func (WebhookCallbackRouter) OnStripeEvent(ctx restate.Context, event StripeEvent) error {
if event.Type == "invoice.payment_failed" {
restate.ObjectSend(ctx, "PaymentTracker", "onPaymentFailed", event.Data.Object.ID).
Send(event)
} else if event.Type == "invoice.payment_succeeded" {
restate.ObjectSend(ctx, "PaymentTracker", "onPaymentSuccess", event.Data.Object.ID).
Send(event)
}
return nil
}
23 changes: 23 additions & 0 deletions go/patterns-use-cases/src/webhookcallbacks/stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import restate "github.com/restatedev/sdk-go"

type StripeEvent struct {
ID string `json:"id"`
Type string `json:"type"`
Created int64 `json:"created"`
Data struct {
Object struct {
ID string `json:"id"`
Customer string `json:"customer"`
} `json:"object"`
} `json:"data"`
}

// Have a look at the scheduling tasks example (../schedulingtasks/paymentreminders.go)
// to see a full implementation of this

type PaymentTracker struct{}

func (PaymentTracker) OnPaymentFailed(ctx restate.Context, event StripeEvent) error { return nil }
func (PaymentTracker) OnPaymentSuccess(ctx restate.Context, event StripeEvent) error { return nil }

0 comments on commit b827956

Please sign in to comment.