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

Kaibocai/update grpcclient #33

Merged
merged 11 commits into from
Jan 8, 2024
Merged
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
18 changes: 6 additions & 12 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@
"amd": true,
"node": true
},
"ignorePatterns": ["src/version.ts", "jest.config.js"],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-types": "off",
"no-constant-condition": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"varsIgnorePattern": "^_",
"argsIgnorePattern": "^_"
}
],
"header/header": [
"error",
"block",
[
"",
{ "pattern": "Copyright \\(c\\) Microsoft Corporation.", "template": "Copyright (c) Microsoft Corporation." },
"Licensed under the MIT License.",
""
// ... rest of the header
],
2
]
"header/header": [2, "config/header.ts"]
}
}
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ temp/
build/

# version file is auto-generated
src/version.ts
src/version.ts

### vscode ###
.vscode
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
2 changes: 2 additions & 0 deletions config/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
11 changes: 7 additions & 4 deletions examples/activity-sequence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { TaskHubGrpcClient } from "../src/client";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TaskHubGrpcClient } from "../src/client/client";
import { ActivityContext } from "../src/task/context/activity-context";
import { OrchestrationContext } from "../src/task/context/orchestration-context";
import { TOrchestrator } from "../src/types/orchestrator.type";
Expand All @@ -8,8 +11,8 @@ import { TaskHubGrpcWorker } from "../src/worker/task-hub-grpc-worker";
(async () => {
// Update the gRPC client and worker to use a local address and port
const grpcServerAddress = "localhost:4001";
let taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(grpcServerAddress);
let taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(grpcServerAddress);
const taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(grpcServerAddress);
const taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(grpcServerAddress);

const hello = async (_: ActivityContext, name: string) => {
return `Hello ${name}!`;
Expand Down Expand Up @@ -41,7 +44,7 @@ import { TaskHubGrpcWorker } from "../src/worker/task-hub-grpc-worker";

// Schedule a new orchestration
try {
const id = await taskHubClient.scheduleNewOrchestration(sequence, 1);
const id = await taskHubClient.scheduleNewOrchestration(sequence);
console.log(`Orchestration scheduled with ID: ${id}`);

// Wait for orchestration completion
Expand Down
11 changes: 7 additions & 4 deletions examples/fanout-fanin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { TaskHubGrpcClient } from "../src/client";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TaskHubGrpcClient } from "../src/client/client";
import { whenAll } from "../src/task";
import { ActivityContext } from "../src/task/context/activity-context";
import { OrchestrationContext } from "../src/task/context/orchestration-context";
Expand All @@ -10,14 +13,14 @@ import { TaskHubGrpcWorker } from "../src/worker/task-hub-grpc-worker";
(async () => {
// Update the gRPC client and worker to use a local address and port
const grpcServerAddress = "localhost:4001";
let taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(grpcServerAddress);
let taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(grpcServerAddress);
const taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(grpcServerAddress);
const taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(grpcServerAddress);

function getRandomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

async function getWorkItemsActivity(context: ActivityContext): Promise<string[]> {
async function getWorkItemsActivity(_: ActivityContext): Promise<string[]> {
const count: number = getRandomInt(2, 10);
console.log(`generating ${count} work items...`);

Expand Down
113 changes: 113 additions & 0 deletions examples/human_interaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TaskHubGrpcClient } from "../src/client/client";
import { whenAny } from "../src/task";
import { ActivityContext } from "../src/task/context/activity-context";
import { OrchestrationContext } from "../src/task/context/orchestration-context";
import { Task } from "../src/task/task";
import { TOrchestrator } from "../src/types/orchestrator.type";
import { TaskHubGrpcWorker } from "../src/worker/task-hub-grpc-worker";
import * as readlineSync from "readline-sync";

// Wrap the entire code in an immediately-invoked async function
(async () => {
class Order {
cost: number;
product: string;
quantity: number;
constructor(cost: number, product: string, quantity: number) {
this.cost = cost;
this.product = product;
this.quantity = quantity;
}
}

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

// Update the gRPC client and worker to use a local address and port
const grpcServerAddress = "localhost:4001";
const taskHubClient: TaskHubGrpcClient = new TaskHubGrpcClient(grpcServerAddress);
const taskHubWorker: TaskHubGrpcWorker = new TaskHubGrpcWorker(grpcServerAddress);

//Activity function that sends an approval request to the manager
const sendApprovalRequest = async (_: ActivityContext, order: Order) => {
// Simulate some work that takes an amount of time
await sleep(3000);
console.log(`Sending approval request for order: ${order.product}`);
};

// Activity function that places an order
const placeOrder = async (_: ActivityContext, order: Order) => {
console.log(`Placing order: ${order.product}`);
};

// Orchestrator function that represents a purchase order workflow
const purchaseOrderWorkflow: TOrchestrator = async function* (ctx: OrchestrationContext, order: Order): any {
// Orders under $1000 are auto-approved
if (order.cost < 1000) {
return "Auto-approved";
}

// Orders of $1000 or more require manager approval
yield ctx.callActivity(sendApprovalRequest, order);

// Approvals must be received within 24 hours or they will be cancled.
const tasks: Task<any>[] = [];
const approvalEvent = ctx.waitForExternalEvent("approval_received");
const timeoutEvent = ctx.createTimer(24 * 60 * 60);
tasks.push(approvalEvent);
tasks.push(timeoutEvent);
const winner = whenAny(tasks);

if (winner == timeoutEvent) {
return "Cancelled";
}

yield ctx.callActivity(placeOrder, order);
const approvalDetails = approvalEvent.getResult();
return `Approved by ${approvalDetails.approver}`;
};

taskHubWorker.addOrchestrator(purchaseOrderWorkflow);
taskHubWorker.addActivity(sendApprovalRequest);
taskHubWorker.addActivity(placeOrder);

// Wrap the worker startup in a try-catch block to handle any errors during startup
try {
await taskHubWorker.start();
console.log("Worker started successfully");
} catch (error) {
console.error("Error starting worker:", error);
}

// Schedule a new orchestration
try {
const cost = readlineSync.question("Cost of your order:");
const approver = readlineSync.question("Approver of your order:");
const timeout = readlineSync.question("Timeout for your order in seconds:");
const order = new Order(cost, "MyProduct", 1);
const id = await taskHubClient.scheduleNewOrchestration(purchaseOrderWorkflow, order);
console.log(`Orchestration scheduled with ID: ${id}`);

if (readlineSync.keyInYN("Press [Y] to approve the order... Y/yes, N/no")) {
const approvalEvent = { approver: approver };
await taskHubClient.raiseOrchestrationEvent(id, "approval_received", approvalEvent);
} else {
return "Order rejected";
}

// Wait for orchestration completion
const state = await taskHubClient.waitForOrchestrationCompletion(id, undefined, timeout + 2);

console.log(`Orchestration completed! Result: ${state?.serializedOutput}`);
} catch (error) {
console.error("Error scheduling or waiting for orchestration:", error);
}

// stop worker and client
await taskHubWorker.stop();
await taskHubClient.stop();
})();
Loading
Loading