Skip to content

Commit 446a95e

Browse files
committed
Merge remote-tracking branch 'remotes/origin/master' into endstep
# Conflicts: # src/samples/WorkflowCore.Sample01/HelloWorldWorkflow.cs # src/samples/WorkflowCore.Sample02/SimpleDecisionWorkflow.cs # src/samples/WorkflowCore.Sample03/PassingDataWorkflow.cs # src/samples/WorkflowCore.Sample04/EventSampleWorkflow.cs # src/samples/WorkflowCore.Sample05/DeferSampleWorkflow.cs # src/samples/WorkflowCore.Sample06/MultipleOutcomeWorkflow.cs # src/samples/WorkflowCore.Sample08/HumanWorkflow.cs # test/WorkflowCore.IntegrationTests/Scenarios/BaseScenario.cs # test/WorkflowCore.IntegrationTests/Scenarios/BasicWorkflow.cs # test/WorkflowCore.IntegrationTests/Scenarios/DataIO.cs # test/WorkflowCore.IntegrationTests/Scenarios/ExternalEvents.cs # test/WorkflowCore.IntegrationTests/Scenarios/OutcomeFork.cs # test/WorkflowCore.IntegrationTests/Scenarios/UserSteps.cs # test/WorkflowCore.Tests.MongoDB/MongoPersistenceProviderTests/CreateNewWorkflow.cs # test/WorkflowCore.Tests.MongoDB/MongoPersistenceProviderTests/GetWorkflowInstance.cs # test/WorkflowCore.Tests.MongoDB/MongoPersistenceProviderTests/PersistWorkflow.cs # test/WorkflowCore.Tests.PostgreSQL/PersistenceProviderTests/CreateNewWorkflow.cs # test/WorkflowCore.Tests.PostgreSQL/PersistenceProviderTests/GetWorkflowInstance.cs # test/WorkflowCore.Tests.PostgreSQL/PersistenceProviderTests/PersistWorkflow.cs # test/WorkflowCore.UnitTests/WorkflowCore.UnitTests.csproj
2 parents 1adfc8d + 674174b commit 446a95e

File tree

25 files changed

+578
-245
lines changed

25 files changed

+578
-245
lines changed

README.md

Lines changed: 41 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -2,226 +2,75 @@
22

33
[![Build status](https://ci.appveyor.com/api/projects/status/xnby6p5v4ur04u76?svg=true)](https://ci.appveyor.com/project/danielgerlag/workflow-core)
44

5-
Workflow Core is a light weight workflow engine targeting .NET Standard. It supports pluggable persistence and concurrency providers to allow for multi-node clusters. See [Wiki here.](https://github.com/danielgerlag/workflow-core/wiki)
5+
Workflow Core is a light weight workflow engine targeting .NET Standard. Think: long running processes with multiple tasks that need to track state. It supports pluggable persistence and concurrency providers to allow for multi-node clusters.
66

7+
## Documentation
78

8-
## Installing
9+
See [Full Documentation here.](https://github.com/danielgerlag/workflow-core/wiki)
910

10-
Install the NuGet package "WorkflowCore"
11+
## Fluent API
1112

12-
```
13-
PM> Install-Package WorkflowCore
14-
```
15-
16-
## Basic Concepts
17-
18-
### Steps
19-
20-
A workflow consists of a series of connected steps. Each step produces an outcome value and subsequent steps are triggered by subscribing to a particular outcome of a preceeding step. The default outcome of *null* can be used for a basic linear workflow.
21-
Steps are usually defined by inheriting from the StepBody abstract class and implementing the Run method. They can also be created inline while defining the workflow structure.
22-
23-
First we define some steps
24-
25-
```C#
26-
public class HelloWorld : StepBody
27-
{
28-
public override ExecutionResult Run(IStepExecutionContext context)
29-
{
30-
Console.WriteLine("Hello world");
31-
return ExecutionResult.Next();
32-
}
33-
}
34-
```
35-
*The StepBody class implementations are constructed by the workflow host which first tries to use IServiceProvider from the built-in dependency injection of .NET Core, if it can't construct it with this method, it will search for a parameterless constructor*
36-
37-
Then we define the workflow structure by composing a chain of steps. The is done by implementing the IWorkflow interface.
38-
39-
```C#
40-
public class HelloWorldWorkflow : IWorkflow
41-
{
42-
public void Build(IWorkflowBuilder<object> builder)
43-
{
44-
builder
45-
.StartWith<HelloWorld>()
46-
.Then<GoodbyeWorld>();
47-
}
48-
...
49-
}
50-
```
51-
The *IWorkflow* interface also has a readonly Id property and readonly Version property. These are generally static and are used by the workflow host to identify a workflow definition.
52-
53-
You can also define your steps inline
13+
Define your workflows with the fluent API.
5414

55-
```C#
56-
public class HelloWorldWorkflow : IWorkflow
15+
```c#
16+
public class MyWorkflow : IWorkflow
5717
{
58-
public void Build(IWorkflowBuilder<object> builder)
59-
{
18+
public void Build(IWorkflowBuilder<MyData> builder)
19+
{
6020
builder
61-
.StartWith(context =>
62-
{
63-
Console.WriteLine("Hello world");
64-
return ExecutionResult.Next();
65-
})
66-
.Then(context =>
67-
{
68-
Console.WriteLine("Goodbye world");
69-
return ExecutionResult.Next();
70-
})
21+
.StartWith<Task1>()
22+
.Then<Task2>()
23+
.Then<Task3>;
7124
}
72-
...
7325
}
7426
```
7527

76-
Each running workflow is persisted to the chosen persistence provider between each step, where it can be picked up at a later point in time to continue execution. The outcome result of your step can instruct the workflow host to defer further execution of the workflow until a future point in time or in response to an external event.
77-
78-
The first time a particular step within the workflow is called, the PersistenceData property on the context object is *null*. The ExecutionResult produced by the Run method can either cause the workflow to proceed to the next step by providing an outcome value, instruct the workflow to sleep for a defined period or simply not move the workflow forward. If no outcome value is produced, then the step becomes re-entrant by setting PersistenceData, so the workflow host will call this step again in the future buy will populate the PersistenceData with it's previous value.
79-
80-
For example, this step will initially run with *null* PersistenceData and put the workflow to sleep for 12 hours, while setting the PersistenceData to *new Object()*. 12 hours later, the step will be called again but context.PersistenceData will now contain the object constructed in the previous iteration, and will now produce an outcome value of *null*, causing the workflow to move forward.
81-
82-
```C#
83-
public class SleepStep : StepBody
84-
{
85-
public override ExecutionResult Run(IStepExecutionContext context)
86-
{
87-
if (context.PersistenceData == null)
88-
return ExecutionResult.Sleep(Timespan.FromHours(12), new Object());
89-
else
90-
return ExecutionResult.Next();
91-
}
92-
}
93-
```
94-
95-
### Passing data between steps
96-
97-
Each step is intended to be a black-box, therefore they support inputs and outputs. These inputs and outputs can be mapped to a data class that defines the custom data relevant to each workflow instance.
98-
99-
The following sample shows how to define inputs and outputs on a step, it then shows how define a workflow with a typed class for internal data and how to map the inputs and outputs to properties on the custom data class.
100-
101-
```C#
102-
//Our workflow step with inputs and outputs
103-
public class AddNumbers : StepBody
104-
{
105-
public int Input1 { get; set; }
106-
107-
public int Input2 { get; set; }
108-
109-
public int Output { get; set; }
110-
111-
public override ExecutionResult Run(IStepExecutionContext context)
112-
{
113-
Output = (Input1 + Input2);
114-
return ExecutionResult.Next();
115-
}
116-
}
28+
### Sample use cases
11729

118-
//Our class to define the internal data of our workflow
119-
public class MyDataClass
30+
* New user workflow
31+
```c#
32+
public class MyData
12033
{
121-
public int Value1 { get; set; }
122-
public int Value2 { get; set; }
123-
public int Value3 { get; set; }
34+
public string Email { get; set; }
35+
public string Password { get; set; }
36+
public string UserId { get; set; }
12437
}
12538

126-
//Our workflow definition with strongly typed internal data and mapped inputs & outputs
127-
public class PassingDataWorkflow : IWorkflow<MyDataClass>
128-
{
129-
public void Build(IWorkflowBuilder<MyDataClass> builder)
130-
{
131-
builder
132-
.StartWith<AddNumbers>()
133-
.Input(step => step.Input1, data => data.Value1)
134-
.Input(step => step.Input2, data => data.Value2)
135-
.Output(data => data.Value3, step => step.Output)
136-
.Then<CustomMessage>()
137-
.Input(step => step.Message, data => "The answer is " + data.Value3.ToString());
138-
}
139-
...
140-
}
141-
142-
```
143-
144-
### Multiple outcomes / forking
145-
146-
A workflow can take a different path depending on the outcomes of preceeding steps. The following example shows a process where first a random number of 0 or 1 is generated and is the outcome of the first step. Then, depending on the outcome value, the workflow will either fork to (TaskA + TaskB) or (TaskC + TaskD)
147-
148-
```C#
149-
public class MultipleOutcomeWorkflow : IWorkflow
39+
public class MyWorkflow : IWorkflow
15040
{
151-
public void Build(IWorkflowBuilder<object> builder)
152-
{
41+
public void Build(IWorkflowBuilder<MyData> builder)
42+
{
15343
builder
154-
.StartWith<RandomOutput>(x => x.Name("Random Step"))
155-
.When(0)
156-
.Then<TaskA>()
157-
.Then<TaskB>()
158-
.End<RandomOutput>("Random Step")
159-
.When(1)
160-
.Then<TaskC>()
161-
.Then<TaskD>()
162-
.End<RandomOutput>("Random Step");
44+
.StartWith<CreateUser>()
45+
.Input(step => step.Email, data => data.Email)
46+
.Input(step => step.Password, data => data.Password)
47+
.Output(data => data.UserId, step => step.UserId);
48+
.Then<SendConfirmationEmail>()
49+
.WaitFor("confirmation", data => data.UserId)
50+
.Then<UpdateUser>()
51+
.Input(step => step.UserId, data => data.UserId);
16352
}
16453
}
16554
```
16655

167-
### Events
56+
* Resilient service orchestration
16857

169-
A workflow can also wait for an external event before proceeding. In the following example, the workflow will wait for an event called *"MyEvent"* with a key of *0*. Once an external source has fired this event, the workflow will wake up and continue processing, passing the data generated by the event onto the next step.
170-
171-
```C#
172-
public class EventSampleWorkflow : IWorkflow<MyDataClass>
58+
```c#
59+
public class MyWorkflow : IWorkflow
17360
{
174-
public void Build(IWorkflowBuilder<MyDataClass> builder)
175-
{
61+
public void Build(IWorkflowBuilder<MyData> builder)
62+
{
17663
builder
177-
.StartWith(context =>
178-
{
179-
Console.WriteLine("workflow started");
180-
return ExecutionResult.Next();
181-
})
182-
.WaitFor("MyEvent", data => "0")
183-
.Output(data => data.Value, step => step.EventData)
184-
.Then<CustomMessage>()
185-
.Input(step => step.Message, data => "The data from the event is " + data.Value);
64+
.StartWith<CreateCustomer>()
65+
.Then<PushToSalesforce>()
66+
.OnError(WorkflowErrorHandling.Retry, TimeSpan.FromMinutes(10))
67+
.Then<PushToERP>()
68+
.OnError(WorkflowErrorHandling.Retry, TimeSpan.FromMinutes(10));
18669
}
18770
}
188-
...
189-
//External events are published via the host
190-
//All workflows that have subscribed to MyEvent 0, will be passed "hello"
191-
host.PublishEvent("MyEvent", "0", "hello");
192-
```
193-
194-
### Host
195-
196-
The workflow host is the service responsible for executing workflows. It does this by polling the persistence provider for workflow instances that are ready to run, executes them and then passes them back to the persistence provider to by stored for the next time they are run. It is also responsible for publishing events to any workflows that may be waiting on one.
197-
198-
#### Setup
199-
200-
Use the *AddWorkflow* extension method for *IServiceCollection* to configure the workflow host upon startup of your application.
201-
By default, it is configured with *MemoryPersistenceProvider* and *SingleNodeConcurrencyProvider* for testing purposes. You can also configure a DB persistence provider at this point.
202-
203-
```C#
204-
services.AddWorkflow();
20571
```
20672

207-
#### Usage
208-
209-
When your application starts, grab the workflow host from the built-in dependency injection framework *IServiceProvider*. Make sure you call *RegisterWorkflow*, so that the workflow host knows about all your workflows, and then call *Start()* to fire up the thread pool that executes workflows. Use the *StartWorkflow* method to initiate a new instance of a particular workflow.
210-
211-
212-
```C#
213-
var host = serviceProvider.GetService<IWorkflowHost>();
214-
host.RegisterWorkflow<HelloWorldWorkflow>();
215-
host.Start();
216-
217-
host.StartWorkflow("HelloWorld", 1, null);
218-
219-
Console.ReadLine();
220-
host.Stop();
221-
```
222-
223-
224-
### Persistence
73+
## Persistence
22574

22675
Since workflows are typically long running processes, they will need to be persisted to storage between steps.
22776
There are several persistence providers available as separate Nuget packages.
@@ -233,40 +82,6 @@ There are several persistence providers available as separate Nuget packages.
23382
* [Sqlite](src/providers/WorkflowCore.Persistence.Sqlite)
23483
* Redis *(coming soon...)*
23584

236-
### Multi-node clusters
237-
238-
By default, the WorkflowHost service will run as a single node using the built-in queue and locking providers for a single node configuration. Should you wish to run a multi-node cluster, you will need to configure an external queueing mechanism and a distributed lock manager to co-ordinate the cluster. These are the providers that are currently available.
239-
240-
#### Queue Providers
241-
242-
* SingleNodeQueueProvider *(Default built-in provider)*
243-
* [RabbitMQ](src/providers/WorkflowCore.QueueProviders.RabbitMQ)
244-
* [0MQ](src/providers/WorkflowCore.QueueProviders.ZeroMQ) *(experimental)*
245-
* Apache ZooKeeper *(coming soon...)*
246-
247-
#### Distributed lock managers
248-
249-
* SingleNodeLockProvider *(Default built-in provider)*
250-
* [Redis Redlock](src/providers/WorkflowCore.LockProviders.Redlock)
251-
* [0MQ](src/providers/WorkflowCore.LockProviders.ZeroMQ) *(experimental)*
252-
* Apache ZooKeeper *(coming soon...)*
253-
254-
### Error handling
255-
256-
Each step can be configured with it's own error handling behavior, it can be retried at a later time, suspend the workflow or terminate the workflow.
257-
258-
```C#
259-
public void Build(IWorkflowBuilder<object> builder)
260-
{
261-
builder
262-
.StartWith<HelloWorld>()
263-
.OnError(WorkflowErrorHandling.Retry, TimeSpan.FromMinutes(10))
264-
.Then<GoodbyeWorld>();
265-
}
266-
```
267-
268-
The WorkflowHost service also has a .OnStepError event which can be used to intercept exceptions from workflow steps on a more global level.
269-
27085
## Extensions
27186

27287
* [User (human) workflows](src/extensions/WorkflowCore.Users)

src/WorkflowCore/Services/EventThread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private bool SeedSubscription(Event evt, EventSubscription sub)
102102
try
103103
{
104104
var workflow = _persistenceStore.GetWorkflowInstance(sub.WorkflowId).Result;
105-
var pointers = workflow.ExecutionPointers.Where(p => p.EventName == sub.EventName && p.EventKey == p.EventKey && !p.EventPublished);
105+
var pointers = workflow.ExecutionPointers.Where(p => p.EventName == sub.EventName && p.EventKey == sub.EventKey && !p.EventPublished);
106106
foreach (var p in pointers)
107107
{
108108
p.EventData = evt.EventData;

src/WorkflowCore/WorkflowCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<Description>Workflow Core is a light weight workflow engine targeting .NET Standard.</Description>
21+
<Version>1.1.2</Version>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

src/extensions/WorkflowCore.Users/WorkflowCore.Users.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<Description>Provides extensions for Workflow Core to enable human workflows.</Description>
21+
<Version>1.1.1</Version>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

src/providers/WorkflowCore.Persistence.MongoDB/WorkflowCore.Persistence.MongoDB.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20-
<Version>1.1.0</Version>
20+
<Version>1.1.1</Version>
21+
<Description>Provides support to persist workflows running on Workflow Core to a MongoDB database.</Description>
2122
</PropertyGroup>
2223

2324
<ItemGroup>

src/providers/WorkflowCore.Persistence.PostgreSQL/WorkflowCore.Persistence.PostgreSQL.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<Description>Provides support to persist workflows running on Workflow Core to a PostgreSQL database.</Description>
21+
<Version>1.1.1</Version>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

src/providers/WorkflowCore.Persistence.SqlServer/WorkflowCore.Persistence.SqlServer.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<Version>1.1.1</Version>
21+
<Description>Provides support to persist workflows running on Workflow Core to a SQL Server database.</Description>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

src/providers/WorkflowCore.Persistence.Sqlite/WorkflowCore.Persistence.Sqlite.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
20+
<Description>Provides support to persist workflows running on Workflow Core to a Sqlite database.</Description>
21+
<Version>1.1.1</Version>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

src/samples/WorkflowCore.Sample08/HumanWorkflow2.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,8 @@ namespace WorkflowCore.Sample08
99
{
1010
public class HumanWorkflow2 : IWorkflow
1111
{
12-
public string Id
13-
{
14-
get
15-
{
16-
return "HumanWorkflow";
17-
}
18-
}
19-
20-
public int Version
21-
{
22-
get
23-
{
24-
return 1;
25-
}
26-
}
12+
public string Id => "HumanWorkflow";
13+
public int Version => 1;
2714

2815
public void Build(IWorkflowBuilder<object> builder)
2916
{

0 commit comments

Comments
 (0)