danielgerlag / workflow-core

Lightweight workflow engine for .NET Standard
MIT License
5.4k stars 1.2k forks source link

[Feature] Add scope support for workflow #1232

Open ling921 opened 10 months ago

ling921 commented 10 months ago

Describe the change

This pull request is to make Step to only obtain services from a specific scope.

Related issues: #1231 #846

Describe your implementation or design

Add a method to IWorkflowController called StartWorkflowWithScope, which accept an additional parameter IServiceScope, and then save it in WorkflowInstance

Tests

Yes

Breaking change

No

Additional context

None

danielgerlag commented 10 months ago

@ling921 thanks for the effort. Unfortunately, this does not make sense to wrap an entire workflow in a scope. When the workflow is executed, the entire thing "may" be executed all at once, or it may be partially executed and continue several days later, or on another machine. So we cannot preserve the scope across these time and node boundaries. We do wrap step executions within a scope here

ling921 commented 10 months ago

I understand this, and I haven't changed its default behavior. If users expect wrokflow steps to be executed within a certain scope, they should manage the creation and disposal of the scope themselves.

Also, I think the design of this scope is meaningful. I can define a scoped service to track or change the step's execution, and even share some unmanaged resources, etc. All of these are more suitable to be written in a service rather than within TData.

The primary challenge is ensuring the effective management of the scope, especially regarding proper disposal. In my current design, it is imperative for the user to proficiently handle scope disposal. With correct scope disposal, I believe there should be no issues with long-term retention.

There may be a more optimal design that can achieve this.