Skip to content
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

Python docs #446

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
54e86d9
Add docs/develop/go
jackkleeman Aug 15, 2024
a227806
Update Cloud docs
jackkleeman Aug 15, 2024
cdf3fba
Use main() in service and virtual object
jackkleeman Aug 15, 2024
7070513
Go quickstart
jackkleeman Aug 15, 2024
a39fd74
Update Go SDK
jackkleeman Aug 16, 2024
0926d23
Add tour
jackkleeman Aug 16, 2024
109b345
Small updates
gvdongen Aug 21, 2024
6e55b90
Updates to Tour of Restate Python
gvdongen Aug 22, 2024
35da53f
Tour of Restate Python fixes
gvdongen Aug 22, 2024
956a12f
Update for latest go sdk
jackkleeman Aug 22, 2024
6282c9d
Update docs/develop/go/journaling-results.mdx
jackkleeman Aug 22, 2024
29a8a08
Review comments
jackkleeman Aug 22, 2024
eacec82
github prerelease workflow and fixes for mypy
gvdongen Aug 22, 2024
2ed99a9
Add Python to test-build github workflow
gvdongen Aug 22, 2024
b1ae453
Try bumping Python to 3.12 for github workflows
gvdongen Aug 22, 2024
05d5e71
*string for default check
jackkleeman Aug 22, 2024
08dbb8f
Add go to github workflows
jackkleeman Aug 22, 2024
6be1506
Add logo to overview
jackkleeman Aug 22, 2024
b6800c8
Go in concepts pages
jackkleeman Aug 22, 2024
bb80398
Add go microservice use case
jackkleeman Aug 22, 2024
3e64e5d
Event processing go docs
jackkleeman Aug 22, 2024
4bb79f3
Reduce tab spacing
jackkleeman Aug 22, 2024
0e539bb
Upgrade Python
gvdongen Aug 27, 2024
2ade447
Java formatting
gvdongen Aug 27, 2024
19d8d00
Fix paths to code snippets
gvdongen Aug 27, 2024
39183aa
Init Python docs, and adapt code loader to accept python comments, an…
gvdongen Aug 27, 2024
5788ee0
Python docs
gvdongen Aug 28, 2024
4e0909a
Add Python docs and highlighting on serving with identity keys
gvdongen Aug 28, 2024
b415c6a
Fix broken links
gvdongen Aug 28, 2024
e80c3c0
Fix broken links
gvdongen Aug 28, 2024
0eae305
Deploy Cloud docs for Python
gvdongen Aug 28, 2024
2db58e7
Fix mypy typing errors
gvdongen Aug 28, 2024
58f71a4
Formatting
gvdongen Aug 28, 2024
b837e86
Add Python to overview page
gvdongen Aug 28, 2024
a97c09e
Merge branch 'refs/heads/golang' into python_tour
gvdongen Aug 28, 2024
2da59b2
Merge branch 'refs/heads/python_tour' into python_docs
gvdongen Aug 28, 2024
7d799ed
Fix logos on overview page
gvdongen Aug 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != '' }}
Expand All @@ -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 != '' }}
Expand Down Expand Up @@ -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: |
Expand Down
36 changes: 32 additions & 4 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
- name: Test build website
run: yarn build


# Test TypeScript code snippets
- name: Compile TypeScript code snippets
run: npm install --prefix code_snippets/ts && npm run build --prefix code_snippets/ts
Expand All @@ -41,11 +40,29 @@ jobs:
exit 1
fi


# Setup Python
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

# Test if Python code snippets compile
- 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

# Setup Java
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
distribution: "temurin"
java-version: "21"

# Check Java code snippets
- name: Test Java code snippets
Expand All @@ -59,4 +76,15 @@ jobs:
uses: gradle/gradle-build-action@v2
with:
arguments: check
build-root-directory: code_snippets/kotlin
build-root-directory: code_snippets/kotlin

# Setup Go
- uses: actions/setup-go@v5
if: github.event.inputs.sdkGoVersion != ''
with:
go-version: "1.21"

# Check Go code snippets
- name: Test Go code snippets
working-directory: code_snippets/go
run: go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ yarn-error.log*
.yarn
/code_snippets/ts/node_modules/
/code_snippets/ts/dist/
/code_snippets/python/venv/
80 changes: 80 additions & 0 deletions code_snippets/go/concepts/foodordering.go
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>
50 changes: 50 additions & 0 deletions code_snippets/go/concepts/invocations.go
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>
87 changes: 87 additions & 0 deletions code_snippets/go/concepts/services/main.go
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
}
Loading