-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Python Tour of Restate #444
Merged
Merged
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
54e86d9
Add docs/develop/go
jackkleeman a227806
Update Cloud docs
jackkleeman cdf3fba
Use main() in service and virtual object
jackkleeman 7070513
Go quickstart
jackkleeman a39fd74
Update Go SDK
jackkleeman 0926d23
Add tour
jackkleeman 109b345
Small updates
gvdongen 6e55b90
Updates to Tour of Restate Python
gvdongen 35da53f
Tour of Restate Python fixes
gvdongen 956a12f
Update for latest go sdk
jackkleeman 6282c9d
Update docs/develop/go/journaling-results.mdx
jackkleeman 29a8a08
Review comments
jackkleeman eacec82
github prerelease workflow and fixes for mypy
gvdongen 2ed99a9
Add Python to test-build github workflow
gvdongen b1ae453
Try bumping Python to 3.12 for github workflows
gvdongen 05d5e71
*string for default check
jackkleeman 08dbb8f
Add go to github workflows
jackkleeman 6be1506
Add logo to overview
jackkleeman b6800c8
Go in concepts pages
jackkleeman bb80398
Add go microservice use case
jackkleeman 3e64e5d
Event processing go docs
jackkleeman 4bb79f3
Reduce tab spacing
jackkleeman 0e539bb
Upgrade Python
gvdongen 2ade447
Java formatting
gvdongen 19d8d00
Fix paths to code snippets
gvdongen a97c09e
Merge branch 'refs/heads/golang' into python_tour
gvdongen a31e199
Merge branch 'refs/heads/main' into python_tour
gvdongen 912180f
Rebase on main (#453)
gvdongen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,18 @@ on: | |
description: "sdk-typescript version (without prepending v)." | ||
required: false | ||
type: string | ||
sdkPythonVersion: | ||
description: "sdk-python version (without prepending v)." | ||
required: false | ||
type: string | ||
sdkJavaVersion: | ||
description: "sdk-java version (without prepending v)." | ||
required: false | ||
type: string | ||
sdkGoVersion: | ||
description: "sdk-go version (WITH the prepending v)." | ||
required: false | ||
type: string | ||
|
||
jobs: | ||
updates: | ||
|
@@ -49,8 +57,8 @@ jobs: | |
- uses: actions/setup-java@v4 | ||
if: ${{ inputs.sdkJavaVersion != '' }} | ||
with: | ||
distribution: 'temurin' | ||
java-version: '21' | ||
distribution: "temurin" | ||
java-version: "21" | ||
- name: Setup Gradle | ||
if: ${{ inputs.sdkJavaVersion != '' }} | ||
uses: gradle/actions/setup-gradle@v3 | ||
|
@@ -85,6 +93,14 @@ jobs: | |
field: TYPESCRIPT_SDK_VERSION | ||
value: ${{ inputs.sdkTypescriptVersion }} | ||
|
||
- name: Update restate.config.json with new Python sdk version | ||
uses: jossef/[email protected] | ||
if: ${{ inputs.sdkPythonVersion != '' }} | ||
with: | ||
file: restate.config.json | ||
field: PYTHON_SDK_VERSION | ||
value: ${{ inputs.sdkPythonVersion }} | ||
|
||
- name: Update restate.config.json with new Java sdk version | ||
uses: jossef/[email protected] | ||
if: ${{ inputs.sdkJavaVersion != '' }} | ||
|
@@ -105,6 +121,26 @@ jobs: | |
- name: Compile TypeScript code snippets | ||
run: npm install --prefix code_snippets/ts && npm run build --prefix code_snippets/ts | ||
|
||
# Upgrade Python code snippets if new version is provided | ||
- name: Upgrade Python Restate SDK | ||
if: github.event.inputs.sdkPythonVersion != '' | ||
run: sed -i 's/restate_sdk==[0-9A-Za-z.-]*/restate_sdk=="${{ inputs.sdkPythonVersion }}"/' "code_snippets/python/requirements.txt" | ||
|
||
# Test if Python code snippets compile | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.11' | ||
- name: Install dependencies and check Python code snippets | ||
run: | | ||
cd code_snippets/python | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install -r requirements.txt | ||
pip install mypy | ||
python3 -m mypy . | ||
deactivate | ||
|
||
# Upgrade Java code snippets if new version is provided | ||
- name: Find and replace restateVersion in build.gradle.kts for Java code snippets | ||
if: ${{ inputs.sdkJavaVersion != '' }} | ||
|
@@ -132,6 +168,26 @@ jobs: | |
repository: restatedev/sdk-java | ||
ref: v${{ inputs.sdkJavaVersion }} | ||
path: temp-sdk-java | ||
|
||
# Setup Go | ||
- uses: actions/setup-go@v5 | ||
if: ${{ inputs.sdkGoVersion != '' }} | ||
with: | ||
go-version: "1.21" | ||
|
||
# Upgrade Go code snippets if new version is provided | ||
- name: Bump sdk-go | ||
if: github.event.inputs.sdkGoVersion != '' | ||
working-directory: code_snippets/go | ||
run: | | ||
go get github.com/restatedev/sdk-go@${{ github.event.inputs.sdkGoVersion }} | ||
go mod tidy | ||
|
||
# Check Go code snippets | ||
- name: Test Go code snippets | ||
working-directory: code_snippets/go | ||
run: go test ./... | ||
|
||
- name: Run the runtime generate script | ||
if: ${{ inputs.sdkJavaVersion != '' }} | ||
run: | | ||
|
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 |
---|---|---|
|
@@ -23,3 +23,4 @@ yarn-error.log* | |
.yarn | ||
/code_snippets/ts/node_modules/ | ||
/code_snippets/ts/dist/ | ||
/code_snippets/python/venv/ |
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,80 @@ | ||
package concepts | ||
|
||
import restate "github.com/restatedev/sdk-go" | ||
|
||
type OrderProcessor struct{} | ||
|
||
// <start_here> | ||
// <mark_1> | ||
func (OrderProcessor) Process(ctx restate.ObjectContext, order Order) error { | ||
// </mark_1> | ||
// 1. Set status | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_CREATED) | ||
// </mark_4> | ||
|
||
// 2. Handle payment | ||
// <mark_5> | ||
token := restate.Rand(ctx).UUID().String() | ||
paid, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) { | ||
return paymentClnt.Charge(ctx, order.Id, token, order.TotalCost) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// </mark_5> | ||
|
||
if !paid { | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_REJECTED) | ||
// </mark_4> | ||
return nil | ||
} | ||
|
||
// 3. Wait until the requested preparation time | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_SCHEDULED) | ||
// </mark_4> | ||
if err := restate.Sleep(ctx, order.DeliveryDelay); err != nil { | ||
return err | ||
} | ||
|
||
// 4. Trigger preparation | ||
// <mark_3> | ||
preparationAwakeable := restate.Awakeable[restate.Void](ctx) | ||
// <mark_5> | ||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
return restate.Void{}, restaurant.Prepare(order.Id, preparationAwakeable.Id()) | ||
}); err != nil { | ||
return err | ||
} | ||
// </mark_5> | ||
// </mark_3> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_IN_PREPARATION) | ||
// </mark_4> | ||
|
||
// <mark_3> | ||
if _, err := preparationAwakeable.Result(); err != nil { | ||
return err | ||
} | ||
// </mark_3> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_SCHEDULING_DELIVERY) | ||
// </mark_4> | ||
|
||
// 5. Find a driver and start delivery | ||
// <mark_2> | ||
if _, err := restate.Object[restate.Void](ctx, "DeliveryManager", order.Id, "StartDelivery"). | ||
Request(order); err != nil { | ||
return err | ||
} | ||
// </mark_2> | ||
// <mark_4> | ||
restate.Set(ctx, "status", Status_DELIVERED) | ||
// </mark_4> | ||
|
||
return nil | ||
} | ||
|
||
// <end_here> |
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,50 @@ | ||
package concepts | ||
|
||
import ( | ||
"time" | ||
|
||
restate "github.com/restatedev/sdk-go" | ||
) | ||
|
||
type MyService struct{} | ||
|
||
/* | ||
// <start_rpc_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
greet, err := restate. | ||
// focus | ||
Service[string](ctx, "Greeter", "Greet"). | ||
// focus | ||
Request("Hi") | ||
return err | ||
} | ||
|
||
// <end_rpc_call> | ||
|
||
// <start_one_way_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
restate. | ||
// focus | ||
ServiceSend(ctx, "Greeter", "Greet"). | ||
// focus | ||
Send("Hi") | ||
return nil | ||
} | ||
|
||
// <end_one_way_call> | ||
*/ | ||
|
||
// <start_delayed_call> | ||
func (MyService) MyRestateHandler(ctx restate.Context) error { | ||
// focus | ||
restate. | ||
// focus | ||
ServiceSend(ctx, "Greeter", "Greet"). | ||
// focus | ||
Send("Hi", restate.WithDelay(1*time.Second)) | ||
return nil | ||
} | ||
|
||
// <end_delayed_call> |
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,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
restate "github.com/restatedev/sdk-go" | ||
"github.com/restatedev/sdk-go/server" | ||
) | ||
|
||
type RoleUpdate struct{} | ||
|
||
// <start_here> | ||
// <mark_2> | ||
func (RoleUpdate) ApplyRoleUpdate(ctx restate.Context, update UpdateRequest) error { | ||
// </mark_2> | ||
|
||
// <mark_1> | ||
success, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) { | ||
return applyUserRole(update.UserId, update.Role) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// </mark_1> | ||
// <mark_3> | ||
if !success { | ||
return nil | ||
} | ||
// </mark_3> | ||
|
||
// <mark_3> | ||
for _, permission := range update.Permissions { | ||
// </mark_3> | ||
// <mark_1> | ||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
return restate.Void{}, applyPermission(update.UserId, permission) | ||
}); err != nil { | ||
return err | ||
} | ||
// </mark_1> | ||
// <mark_3> | ||
} | ||
// </mark_3> | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if err := server.NewRestate(). | ||
Bind(restate.Reflect(RoleUpdate{})). | ||
Start(context.Background(), ":9080"); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// <end_here> | ||
|
||
type UserRole struct { | ||
RoleKey string | ||
RoleDescription string | ||
} | ||
|
||
type Permission struct { | ||
PermissionKey string | ||
Setting string | ||
} | ||
|
||
type UpdateRequest struct { | ||
UserId string | ||
Role UserRole | ||
Permissions []Permission | ||
} | ||
|
||
func applyUserRole( | ||
userId string, | ||
userRole UserRole, | ||
) (bool, error) { | ||
return true, nil | ||
} | ||
|
||
func applyPermission( | ||
userId string, | ||
permission Permission, | ||
) error { | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me this is not running?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved it in another PR