-
Notifications
You must be signed in to change notification settings - Fork 21
New quickstart #214
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
Merged
Merged
New quickstart #214
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
64fab4c
new greeter template to show durable execution
gvdongen 8c5df86
new greeter template for Java templates
gvdongen dd8bfe4
new greeter template for Kotlin templates
gvdongen d6de84f
new greeter template for Go templates
gvdongen 2fcc96e
new greeter template for Python templates
gvdongen 421b30d
new greeter template for Rust templates
gvdongen 33f9818
Fix comments
gvdongen 98a2766
Add ghost to error messages
gvdongen 088d697
Fix wrong imports in templates
gvdongen eba7fff
Remove licenses of templates because they are shown as code snippets …
gvdongen ae8808d
Make code snippets more concise
gvdongen 45d6dcf
Use pydantic for Python template
gvdongen b498456
throw runtime exception in java templates
gvdongen 3c65597
Fix import in TS deno template
gvdongen 10ed7eb
Kotlin template: Rename to utils.kt
gvdongen 81d3ae6
Upgrade go version
gvdongen a4cb0f9
Merge branch 'main' into new_quickstart
gvdongen d3e5029
Fix rust template
gvdongen 283ec6f
Fix Java templates issues
gvdongen 15c96c0
Fix cloudflare workers template
gvdongen 0c4d755
Fix package name quarkus template
gvdongen 8569a6e
Fix Rust shuttle template
gvdongen 1500e34
Clean imports
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 hidden or 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 hidden or 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,19 @@ | ||
|
||
// You can remove this file. | ||
// It's only purpose is providing stubs for the template. | ||
|
||
export function sendNotification(greetingId: string, name: string) { | ||
if (Math.random() < 0.5) { // 50% chance of failure | ||
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`); | ||
throw new Error(`Failed to send notification ${greetingId} - ${name}`); | ||
} | ||
console.log(`Notification sent: ${greetingId} - ${name}`); | ||
} | ||
|
||
export function sendReminder(greetingId: string) { | ||
if (Math.random() < 0.5) { // 50% chance of failure | ||
console.error(`👻 Failed to send reminder: ${greetingId}`); | ||
throw new Error(`Failed to send reminder: ${greetingId}`); | ||
} | ||
console.log(`Reminder sent: ${greetingId}`); | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import { Context, endpoint, service } from "@restatedev/restate-sdk-cloudflare-workers/fetch"; | ||
import * as restate from "@restatedev/restate-sdk-cloudflare-workers/fetch"; | ||
import { sendNotification, sendReminder } from "./utils.js"; | ||
|
||
// Template of a Restate service and handler | ||
// | ||
// Have a look at the TS QuickStart: https://docs.restate.dev/get_started/quickstart?sdk=ts | ||
// | ||
export default restate | ||
.endpoint() | ||
.bind( | ||
restate.service({ | ||
name: "greeter", | ||
handlers: { | ||
greet: async (ctx: restate.Context, name: string) => { | ||
// Durably execute a set of steps; resilient against failures | ||
const greetingId = ctx.rand.uuidv4(); | ||
await ctx.run(() => sendNotification(greetingId, name)); | ||
await ctx.sleep(1000); | ||
await ctx.run(() => sendReminder(greetingId)); | ||
|
||
const greeter = service({ | ||
name: "greeter", | ||
handlers: { | ||
greet: async (ctx: Context, greeting: string) => { | ||
return `${greeting}!`; | ||
}, | ||
}, | ||
}); | ||
|
||
export default endpoint().bind(greeter).handler(); | ||
// Respond to caller | ||
return `You said hi to ${name}!`; | ||
}, | ||
}, | ||
}), | ||
) | ||
.handler(); |
This file contains hidden or 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,20 @@ | ||
// You can remove this file. | ||
// It's only purpose is providing stubs for the template. | ||
|
||
export function sendNotification(greetingId: string, name: string) { | ||
if (Math.random() < 0.5) { | ||
// 50% chance of failure | ||
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`); | ||
throw new Error(`Failed to send notification ${greetingId} - ${name}`); | ||
} | ||
console.log(`Notification sent: ${greetingId} - ${name}`); | ||
} | ||
|
||
export function sendReminder(greetingId: string) { | ||
if (Math.random() < 0.5) { | ||
// 50% chance of failure | ||
console.error(`👻 Failed to send reminder: ${greetingId}`); | ||
throw new Error(`Failed to send reminder: ${greetingId}`); | ||
} | ||
console.log(`Reminder sent: ${greetingId}`); | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,23 +1,26 @@ | ||
import { | ||
Context, | ||
endpoint, | ||
service, | ||
} from "npm:@restatedev/restate-sdk@^1.4.0/fetch"; | ||
import * as restate from "npm:@restatedev/restate-sdk/fetch"; | ||
import { sendNotification, sendReminder } from "./utils.ts"; | ||
|
||
// Template of a Restate service and handler | ||
// | ||
// Have a look at the TS QuickStart: https://docs.restate.dev/get_started/quickstart?sdk=ts | ||
// | ||
const handler = restate | ||
.endpoint() | ||
.bind( | ||
restate.service({ | ||
name: "Greeter", | ||
handlers: { | ||
greet: async (ctx: restate.Context, name: string) => { | ||
// Durably execute a set of steps; resilient against failures | ||
const greetingId = ctx.rand.uuidv4(); | ||
await ctx.run(() => sendNotification(greetingId, name)); | ||
await ctx.sleep(1000); | ||
await ctx.run(() => sendReminder(greetingId)); | ||
|
||
const greeter = service({ | ||
name: "Greeter", | ||
handlers: { | ||
greet: async (ctx: Context, greeting: string) => { | ||
return `${greeting}!`; | ||
}, | ||
}, | ||
}); | ||
|
||
const handler = endpoint().bind(greeter).bidirectional().handler(); | ||
// Respond to caller | ||
return `You said hi to ${name}!`; | ||
}, | ||
}, | ||
}), | ||
) | ||
.bidirectional() | ||
.handler(); | ||
|
||
Deno.serve({ port: 9080 }, handler.fetch); |
This file contains hidden or 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,20 @@ | ||
// You can remove this file. | ||
// It's only purpose is providing stubs for the template. | ||
|
||
export function sendNotification(greetingId: string, name: string) { | ||
if (Math.random() < 0.5) { | ||
// 50% chance of failure | ||
console.error(`👻 Failed to send notification: ${greetingId} - ${name}`); | ||
throw new Error(`Failed to send notification ${greetingId} - ${name}`); | ||
} | ||
console.log(`Notification sent: ${greetingId} - ${name}`); | ||
} | ||
|
||
export function sendReminder(greetingId: string) { | ||
if (Math.random() < 0.5) { | ||
// 50% chance of failure | ||
console.error(`👻 Failed to send reminder: ${greetingId}`); | ||
throw new Error(`Failed to send reminder: ${greetingId}`); | ||
} | ||
console.log(`Reminder sent: ${greetingId}`); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,14 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
restate "github.com/restatedev/sdk-go" | ||
"time" | ||
) | ||
|
||
// Greeter is a struct which represents a Restate service; reflection will turn exported methods into service handlers | ||
type Greeter struct{} | ||
|
||
func (Greeter) Greet(ctx restate.Context, greeting string) (string, error) { | ||
return fmt.Sprintf("%s!", greeting), nil | ||
func (Greeter) Greet(ctx restate.Context, name string) (string, error) { | ||
// Durably execute a set of steps; resilient against failures | ||
greetingId := restate.Rand(ctx).UUID().String() | ||
|
||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
return restate.Void{}, SendNotification(greetingId, name) | ||
}); err != nil { | ||
return "", err | ||
} | ||
|
||
if err := restate.Sleep(ctx, 1*time.Second); err != nil { | ||
return "", err | ||
} | ||
|
||
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) { | ||
gvdongen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return restate.Void{}, SendReminder(greetingId) | ||
}); err != nil { | ||
return "", err | ||
} | ||
|
||
// Respond to caller | ||
return "You said hi to " + name + "!", nil | ||
} |
This file contains hidden or 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,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
func SendNotification(greetingId string, name string) error { | ||
if rand.Float32() < 0.5 { // 50% chance of failure | ||
fmt.Printf("👻 Failed to send notification: %s - %s\n", greetingId, name) | ||
return fmt.Errorf("failed to send notification: %s - %s", greetingId, name) | ||
} | ||
fmt.Printf("Notification sent: %s - %s\n", greetingId, name) | ||
return nil | ||
} | ||
|
||
func SendReminder(greetingId string) error { | ||
if rand.Float32() < 0.5 { // 50% chance of failure | ||
fmt.Printf("👻 Failed to send reminder: %s\n", greetingId) | ||
return fmt.Errorf("failed to send reminder: %s", greetingId) | ||
} | ||
fmt.Printf("Reminder sent: %s\n", greetingId) | ||
return nil | ||
} |
This file contains hidden or 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
45 changes: 21 additions & 24 deletions
45
templates/java-gradle/src/main/java/my/example/Greeter.java
This file contains hidden or 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 |
---|---|---|
@@ -1,36 +1,33 @@ | ||
/* | ||
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH | ||
* | ||
* This file is part of the Restate examples, | ||
* which is released under the MIT license. | ||
* | ||
* You can find a copy of the license in the file LICENSE | ||
* in the root directory of this repository or package or at | ||
* https://github.com/restatedev/examples/ | ||
*/ | ||
|
||
package my.example; | ||
|
||
import dev.restate.sdk.Context; | ||
import dev.restate.sdk.annotation.Handler; | ||
import dev.restate.sdk.annotation.Service; | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder; | ||
|
||
/** | ||
* Template of a Restate service and handler | ||
* Have a look at the Java QuickStart to learn how to run this: https://docs.restate.dev/get_started/quickstart?sdk=java | ||
*/ | ||
import java.time.Duration; | ||
|
||
import static my.example.Utils.sendNotification; | ||
import static my.example.Utils.sendReminder; | ||
|
||
@Service | ||
public class Greeter { | ||
|
||
@Handler | ||
public String greet(Context ctx, String greeting) { | ||
return "Hello " + greeting; | ||
} | ||
@Handler | ||
public String greet(Context ctx, String name) { | ||
// Durably execute a set of steps; resilient against failures | ||
String greetingId = ctx.random().nextUUID().toString(); | ||
ctx.run(() -> sendNotification(greetingId, name)); | ||
ctx.sleep(Duration.ofMillis(1000)); | ||
ctx.run(() -> sendReminder(greetingId)); | ||
|
||
// Respond to caller | ||
return "You said hi to " + name + "!"; | ||
} | ||
|
||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder() | ||
.bind(new Greeter()) | ||
.buildAndListen(); | ||
} | ||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder() | ||
.bind(new Greeter()) | ||
.buildAndListen(); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.