Skip to content

RFC: Support dynamic workflow types #973

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

Closed
wants to merge 1 commit into from

Conversation

lejenome
Copy link

This is a proposal to allow the implementation of Workflow Types dynamically. It's done by exporting a default function with the the following signature:

// @file src/workflows.js
import { proxyActivities } from '@temporalio/workflow';

/**
 * @param {string} workflowTypeName
 * @returns {(...args: any[]) => Promise<any>} The Workflow Type function
 */
export default function dynamicWorkflow(workflowTypeName) {
  if (workflowTypeName === "greeting") {
    // Workflow Type: greeting
    return async (name) => {
      const { greet } = proxyActivities();
      return await greet(name)
    }
  }
  else if (workflowTypeName === "multi") {
    // Workflow Type: multi
    return async (num1, num2) => {
      const { multi } = proxyActivities();
      return await multi(num1, num2)
    }
  }
}

And from the client.js

  const handle = await client.start("greeting", {
    args: ['World'],
    taskQueue: 'hello-world',
  });
  console.log(await handle.result()); // Hello World!

  const handle2 = await client.start("multi", {
    args: [2, 4],
    taskQueue: 'hello-world',
  });
  console.log(await handle2.result()); // 8

What was changed

  • initRuntime will fallback to default export (if it's a function) to get Workflow Type dynamicly

Why?

Allow the creation of Workflow Type to be dynamic, use cases:

  • Generate Workflow Types from a DSL definition
  • Allow lazy load of Workflow Types definitions from an external source or from a DSL definition

Checklist

  1. Closes

  2. How was this tested:

  • Tested on external project, sample project can be added after initial feedbacks
  1. Any docs updates needed?
  • Documentation of Workflow Type definition on TypeScript will need some updates

This is a proposal to allow the implementation of Workflow Types dynamically. It's done by exporting a default function with the the following signature:

```js
// @file src/workflows.js
import { proxyActivities } from '@temporalio/workflow';

/**
 * @param {string} workflowTypeName
 * @returns {(args: any) => Promise<any>} The Workflow Type function
 */
export default function dynamicWorkflow(workflowTypeName) {
  if (workflowTypeName === "greeting") {
    return async (name) => {
      const { greet } = proxyActivities();
      return await greet(name)
    }
  }
  else if (workflowTypeName === "multi") {
    return async (num1, num2) => {
      const { multi } = proxyActivities();
      return await multi(num1, num2)
    }
  }
}
```

And from the client.js

```js
  const handle = await client.start("greeting", {
    args: ['World'],
    taskQueue: 'hello-world',
  });
  console.log(await handle.result()); // Hello World!

  const handle2 = await client.start("multi", {
    args: [2, 4],
    taskQueue: 'hello-world',
  });
  console.log(await handle2.result()); // 8
```
@CLAassistant
Copy link

CLAassistant commented Nov 18, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@bergundy bergundy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
This matches some internal conversations we’ve had in the team.
We definitely want to support this.
The only thing I’m slightly concerned about is backwards compatibility, there is a chance that user have workflows named default and this change would break them.
Before we merge this we’d need to add tests and make sure to reflect this in the worker options docs and the Temporal docs site.
We can help with all of that.

@bergundy
Copy link
Member

@mjameswh want to weigh in on this too?

@mjameswh
Copy link
Contributor

This is indeed a feature we want to support. Thank you for your proposal!

I still have uncertainties on the best way to implement this, so I will take a few days to ponder about your proposition a little more. There are other issues that also relates to the structure of that object, and I want to make sure the implementation we put forward for this correctly fit our requirements around this.

@mjameswh
Copy link
Contributor

@lejenome You mention this potential use case:

"Allow lazy load of Workflow Types definitions from an external source or from a DSL definition"

Can you explain a little bit more?

I don't think the default function, as it has been proposed at this point, can help with that. The default function will be executed inside the workflow sandbox, so it can't make calls to a database or load more files from the filesystem... Nor can the default function use activities to perform such access, since the internal state of the workflow has not yet been properly setup at that point in time...

@mjameswh
Copy link
Contributor

Closing in favor of #1038.

@mjameswh mjameswh closed this Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants