-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from glucaci/postMiddleware
Add execute middleware phase
- Loading branch information
Showing
9 changed files
with
303 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Workflow Core 3.4.0 | ||
|
||
## Execute Workflow Middleware | ||
|
||
These middleware get run after each workflow execution and can be used to perform additional actions or build metrics/statistics for all workflows in your app. | ||
|
||
The following example illustrates how you can use a execute workflow middleware to build [prometheus](https://prometheus.io/) metrics. | ||
|
||
Note that you use `WorkflowMiddlewarePhase.ExecuteWorkflow` to specify that it runs after each workflow execution. | ||
|
||
**Important:** You should call `next` as part of the workflow middleware to ensure that the next workflow in the chain runs. | ||
|
||
```cs | ||
public class MetricsMiddleware : IWorkflowMiddleware | ||
{ | ||
private readonly ConcurrentHashSet<string>() _suspendedWorkflows = | ||
new ConcurrentHashSet<string>(); | ||
|
||
private readonly Counter _completed; | ||
private readonly Counter _suspended; | ||
|
||
public MetricsMiddleware() | ||
{ | ||
_completed = Prometheus.Metrics.CreateCounter( | ||
"workflow_completed", "Workflow completed"); | ||
|
||
_suspended = Prometheus.Metrics.CreateCounter( | ||
"workflow_suspended", "Workflow suspended"); | ||
} | ||
|
||
public WorkflowMiddlewarePhase Phase => | ||
WorkflowMiddlewarePhase.ExecuteWorkflow; | ||
|
||
public Task HandleAsync( | ||
WorkflowInstance workflow, | ||
WorkflowDelegate next) | ||
{ | ||
switch (workflow.Status) | ||
{ | ||
case WorkflowStatus.Complete: | ||
if (_suspendedWorkflows.TryRemove(workflow.Id)) | ||
{ | ||
_suspended.Dec(); | ||
} | ||
_completed.Inc(); | ||
break; | ||
case WorkflowStatus.Suspended: | ||
_suspended.Inc(); | ||
break; | ||
} | ||
|
||
return next(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.