11using Hangfire ;
22using Hangfire . Server ;
33using System ;
4+ using System . Collections ;
5+ using System . Collections . Generic ;
6+ using System . Linq ;
47using System . Threading . Tasks ;
58using WorkflowEngine . Core ;
69using WorkflowEngine . Core . Actions ;
912
1013namespace WorkflowEngine
1114{
12-
15+
16+ public interface IWorkflowAccessor
17+ {
18+ ValueTask < WorkflowManifest > GetWorkflowManifestAsync ( IWorkflow workflow ) ;
19+ }
20+ public class DefaultWorkflowAccessor : IWorkflowAccessor
21+ {
22+ private readonly IEnumerable < IWorkflow > workflows ;
23+
24+ public DefaultWorkflowAccessor ( IEnumerable < IWorkflow > workflows )
25+ {
26+ this . workflows = workflows ?? throw new ArgumentNullException ( nameof ( workflows ) ) ;
27+ }
28+ public ValueTask < WorkflowManifest > GetWorkflowManifestAsync ( IWorkflow workflow )
29+ {
30+ return new ValueTask < WorkflowManifest > ( workflows . FirstOrDefault ( x=> x . Id == workflow . Id && workflow . Version == x . Version ) . Manifest ) ;
31+ }
32+ }
1333 public class HangfireWorkflowExecutor : IHangfireWorkflowExecutor , IHangfireActionExecutor
1434 {
35+ private readonly IWorkflowAccessor workflowAccessor ;
1536 private readonly IBackgroundJobClient backgroundJobClient ;
1637 private readonly IRunContextAccessor runContextAccessor ;
1738 private readonly IWorkflowExecutor executor ;
1839 private readonly IActionExecutor actionExecutor ;
1940 private readonly IOutputsRepository outputRepository ;
2041 private readonly IArrayContext arrayContext ;
2142
22- public HangfireWorkflowExecutor ( IBackgroundJobClient backgroundJobClient , IArrayContext arrayContext , IRunContextAccessor runContextAccessor , IWorkflowExecutor executor , IActionExecutor actionExecutor , IOutputsRepository actionResultRepository )
43+ public HangfireWorkflowExecutor ( IWorkflowAccessor workflowAccessor , IBackgroundJobClient backgroundJobClient , IArrayContext arrayContext , IRunContextAccessor runContextAccessor , IWorkflowExecutor executor , IActionExecutor actionExecutor , IOutputsRepository actionResultRepository )
2344 {
45+ this . workflowAccessor = workflowAccessor ?? throw new ArgumentNullException ( nameof ( workflowAccessor ) ) ;
2446 this . backgroundJobClient = backgroundJobClient ?? throw new ArgumentNullException ( nameof ( backgroundJobClient ) ) ;
2547 this . arrayContext = arrayContext ?? throw new ArgumentNullException ( nameof ( arrayContext ) ) ;
2648 this . runContextAccessor = runContextAccessor ;
@@ -38,10 +60,15 @@ public HangfireWorkflowExecutor(IBackgroundJobClient backgroundJobClient, IArray
3860
3961 public async ValueTask < object > ExecuteAsync ( IRunContext run , IWorkflow workflow , IAction action , PerformContext context )
4062 {
63+ //TODO - avoid sending all workflow over hangfire, so we should lookup the manifest here if not set on workflow form its ID.
64+ workflow . Manifest ??= await workflowAccessor . GetWorkflowManifestAsync ( workflow ) ;
65+
66+
4167 runContextAccessor . RunContext = run ;
4268 arrayContext . JobId = context . BackgroundJob . Id ;
4369
4470
71+
4572 var result = await actionExecutor . ExecuteAsync ( run , workflow , action ) ;
4673
4774
@@ -76,14 +103,19 @@ public async ValueTask<object> ExecuteAsync(IRunContext run, IWorkflow workflow,
76103 /// <returns></returns>
77104 public async ValueTask < object > TriggerAsync ( ITriggerContext context )
78105 {
79-
106+ //TODO - avoid sending all workflow over hangfire,
107+ context . Workflow . Manifest ??= await workflowAccessor . GetWorkflowManifestAsync ( context . Workflow ) ;
108+
80109 context . RunId = context . RunId == Guid . Empty ? Guid . NewGuid ( ) : context . RunId ;
81110
82111 runContextAccessor . RunContext = context ;
83112 var action = await executor . Trigger ( context ) ;
84113
85114 if ( action != null )
86115 {
116+ //TODO - avoid sending all workflow over hangfire, so we should wipe the workflow.manifest before scheduling and restore it after.
117+ context . Workflow . Manifest = null ;
118+
87119
88120 var a = backgroundJobClient . Enqueue < IHangfireActionExecutor > (
89121 ( executor ) => executor . ExecuteAsync ( context , context . Workflow , action , null ) ) ;
0 commit comments