-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: slinkydeveloper <[email protected]>
- Loading branch information
1 parent
411f604
commit 1de00c5
Showing
39 changed files
with
134 additions
and
121 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
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
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
*/ | ||
|
||
import * as restate from "@restatedev/restate-sdk"; | ||
import { workflow as wf } from "@restatedev/restate-sdk"; | ||
import * as restateClients from "@restatedev/restate-sdk-clients"; | ||
import { createUserEntry, sendEmailWithLink } from "./utils/workflow_stubs"; | ||
|
||
// | ||
|
@@ -23,87 +23,90 @@ import { createUserEntry, sendEmailWithLink } from "./utils/workflow_stubs"; | |
// Workflow instances always have a unique ID that identifies the workflow execution. | ||
// Each workflow instance (ID) can run only once (to success or failure). | ||
// | ||
const myWorkflow = wf.workflow("usersignup", { | ||
const myWorkflow = restate.workflow({ | ||
// --- The workflow logic is in the run() function --- | ||
name: "usersignup", | ||
handlers: { | ||
run: async (ctx: restate.WorkflowContext, params: { name: string; email: string }) => { | ||
const {name, email} = params; | ||
const userId = ctx.key; | ||
|
||
// publish state, for the world to see our progress | ||
ctx.set("stage", "Creating User"); | ||
|
||
// use all the standard durable execution features here | ||
await ctx.run(() => createUserEntry({userId, name})); | ||
|
||
ctx.set("stage", "Email Verification"); | ||
|
||
// send the email with the verification secret | ||
const secret = await ctx.run(() => crypto.randomUUID()); | ||
ctx.run(() => sendEmailWithLink({email, secret})); | ||
|
||
try { | ||
// the promise here is resolved or rejected by the additional workflow methods below | ||
const clickSecret = await ctx.promise<string>("email-link"); | ||
if (clickSecret !== secret) { | ||
throw new restate.TerminalError("Wrong secret from email link"); | ||
} | ||
} catch (err: any) { | ||
ctx.set("stage", "Verification failed: " + err.message); | ||
return; | ||
} | ||
|
||
run: async (ctx: wf.WfContext, params: { name: string; email: string }) => { | ||
const { name, email } = params; | ||
const userId = ctx.workflowId(); | ||
|
||
// publish state, for the world to see our progress | ||
ctx.set("stage", "Creating User"); | ||
ctx.set("stage", "User verified"); | ||
}, | ||
|
||
// use all the standard durable execution features here | ||
await ctx.run(() => createUserEntry({ userId, name })); | ||
// --- various interactions for queries and signals --- | ||
|
||
ctx.set("stage", "Email Verification"); | ||
getStage: (ctx: restate.WorkflowSharedContext) => { | ||
// read the state to get the stage where the workflow is | ||
return ctx.get("stage"); | ||
}, | ||
|
||
// send the email with the verification secret | ||
const secret = await ctx.run(() => crypto.randomUUID()); | ||
ctx.run(() => sendEmailWithLink({ email, secret })); | ||
verifyEmail: async (ctx: restate.WorkflowSharedContext, request: { secret: string }) => { | ||
// resolve the durable promise to let the awaiter know | ||
await ctx.promise<string>("email-link").resolve(request.secret); | ||
}, | ||
|
||
try { | ||
// the promise here is resolved or rejected by the additional workflow methods below | ||
const clickSecret = await ctx.promise<string>("email-link"); | ||
if (clickSecret !== secret) { | ||
throw new restate.TerminalError("Wrong secret from email link"); | ||
} | ||
} catch (err: any) { | ||
ctx.set("stage", "Verification failed: " + err.message); | ||
return; | ||
} | ||
|
||
ctx.set("stage", "User verified"); | ||
}, | ||
|
||
// --- various interactions for queries and signals --- | ||
|
||
getStage: (ctx: wf.SharedWfContext) => { | ||
// read the state to get the stage where the workflow is | ||
return ctx.get("stage"); | ||
}, | ||
|
||
verifyEmail: async (ctx: wf.SharedWfContext, request: { secret: string }) => { | ||
// resolve the durable promise to let the awaiter know | ||
ctx.promise<string>("email-link").resolve(request.secret); | ||
}, | ||
|
||
abortVerification: async (ctx: wf.SharedWfContext) => { | ||
// failing the durable promise will throw an Error for the awaiting thread | ||
ctx.promise<string>("email-link").reject("User aborted verification"); | ||
}, | ||
abortVerification: async (ctx: restate.WorkflowSharedContext) => { | ||
// failing the durable promise will throw an Error for the awaiting thread | ||
await ctx.promise<string>("email-link").reject("User aborted verification"); | ||
}, | ||
} | ||
}); | ||
|
||
export const workflowApi = myWorkflow.api; | ||
restate.endpoint().bind(myWorkflow).listen(); | ||
|
||
export type WorkflowApi = typeof myWorkflow; | ||
|
||
// ---------- ⬆️⬆️ deploy this as a container, lambda, etc. ⬆️⬆️ ---------- | ||
|
||
// start it via an HTTP call. | ||
// `curl restate:8080/usersignup/submit --json '{ "request": { | ||
// "workflowId": "signup-userid1", | ||
// "name": "Bob", | ||
// "email": "[email protected]" | ||
// } }' | ||
// `curl restate:8080/usersignup/signup-userid1/run/send --json '{ "name": "Bob", "email": "bob@builder.com" }' | ||
|
||
// or programmatically | ||
async function signupUser(userId: string, name: string, email: string) { | ||
const rs = restate.clients.connect("http://restate:8080"); | ||
const { client, status } = await rs.submitWorkflow(workflowApi, "signup-" + userId, { | ||
const rs = restateClients.connect({ url: "http://restate:8080" }); | ||
const workflow: WorkflowApi = { name: "usersignup" }; | ||
const workflowClient = rs.workflowClient(workflow, "signup-" + userId); | ||
const { status } = await workflowClient.workflowSubmit({ | ||
name, | ||
email, | ||
}); | ||
|
||
if (status != wf.WorkflowStartResult.STARTED) { | ||
if (status != "Accepted") { | ||
throw new Error("User ID already taken"); | ||
} | ||
|
||
await client.result(); | ||
await workflowClient.workflowAttach(); | ||
} | ||
|
||
// interact with the workflow from any other code | ||
async function verifyEmail(userId: string, emailSecret: string) { | ||
const rs = restate.clients.connect("http://restate:8080"); | ||
const { client, status } = await rs.connectToWorkflow(workflowApi, "signup-" + userId); | ||
const rs = restateClients.connect({ url: "http://restate:8080" }); | ||
const workflow: WorkflowApi = { name: "usersignup" }; | ||
const workflowClient = rs.workflowClient(workflow, "signup-" + userId); | ||
|
||
client.workflowInterface().verifyEmail({ secret: emailSecret }); | ||
workflowClient.verifyEmail({ secret: emailSecret }); | ||
} |
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
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
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
Oops, something went wrong.