-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin to workflow docs Java SDK
- Loading branch information
Showing
7 changed files
with
255 additions
and
12 deletions.
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
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
64 changes: 64 additions & 0 deletions
64
code_snippets/kotlin/src/main/kotlin/develop/workflows/SignupWorkflow.kt
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,64 @@ | ||
package develop.workflows | ||
|
||
import dev.restate.sdk.annotation.Shared | ||
import dev.restate.sdk.annotation.Workflow | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder | ||
import dev.restate.sdk.kotlin.* | ||
import develop.workflows.Utils.Companion.sendEmailWithLink | ||
import kotlinx.serialization.Serializable | ||
|
||
// <start_here> | ||
@Workflow | ||
class SignupWorkflow { | ||
|
||
companion object { | ||
private val EMAIL_CLICKED = KtDurablePromiseKey.json<String>("email_clicked") | ||
private val STATUS = KtStateKey.json<String>("status") | ||
} | ||
|
||
@Workflow | ||
suspend fun run(ctx: WorkflowContext, email: Email): Boolean { | ||
val secret = ctx.random().nextUUID().toString() | ||
ctx.set(STATUS, "Generated secret") | ||
|
||
ctx.runBlock ("send email") { | ||
sendEmailWithLink(email, secret) | ||
} | ||
|
||
val clickSecret = ctx.promise(EMAIL_CLICKED) | ||
.awaitable() | ||
.await() | ||
ctx.set(STATUS, "Clicked email") | ||
|
||
return clickSecret == secret | ||
} | ||
|
||
@Shared | ||
suspend fun click(ctx: SharedWorkflowContext, secret: String) { | ||
ctx.promiseHandle(EMAIL_CLICKED).resolve(secret) | ||
} | ||
|
||
@Shared | ||
suspend fun getStatus(ctx: SharedWorkflowContext): String? { | ||
return ctx.get(STATUS) | ||
} | ||
} | ||
|
||
fun main() { | ||
RestateHttpEndpointBuilder | ||
.builder() | ||
.bind(SignupWorkflow()) | ||
.buildAndListen() | ||
} | ||
// <end_here> | ||
|
||
@Serializable | ||
data class Email(val email: String) | ||
|
||
class Utils { | ||
companion object { | ||
fun sendEmailWithLink(email: Email, secret: String){ | ||
|
||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
code_snippets/kotlin/src/main/kotlin/develop/workflows/UserManagementService.kt
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,27 @@ | ||
package develop.workflows | ||
|
||
import dev.restate.sdk.annotation.Handler; | ||
import dev.restate.sdk.annotation.VirtualObject; | ||
import dev.restate.sdk.kotlin.ObjectContext | ||
|
||
@VirtualObject | ||
class UserManagementService { | ||
// <start_here> | ||
@Handler | ||
suspend fun setup(ctx: ObjectContext, email: Email) { | ||
// focus(1:3) | ||
val result = SignupWorkflowClient.fromContext(ctx, "someone") | ||
.run(email) | ||
.await() | ||
|
||
} | ||
|
||
@Handler | ||
suspend fun queryStatus(ctx: ObjectContext) { | ||
// focus(1:3) | ||
val status = SignupWorkflowClient.fromContext(ctx, "someone") | ||
.getStatus() | ||
.await() | ||
} | ||
// <end_here> | ||
} |
2 changes: 2 additions & 0 deletions
2
code_snippets/kotlin/src/main/kotlin/develop/workflows/Utils.kt
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,2 @@ | ||
package develop.workflows | ||
|
43 changes: 43 additions & 0 deletions
43
code_snippets/kotlin/src/main/kotlin/develop/workflows/WorkflowSubmitter.kt
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,43 @@ | ||
package develop.workflows | ||
|
||
import dev.restate.sdk.client.Client | ||
import dev.restate.sdk.client.SendResponse | ||
|
||
class WorkflowSubmitter { | ||
|
||
suspend fun submitWorkflow(email: Email){ | ||
// <start_submit> | ||
val restate = Client.connect("http://localhost:8080") | ||
val handle: SendResponse = SignupWorkflowClient | ||
.fromClient(restate, "someone") | ||
.submit(email) | ||
// <end_submit> | ||
|
||
// <start_query> | ||
val status = SignupWorkflowClient | ||
.fromClient(restate, "someone") | ||
.getStatus() | ||
// <end_query> | ||
|
||
// <start_interact> | ||
|
||
// <end_query> | ||
|
||
// <start_interact> | ||
// Option 1: attach and wait for result | ||
val result = SignupWorkflowClient | ||
.fromClient(restate, "someone") | ||
.workflowHandle() | ||
.attach() | ||
|
||
// Option 2: peek to check if ready | ||
val peekOutput = SignupWorkflowClient | ||
.fromClient(restate, "someone") | ||
.workflowHandle() | ||
.output | ||
if (peekOutput.isReady) { | ||
val result2 = peekOutput.value | ||
} | ||
// <end_interact> | ||
} | ||
} |
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