dapr / js-sdk

Dapr SDK for Javascript
Apache License 2.0
192 stars 80 forks source link

Issue with SDK on workflow when using nestjs #604

Open trillzglobal opened 1 month ago

trillzglobal commented 1 month ago

I am able to setup a service but if I call the service I get error:

== APP == Waiting 30 seconds for instance e40321a6-ed22-4707-80d9-9198d53cd934 to complete... == APP == Received "Orchestrator Request" work item with instance id 'e40321a6-ed22-4707-80d9-9198d53cd934' == APP == e40321a6-ed22-4707-80d9-9198d53cd934: Rebuilding local state with 0 history event... == APP == e40321a6-ed22-4707-80d9-9198d53cd934: Processing 2 new history event(s): [ORCHESTRATORSTARTED=1, EXECUTIONSTARTED=1] == APP == An orchestrator was returned that doesn't schedule any tasks (type = [object Object]) == APP == e40321a6-ed22-4707-80d9-9198d53cd934: Orchestration completed with status COMPLETED == APP == e40321a6-ed22-4707-80d9-9198d53cd934: Returning 1 action(s)

If I run the script alone it works but I can the injectable function from the controller it does not see any scheduled task as stated above.

`import { DaprWorkflowClient, TWorkflow, WorkflowActivityContext, WorkflowContext, WorkflowRuntime, } from '@dapr/dapr'; import { Injectable } from '@nestjs/common';

const daprHost = 'localhost'; // Replace with your Dapr host const daprPort = '50002'; // Replace with your Dapr port

@Injectable() export class DaprActivityService { private readonly workflowClient: DaprWorkflowClient; private readonly workflowRuntime: WorkflowRuntime;

constructor() { this.workflowClient = new DaprWorkflowClient({ daprHost, daprPort }); this.workflowRuntime = new WorkflowRuntime({ daprHost, daprPort }); }

async startWorkflow(id: string) { console.log('WorkflowClient:', this.workflowClient); console.log('WorkflowRuntime:', this.workflowRuntime);

const hello = async (_: WorkflowActivityContext, name: string) => {
  return `Hello ${name}!`;
};

const helloWorld = async (_: WorkflowActivityContext, name: string) => {
  return 'Hello World! ' + name;
};

const sequence: TWorkflow = async function* (ctx: WorkflowContext): any {
  const cities: any[] = [];

  const result1 = yield ctx.callActivity(hello, 'Tokyo');
  cities.push(result1);
  const result2 = yield ctx.callActivity(helloWorld, result1);
  cities.push(result2);
  const result3 = yield ctx.callActivity(hello, 'London');
  cities.push(result3);

  return cities;
};

this.workflowRuntime
  .registerWorkflow(sequence)
  .registerActivity(hello)
  .registerActivity(helloWorld);

// Wrap the worker startup in a try-catch block to handle any errors during startup
try {
  await this.workflowRuntime.start();
  console.log(this.workflowRuntime);

  console.log('Workflow runtime started successfully');
} catch (error) {
  console.error('Error starting workflow runtime:', error);
}

// Schedule a new orchestration
try {
  const id = await this.workflowClient.scheduleNewWorkflow(sequence);
  console.log(`Orchestration scheduled with ID: ${id}`);
  console.log('New workflowClient', this.workflowClient);
  // Wait for orchestration completion
  const state = await this.workflowClient.waitForWorkflowCompletion(
    id,
    undefined,
    30,
  );

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

try {
  await this.workflowRuntime.stop();
  await this.workflowClient.stop();
} catch (error) {
  console.log('Error stopping workflow runtime:', error);
}

} }

const daprService = new DaprActivityService(); daprService.startWorkflow('12345'); `