-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1039 from dapr/workflow
Reworked javascript workflow examples to use a webserver
- Loading branch information
Showing
9 changed files
with
416 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: 1 | ||
common: | ||
resourcesPath: ../../components | ||
apps: | ||
- appID: workflowApp | ||
appDirPath: ./order-processor/ | ||
appPort: 3000 | ||
daprHTTPPort: 3500 | ||
daprGRPCPort: 50001 | ||
command: ["npm", "run", "start:order-process-with-dapr-server"] |
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,10 @@ | ||
version: 1 | ||
common: | ||
resourcesPath: ../../components | ||
apps: | ||
- appID: workflowApp | ||
appDirPath: ./order-processor/ | ||
appPort: 3000 | ||
daprHTTPPort: 3500 | ||
daprGRPCPort: 50001 | ||
command: ["npm", "run", "start:order-process-with-express-server"] |
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
87 changes: 87 additions & 0 deletions
87
workflows/javascript/sdk/order-processor/appWithDaprServer.ts
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,87 @@ | ||
import { DaprWorkflowClient, WorkflowRuntime, DaprClient } from "@dapr/dapr"; | ||
import { InventoryItem, OrderPayload } from "./model"; | ||
import { notifyActivity, orderProcessingWorkflow, processPaymentActivity, requestApprovalActivity, reserveInventoryActivity, updateInventoryActivity } from "./orderProcessingWorkflow"; | ||
import { DaprServer, CommunicationProtocolEnum } from "@dapr/dapr"; | ||
import express from "express"; | ||
|
||
const daprHost = process.env.DAPR_HOST ?? "localhost"; // Dapr Sidecar Host | ||
const daprPort = process.env.DAPR_HTTP_PORT || "3500"; // Dapr Sidecar Port of this Example Server | ||
|
||
const app = express(); | ||
|
||
const daprServer = new DaprServer({ | ||
serverHost: "127.0.0.1", // App Host | ||
serverPort: process.env.APP_PORT || "3000", // App Port | ||
serverHttp: app, | ||
communicationProtocol: CommunicationProtocolEnum.HTTP, // Add this line | ||
clientOptions: { | ||
daprHost, | ||
daprPort | ||
} | ||
}); | ||
|
||
const daprClient = new DaprClient(); | ||
const workflowClient = new DaprWorkflowClient(); | ||
const workflowWorker = new WorkflowRuntime(); | ||
|
||
app.post("/start-workflow", async (req, res) => { | ||
|
||
const storeName = "statestore"; | ||
const inventory = new InventoryItem("item1", 100, 100); | ||
const key = inventory.itemName; | ||
|
||
await daprClient.state.delete(storeName, key); | ||
await daprClient.state.save(storeName, [ | ||
{ | ||
key: key, | ||
value: inventory, | ||
} | ||
]); | ||
|
||
const order = new OrderPayload("item1", 100, 10); | ||
|
||
// Schedule a new orchestration | ||
try { | ||
const id = await workflowClient.scheduleNewWorkflow(orderProcessingWorkflow, order); | ||
console.log(`Orchestration scheduled with ID: ${id}`); | ||
|
||
// Wait for orchestration completion | ||
const state = await workflowClient.waitForWorkflowCompletion(id, undefined, 30); | ||
|
||
var orchestrationResult = `Orchestration completed! Result: ${state?.serializedOutput}`; | ||
console.log(orchestrationResult); | ||
} catch (error) { | ||
console.error("Error scheduling or waiting for orchestration:", error); | ||
throw error; | ||
} | ||
|
||
res.send(orchestrationResult); | ||
}); | ||
|
||
async function start() { | ||
workflowWorker | ||
.registerWorkflow(orderProcessingWorkflow) | ||
.registerActivity(notifyActivity) | ||
.registerActivity(reserveInventoryActivity) | ||
.registerActivity(requestApprovalActivity) | ||
.registerActivity(processPaymentActivity) | ||
.registerActivity(updateInventoryActivity); | ||
|
||
// Wrap the worker startup in a try-catch block to handle any errors during startup | ||
try { | ||
await workflowWorker.start(); | ||
console.log("Workflow runtime started successfully"); | ||
} catch (error) { | ||
console.error("Error starting workflow runtime:", error); | ||
} | ||
|
||
// Initialize subscriptions before the server starts, the Dapr sidecar uses it. | ||
// This will also initialize the app server itself (removing the need for `app.listen` to be called). | ||
await daprServer.start(); | ||
}; | ||
|
||
start().catch((e) => { | ||
workflowWorker.stop(); | ||
console.error(e); | ||
process.exit(1); | ||
}); |
76 changes: 76 additions & 0 deletions
76
workflows/javascript/sdk/order-processor/appWithExpressServer.ts
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,76 @@ | ||
import { DaprWorkflowClient, WorkflowRuntime, DaprClient } from "@dapr/dapr"; | ||
import { InventoryItem, OrderPayload } from "./model"; | ||
import { notifyActivity, orderProcessingWorkflow, processPaymentActivity, requestApprovalActivity, reserveInventoryActivity, updateInventoryActivity } from "./orderProcessingWorkflow"; | ||
import express from "express"; | ||
|
||
const app = express(); | ||
|
||
const daprClient = new DaprClient(); | ||
const workflowClient = new DaprWorkflowClient(); | ||
const workflowWorker = new WorkflowRuntime(); | ||
|
||
app.post("/start-workflow", async (req, res) => { | ||
|
||
const storeName = "statestore"; | ||
const inventory = new InventoryItem("item1", 100, 100); | ||
const key = inventory.itemName; | ||
|
||
await daprClient.state.save(storeName, [ | ||
{ | ||
key: key, | ||
value: inventory, | ||
} | ||
]); | ||
|
||
const order = new OrderPayload("item1", 100, 10); | ||
|
||
// Schedule a new orchestration | ||
try { | ||
const id = await workflowClient.scheduleNewWorkflow(orderProcessingWorkflow, order); | ||
console.log(`Orchestration scheduled with ID: ${id}`); | ||
|
||
// Wait for orchestration completion | ||
const state = await workflowClient.waitForWorkflowCompletion(id, undefined, 30); | ||
|
||
var orchestrationResult = `Orchestration completed! Result: ${state?.serializedOutput}`; | ||
console.log(orchestrationResult); | ||
} catch (error) { | ||
console.error("Error scheduling or waiting for orchestration:", error); | ||
throw error; | ||
} | ||
|
||
res.send(orchestrationResult); | ||
}); | ||
|
||
async function start() { | ||
workflowWorker | ||
.registerWorkflow(orderProcessingWorkflow) | ||
.registerActivity(notifyActivity) | ||
.registerActivity(reserveInventoryActivity) | ||
.registerActivity(requestApprovalActivity) | ||
.registerActivity(processPaymentActivity) | ||
.registerActivity(updateInventoryActivity); | ||
|
||
// Wrap the worker startup in a try-catch block to handle any errors during startup | ||
try { | ||
await workflowWorker.start(); | ||
console.log("Workflow runtime started successfully"); | ||
} catch (error) { | ||
console.error("Error starting workflow runtime:", error); | ||
} | ||
}; | ||
|
||
const server = app.listen(process.env.APP_PORT || 3000, () => { | ||
console.log(`Example app listening on port APP_PORT or 3000`); | ||
}) | ||
|
||
process.on('SIGTERM', () => { | ||
workflowWorker.stop(); | ||
server.close(); | ||
}) | ||
|
||
start().catch((e) => { | ||
workflowWorker.stop(); | ||
console.error(e); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.