워크플로 만들고 관리하는 방법
콘솔을 사용해서 Dapr 워크플로를 만들고 호출해봐요. 제공된 워크플로 예제를 통해 여러분은:
- JavaScript 워크플로 워커를 사용해서 워크플로 인스턴스를 실행해요
- JavaScript 워크플로 클라이언트와 API 호출을 사용해서 워크플로 인스턴스를 시작하고 종료해요
이 예제는 self-hosted 모드의 dapr init 기본 구성을 사용해요.
출처: Dapr 문서
본문
사전 준비사항 (Prerequisites)
- Dapr CLI와 초기화된 환경.
- Node.js와 npm,
- Docker Desktop
- 최신 proto 바인딩을 사용하고 있는지 확인해요
환경 설정하기
JavaScript SDK 레포를 클론하고 해당 디렉터리로 이동해요.
git clone https://github.com/dapr/js-sdk
cd js-sdk
JavaScript SDK 루트 디렉터리에서 Dapr 워크플로 예제로 이동해요.
cd examples/workflow/authoring
Dapr JavaScript SDK로 이 워크플로 예제를 실행하는 데 필요한 요구사항을 설치하려면 다음 명령을 실행해요.
npm install
activity-sequence.ts 실행하기
activity-sequence 파일은 Dapr 워크플로 런타임에 워크플로와 활동(activity)을 등록해요. 이 워크플로는 순서대로 실행되는 활동들의 시퀀스예요. DaprWorkflowClient를 사용해서 새 워크플로 인스턴스를 예약하고 그것이 완료될 때까지 기다려요.
const daprHost = "localhost";
const daprPort = "50001";
const workflowClient = new DaprWorkflowClient({
daprHost,
daprPort,
});
const workflowRuntime = new WorkflowRuntime({
daprHost,
daprPort,
});
const hello = async (_: WorkflowActivityContext, name: string) => {
return `Hello ${name}!`;
};
const sequence: TWorkflow = async function* (ctx: WorkflowContext): any {
const cities: string[] = [];
const result1 = yield ctx.callActivity(hello, "Tokyo");
cities.push(result1);
const result2 = yield ctx.callActivity(hello, "Seattle");
cities.push(result2);
const result3 = yield ctx.callActivity(hello, "London");
cities.push(result3);
return cities;
};
workflowRuntime.registerWorkflow(sequence).registerActivity(hello);
// Wrap the worker startup in a try-catch block to handle any errors during startup
try {
await workflowRuntime.start();
console.log("Workflow runtime started successfully");
} catch (error) {
console.error("Error starting workflow runtime:", error);
}
// Schedule a new orchestration
try {
const id = await workflowClient.scheduleNewWorkflow(sequence);
console.log(`Orchestration scheduled with ID: ${id}`);
// Wait for orchestration completion
const state = await workflowClient.waitForWorkflowCompletion(id, undefined, 30);
console.log(`Orchestration completed! Result: ${state?.serializedOutput}`);
} catch (error) {
console.error("Error scheduling or waiting for orchestration:", error);
}
위 코드에서:
workflowRuntime.registerWorkflow(sequence)는sequence를 Dapr 워크플로 런타임에 워크플로로 등록해요.await workflowRuntime.start();는 Dapr 워크플로 런타임 내부에서 엔진을 빌드하고 시작해요.await workflowClient.scheduleNewWorkflow(sequence)는 Dapr 워크플로 런타임에 새 워크플로 인스턴스를 예약해요.await workflowClient.waitForWorkflowCompletion(id, undefined, 30)는 워크플로 인스턴스가 완료될 때까지 기다려요.
터미널에서 다음 명령을 실행해서 activity-sequence.ts를 시작해요:
npm run start:dapr:activity-sequence
예상 출력 (Expected output)
You're up and running! Both Dapr and your app logs will appear here.
...
== APP == Orchestration scheduled with ID: dc040bea-6436-4051-9166-c9294f9d2201
== APP == Waiting 30 seconds for instance dc040bea-6436-4051-9166-c9294f9d2201 to complete...
== APP == Received "Orchestrator Request" work item with instance id 'dc040bea-6436-4051-9166-c9294f9d2201'
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Rebuilding local state with 0 history event...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Processing 2 new history event(s): [ORCHESTRATORSTARTED=1, EXECUTIONSTARTED=1]
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Waiting for 1 task(s) and 0 event(s) to complete...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Returning 1 action(s)
== APP == Received "Activity Request" work item
== APP == Activity hello completed with output "Hello Tokyo!" (14 chars)
== APP == Received "Orchestrator Request" work item with instance id 'dc040bea-6436-4051-9166-c9294f9d2201'
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Rebuilding local state with 3 history event...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Processing 2 new history event(s): [ORCHESTRATORSTARTED=1, TASKCOMPLETED=1]
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Waiting for 1 task(s) and 0 event(s) to complete...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Returning 1 action(s)
== APP == Received "Activity Request" work item
== APP == Activity hello completed with output "Hello Seattle!" (16 chars)
== APP == Received "Orchestrator Request" work item with instance id 'dc040bea-6436-4051-9166-c9294f9d2201'
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Rebuilding local state with 6 history event...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Processing 2 new history event(s): [ORCHESTRATORSTARTED=1, TASKCOMPLETED=1]
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Waiting for 1 task(s) and 0 event(s) to complete...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Returning 1 action(s)
== APP == Received "Activity Request" work item
== APP == Activity hello completed with output "Hello London!" (15 chars)
== APP == Received "Orchestrator Request" work item with instance id 'dc040bea-6436-4051-9166-c9294f9d2201'
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Rebuilding local state with 9 history event...
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Processing 2 new history event(s): [ORCHESTRATORSTARTED=1, TASKCOMPLETED=1]
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Orchestration completed with status COMPLETED
== APP == dc040bea-6436-4051-9166-c9294f9d2201: Returning 1 action(s)
INFO[0006] dc040bea-6436-4051-9166-c9294f9d2201: 'sequence' completed with a COMPLETED status. app_id=activity-sequence-workflow instance=kaibocai-devbox scope=wfengine.backend type=log ver=1.12.3
== APP == Instance dc040bea-6436-4051-9166-c9294f9d2201 completed
== APP == Orchestration completed! Result: ["Hello Tokyo!","Hello Seattle!","Hello London!"]
다음 단계 (Next steps)
- Dapr 워크플로에 대해 더 알아보기
- 워크플로 API 레퍼런스