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

Quickstart videos #450

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 get_started.quickstart;

import dev.restate.sdk.Context;
import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;

import java.time.Duration;

// <start_here>
@Service
public class Greeter {

@Handler
public String greet(Context ctx, String text) {
// this is a persistent workflow step. the result of the function is
// durably committed before it is returned and further steps can execute
String greeting = ctx.run(JsonSerdes.STRING,
() -> (Math.random() < 0.5) ? "Hello" : "Howdy");

// this is a delay during which the code may suspend (if running on FaaS)
ctx.sleep(Duration.ofMillis(2000));

return greeting + " - " + text;
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new Greeter())
.buildAndListen();
}
}
// <end_here>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 get_started.quickstart;

import dev.restate.sdk.JsonSerdes;
import dev.restate.sdk.ObjectContext;
import dev.restate.sdk.annotation.Handler;
import dev.restate.sdk.annotation.VirtualObject;
import dev.restate.sdk.common.StateKey;
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder;

import java.time.Duration;

// <start_here>
@VirtualObject
public class GreeterObject {

private static final StateKey<Long> COUNT =
StateKey.of("count", JsonSerdes.LONG);

@Handler
public String greet(ObjectContext ctx, String text) {
String greeting = ctx.run(JsonSerdes.STRING,
() -> (Math.random() < 0.5) ? "Hello" : "Howdy");

ctx.sleep(Duration.ofMillis(2000));

var count = ctx.get(COUNT).orElse(0L);
ctx.set(COUNT, count + 1);

String name = ctx.key();
return String.format("%s %s - %s", greeting, name, text);
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.bind(new Greeter())
.buildAndListen();
}
}
// <end_here>
5 changes: 1 addition & 4 deletions code_snippets/kotlin/src/main/kotlin/develop/Greeter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder
import dev.restate.sdk.kotlin.KtStateKey
import dev.restate.sdk.kotlin.ObjectContext

// withClass tooltip java-overview-virtual-object
@VirtualObject
class Greeter {

// withClass(1:3) tooltip java-overview-state-key
companion object {
private val COUNT = KtStateKey.json<Int>("count")
}

// withClass tooltip java-overview-virtual-object-handler
@Handler
suspend fun greet(ctx: ObjectContext, greeting: String): String {
// Get the count and increment it
val count = ctx.get(COUNT) ?: 1
val count = ctx.get(COUNT) ?: 0
ctx.set(COUNT, count + 1)

// Send the response back
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 get_started

import dev.restate.sdk.annotation.Handler
import dev.restate.sdk.annotation.Service
import dev.restate.sdk.kotlin.Context
import dev.restate.sdk.kotlin.KtSerdes
import kotlin.time.Duration.Companion.seconds

// <start_here>
@Service
class Greeter {
@Handler
suspend fun greet(ctx: Context, text: String): String {
// this is a persistent workflow step. the result of the function is
// durably committed before it is returned and further steps can execute
val greeting: String = ctx.runBlock(
KtSerdes.json<String>()
) { if ((Math.random() < 0.5)) "Hello" else "Howdy" }

// this is a delay during which the code may suspend (if running on FaaS)
ctx.sleep(2.seconds)

return "$greeting - $text"
}
}

fun main() {
RestateHttpEndpointBuilder.builder().bind(Greeter()).buildAndListen()
}
// <end_here>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 get_started

import dev.restate.sdk.annotation.Handler
import dev.restate.sdk.annotation.VirtualObject
import dev.restate.sdk.common.StateKey
import dev.restate.sdk.kotlin.KtSerdes
import dev.restate.sdk.kotlin.KtStateKey
import dev.restate.sdk.kotlin.ObjectContext
import kotlin.time.Duration.Companion.seconds

// <start_here>
@VirtualObject
class GreeterObject {

companion object {
private val COUNT = KtStateKey.json<Long>("count")
}

@Handler
suspend fun greet(ctx: ObjectContext, text: String): String {
val greeting: String = ctx.runBlock(
KtSerdes.json<String>()
) { if ((Math.random() < 0.5)) "Hello" else "Howdy" }

ctx.sleep(2.seconds)

val count = ctx.get(COUNT) ?: 0
ctx.set(COUNT, count + 1)

val name = ctx.key()
return "$greeting $name - $text"
}
}

fun main() {
RestateHttpEndpointBuilder.builder().bind(Greeter()).buildAndListen()
}
// <end_here>
Empty file.
23 changes: 23 additions & 0 deletions code_snippets/python/src/get_started/quickstart/greeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import random
from datetime import timedelta

import restate
from restate import Context, Service

# <start_here>
greeter = Service("Greeter")


@greeter.handler()
async def greet(ctx: Context, text: str) -> str:
# this is a persistent workflow step. the result of the function is
# durably committed before it is returned and further steps can execute
greeting = await ctx.run("greeting", lambda: "Hello" if random.Random().random() < 0.5 else "Howdy")

# this is a delay during which the code may suspend (if running on FaaS)
await ctx.sleep(timedelta(milliseconds=2000))

return f"${greeting} ${text}"

app = restate.app([greeter])
# <end_here>
24 changes: 24 additions & 0 deletions code_snippets/python/src/get_started/quickstart/greeterobject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import random
from datetime import timedelta

import restate
from restate import Context, VirtualObject, ObjectContext

# <start_here>
greeter = VirtualObject("Greeter")


@greeter.handler()
async def greet(ctx: ObjectContext, text: str) -> str:
greeting = await ctx.run("greeting", lambda: "Hello" if random.Random().random() < 0.5 else "Howdy")

await ctx.sleep(timedelta(milliseconds=2000))

count = await ctx.get("count") or 0
ctx.set("count", count + 1)

name = ctx.key()
return f"${greeting} ${name} - ${text}"

app = restate.app([greeter])
# <end_here>
24 changes: 24 additions & 0 deletions code_snippets/ts/src/get_started/quickstart/greeter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as restate from "@restatedev/restate-sdk";
import {
Context,
} from "@restatedev/restate-sdk";

// <start_here>
const greeter = restate.object({
name: "Greeter",
handlers: {
greet: async (ctx: Context, text: string) => {
// this is a persistent workflow step. the result of the function is
// durably committed before it is returned and further steps can execute
const greeting = ctx.run(() =>
Math.random() < 0.5 ? "Hello!" : "Howdy!");

// this is a delay during which the code may suspend (if running on FaaS)
ctx.sleep(2000);

return `${greeting} - ${text}`;
}
},
});
// <end_here>

25 changes: 25 additions & 0 deletions code_snippets/ts/src/get_started/quickstart/greeterobject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as restate from "@restatedev/restate-sdk";
import {
ObjectContext,
} from "@restatedev/restate-sdk";

// <start_here>
const greeter = restate.object({
name: "Greeter",
handlers: {
greet: async (ctx: ObjectContext, text: string) => {
const greeting = ctx.run(() =>
Math.random() < 0.5 ? "Hello!" : "Howdy!");

ctx.sleep(2000);

const count = (await ctx.get<number>("count")) ?? 0;
ctx.set("count", count + 1);
const name = ctx.key;

return `${greeting} ${name} - ${text}`;
}
},
});
// <end_here>

Loading
Loading