elsa-core自定義Activity創建Bookmark人工任務完成

//實現一個自定的activity用於人工處理的節點
public class MyRunTask : Activity<object>
{ [Input(Description = "The name of the task being requested.")] public Input<string> TaskName { get; set; } = default!; /// <inheritdoc /> /// <inheritdoc /> [JsonConstructor] private MyRunTask(string? source = default, int? line = default) : base(source, line) { } /// <inheritdoc /> public MyRunTask(MemoryBlockReference output, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(output, source, line) { } /// <inheritdoc /> public MyRunTask(Output<object>? output, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(output, source, line) { } /// <inheritdoc /> public MyRunTask(string taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new Literal<string>(taskName), source, line) { } /// <inheritdoc /> /// <inheritdoc /> public MyRunTask(Func<string> taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line) { } /// <inheritdoc /> public MyRunTask(Func<ExpressionExecutionContext, string?> taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(new Input<string>(Expression.DelegateExpression(taskName), new MemoryBlockReference()), source, line) { } /// <inheritdoc /> public MyRunTask(Variable<string> taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line) => TaskName = new Input<string>(taskName); /// <inheritdoc /> public MyRunTask(Literal<string> taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line) => TaskName = new Input<string>(taskName); /// <inheritdoc /> public MyRunTask(Input<string> taskName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line) => TaskName = taskName; protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) { // Create bookmark. var taskName = TaskName.Get(context); var identityGenerator = context.GetRequiredService<IIdentityGenerator>(); var taskId = identityGenerator.GenerateId(); var payload = new RunTaskBookmarkPayload(taskId, taskName); context.CreateBookmark(payload, ResumeAsync, includeActivityInstanceId: false); await ValueTask.CompletedTask; } public const string InputKey = "RunTaskInput"; private async ValueTask ResumeAsync(ActivityExecutionContext context) { var input = context.GetWorkflowInput<object>(InputKey); Console.WriteLine($"-------------------{this}-----input={input}"); context.Set(Result, input); await context.CompleteActivityAsync(); } } public class MyWorkflow : WorkflowBase { protected override void Build(IWorkflowBuilder builder) { var employee = builder.WithVariable<Employee>().WithWorkflowStorage(); builder.Root = new Sequence { Activities = { // Capture the workflow input (the employee to onboard). new SetVariable { Variable = employee, Value = new(context => context.GetInput<Employee>("Employee")) }, // First thing we need to do is get an email account setup. new MyRunTask("科室主任") { }, new MyRunTask("副總經理") { }, new MyRunTask("總經理") { }, // Onboarding has completed. new Finish() } }; } } [ApiController] [Route("[controller]")] public class HomeController : ControllerBase { private readonly IWorkflowDispatcher workflowDispatcher; public HomeController(IWorkflowDispatcher workflowDispatcher) { this.workflowDispatcher = workflowDispatcher; } [HttpPost] [HttpPost("complete")] public async Task<string> Complete(string taskId, object? result = default) { var bookmarkPayload = new RunTaskBookmarkPayload(taskId, default!); var input = new Dictionary<string, object> { [RunTask.InputKey] = result! }; var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<MyRunTask>(); var request = new DispatchResumeWorkflowsRequest(activityTypeName, bookmarkPayload, Input: input); await workflowDispatcher.DispatchAsync(request); return taskId + result; } }


 
使用postman測試
1、啓動工作流:post http://localhost:51238/elsa/api/workflow-definitions/MyWorkflow/execute
2、獲取工作流實例的執行狀態:get http://localhost:51238/elsa/api/workflow-instances/f407c64cec823692
3、完成當前任務:post http://localhost:51238/home/complete?taskid=d1a4507097abfb0c    我的這個流程需要執行三次,沒執行一次使用2重新獲取,
  注意使用:post http://localhost:51238/elsa/api/tasks/47b1b97bc2ef4835/complete 不能實現當前任務的完成,原因是自定義的Activity和引擎帶的RunTask是不一樣的

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章