//实现一个自定的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是不一样的

 

posted on 2024-02-10 21:44  daconglee  阅读(150)  评论(0编辑  收藏  举报