首页 > 其他分享 >elsa-core自定义Activity创建Bookmark人工任务完成

elsa-core自定义Activity创建Bookmark人工任务完成

时间:2024-02-10 21:55:06浏览次数:16  
标签:core taskName 自定义 source Bookmark default new line public

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

 

标签:core,taskName,自定义,source,Bookmark,default,new,line,public
From: https://www.cnblogs.com/dacong/p/18013058

相关文章

  • .NET Core 依赖注入 - IServiceProvider和IServiceScope
    要说起.NETCore,我想没有人会不知道依赖注入(DI),同时,这也真是一个被说烂的话题,如果你关注.NETCore,总会有人不厌其烦的给你讲什么是依赖,什么是注入,什么是控制反转,同时会给你举例.NETCoreDI三种生命周期(Transient,Scoped还有Singleton),并且通过打印hashcode的方式来说明彼此之......
  • 记录一下自定义博客园主题过程
    前言以前使用的都是默认的博客园主题,最近刚好有空,着手定制以下自己的博客园主题。最终效果参考当前的博客,如果看不到则需要在博客园首页头像处悬停关闭简洁模式思路是尽量保持原有结构,不进行破坏性改动,以css样式为主(当前只添加了两个js方法用于主题切换和判断是否在随笔阅读......
  • 【SpringBootStarter】自定义全局加解密组件
    【SpringBootStarter】目的了解SpringBootStarter相关概念以及开发流程实现自定义SpringBootStarter(全局加解密)了解测试流程优化最终引用的效果:<dependency><groupId>com.xbhog</groupId><artifactId>globalValidation-spring-boot-starter</artifactId>......
  • 零基础入门Vue之拘元遣将——其他常用指令&自定义指令
    回首在零基础入门Vue之梦开始的地方——插值语法我记录了v-bind、v-on、v-model的学习在零基础入门Vue之Tobeornottobe——条件渲染我记录了v-if、v-else-if、v-else、v-show的学习在零基础入门Vue之影分身之术——列表渲染&渲染原理浅析我记录了v-for的学习为了推......
  • 基于.NetCore开发博客项目 StarBlog - (31) 发布和部署
    前言StarBlog第一期规划的功能基本完成了,我想着在春节前应该可以把第一期的系列文章完结掉,那么在差缺补漏阶段就剩下开发项目的最后一个环节——部署了。PS:事实上,还有一个很重要但又经常被略过的测试环节我们没有提到,因为时间关系,第一期规划我没有写单元测试和集成测试,在开......
  • 【实用技巧】【Mybatis】自定义的动态 SQL
    1 前言平时我们可能会拼接SQL,尤其是做一些报表的开发或者连表查询的时候,当条件发生变更或者关联关系需要改变的时候,就需要更改代码里的SQL,所以我这里弄了一个简易版的基于Mybatis的动态SQL调用。2 实现主要是Mybatis的动态SQL://controller层@Override@PostMapp......
  • Asp-Net-Core学习笔记:4.Blazor-Server入门
    本来今天开始是有其他的安排了,也没办法抽出那么多时间来学NetCore,不过我想做事情有始有终吧,除了gRPC还没跑起来之外,Blazor这部分也了解了一点,官网地址:https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor目前来说还不是很完善,真正的离线单页应用还处于预览版阶段。Blazo......
  • Asp-Net-Core学习笔记:5.构建和部署
    从上次开始学习Asp.NetCore以来,一直都是玩一玩,还没有什么机会用来实战,最近单位有个新的小项目,于是我用Asp.NetCore来尝尝新,结果也是非常OK,熟悉之后开发效率感觉和Django基本没差。那么进入正题,本文简单说说Asp.NetCore应用的构建和部署。构建构建是使用build命令,不过一般我......
  • Asp-Net-Core学习笔记:WebApi开发实践
    前言用AspNetCore做Api开发也有一段时间了,正好年底在做总结,做一个WebApi开发实践笔记。暂时想到的一些技术关键词,同时也作为本文的大纲,现在对这套技术体系的了解还不够深入,以后会持续更新这个Api开发实践~身份认证:JwtBearer分页:X.PagedList缓存ResponseCacheRedis:St......
  • Asp-Net-Core学习笔记:3.使用SignalR实时通信框架开发聊天室
    SignalR牛刀小试在MVP杨老师的博客里看到这么个东西,我还以为是NetCore3才推出的新玩意,原来是已经有很多年的历史了,那看来还是比较成熟的一个技术了。简介SignalR是一个.NETCore/.NETFramework的开源实时框架,SignalR的可使用WebSocket,ServerSentEvents和LongPolling......