Skip to content

Commit b827956

Browse files
committed
Add go callbacks example
1 parent 8130535 commit b827956

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

go/patterns-use-cases/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ Have a look at the logs to see the cancellations of the flight and car booking i
114114
```
115115
</details>
116116

117+
## Durable Webhook Event Processing
118+
119+
This example processes webhook callbacks from a payment provider.
120+
Restate handlers can be used as the target for webhook callbacks.
121+
This turns handlers into durable event processors that ensure the event is processed exactly once.
122+
You don't need to do anything special!
123+
124+
## Scheduling Tasks
125+
This example processes failed payment events from a payment provider.
126+
The service reminds the customer for 3 days to update their payment details, and otherwise escalates to support.
127+
128+
To schedule the reminders, the handler uses Restate's durable timers and delayed calls.
129+
The handler calls itself three times in a row after a delay of one day, and then stops the loop and calls another handler.
130+
131+
Restate tracks the timer across failures, and triggers execution.
132+
117133
## Stateful Actors
118134

119135
This example implements a State Machine with a Virtual Object.

go/patterns-use-cases/src/dataupload/client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const RESTATE_URL = "http://localhost:8080"
2020
// workflow to send the upload url via email instead.
2121

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

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

@@ -50,7 +50,7 @@ func upload(id string, email string) error {
5050
}
5151

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import restate "github.com/restatedev/sdk-go"
4+
5+
type WebhookCallbackRouter struct{}
6+
7+
// Any handler can be a durable webhook processor that never loses events
8+
// You don't need to do anything special for this. Just point your webhook to the handler endpoint.
9+
10+
func (WebhookCallbackRouter) OnStripeEvent(ctx restate.Context, event StripeEvent) error {
11+
if event.Type == "invoice.payment_failed" {
12+
restate.ObjectSend(ctx, "PaymentTracker", "onPaymentFailed", event.Data.Object.ID).
13+
Send(event)
14+
} else if event.Type == "invoice.payment_succeeded" {
15+
restate.ObjectSend(ctx, "PaymentTracker", "onPaymentSuccess", event.Data.Object.ID).
16+
Send(event)
17+
}
18+
return nil
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import restate "github.com/restatedev/sdk-go"
4+
5+
type StripeEvent struct {
6+
ID string `json:"id"`
7+
Type string `json:"type"`
8+
Created int64 `json:"created"`
9+
Data struct {
10+
Object struct {
11+
ID string `json:"id"`
12+
Customer string `json:"customer"`
13+
} `json:"object"`
14+
} `json:"data"`
15+
}
16+
17+
// Have a look at the scheduling tasks example (../schedulingtasks/paymentreminders.go)
18+
// to see a full implementation of this
19+
20+
type PaymentTracker struct{}
21+
22+
func (PaymentTracker) OnPaymentFailed(ctx restate.Context, event StripeEvent) error { return nil }
23+
func (PaymentTracker) OnPaymentSuccess(ctx restate.Context, event StripeEvent) error { return nil }

0 commit comments

Comments
 (0)