-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDynamicWorkflow.cs
More file actions
113 lines (97 loc) · 4.55 KB
/
DynamicWorkflow.cs
File metadata and controls
113 lines (97 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2024 Conductor Authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
using conductor.Examples;
using Conductor.Api;
using Conductor.Client;
using Conductor.Client.Extensions;
using Conductor.Client.Models;
using Conductor.Client.Worker;
using Conductor.Definition;
using Conductor.Definition.TaskType;
using Conductor.Executor;
using System.Collections.Generic;
using System.Threading;
namespace Conductor.Examples
{
[WorkerTask]
public class DynamicWorkflow
{
private readonly WorkflowResourceApi _workflowClient;
private readonly MetadataResourceApi _metaDataClient;
private readonly WorkflowExecutor _workflowExecutor;
//const
private const string WorkflowName = "dynamic_workflow";
private const string WorkflowDescription = "test_dynamic_workflow";
public DynamicWorkflow()
{
var config = new Configuration();
_workflowExecutor = new WorkflowExecutor(config);
_workflowClient = ApiExtensions.GetClient<WorkflowResourceApi>();
_metaDataClient = ApiExtensions.GetClient<MetadataResourceApi>();
//For local testing
//var _orkesApiClient = new OrkesApiClient(config, new OrkesAuthenticationSettings(Constants.KEY_ID, Constants.KEY_SECRET));
//_workflowClient = _orkesApiClient.GetClient<WorkflowResourceApi>();
//_metaDataClient = _orkesApiClient.GetClient<MetadataResourceApi>();
}
[WorkerTask(taskType: ExampleConstants.GetEmail, batchSize: 5, pollIntervalMs: 520, workerId: "workerId")]
public string GetUserEmail(string userId)
{
return $"{userId}@example.com";
}
[WorkerTask(taskType: ExampleConstants.SendEmail, batchSize: 5, pollIntervalMs: 520, workerId: "workerId")]
public string SendEmail(string email, string subject, string body)
{
return $"sending email to {email} with subject {subject} and body {body}";
}
public void DynamicWorkFlowMain()
{
ConductorWorkflow workflow = new ConductorWorkflow()
.WithName(WorkflowName)
.WithDescription(WorkflowDescription)
.WithVersion(1);
workflow.WithInputParameter("userId");
var getEmailTask = new SimpleTask(ExampleConstants.GetEmail, ExampleConstants.GetEmail).WithInput("userId", workflow.Input("userId"));
getEmailTask.Description = ExampleConstants.GetEmailDescription;
workflow.WithTask(getEmailTask);
var SendEmailTask = new SimpleTask(ExampleConstants.SendEmail, ExampleConstants.SendEmail).WithInput("email", workflow.Input("email")).WithInput("subject", workflow.Input("subject")).WithInput("body", workflow.Input("body"));
SendEmailTask.Description = ExampleConstants.SendEmailDescription;
workflow.WithTask(SendEmailTask);
List<TaskDef> taskDefs = new List<TaskDef>()
{
new TaskDef{Description = ExampleConstants.GetEmailDescription, Name = ExampleConstants.GetEmail },
new TaskDef{Description = ExampleConstants.SendEmailDescription,Name = ExampleConstants.SendEmail}
};
_metaDataClient.RegisterTaskDef(taskDefs);
_metaDataClient.UpdateWorkflowDefinitions(new List<WorkflowDef>(1) { workflow });
var testInput = new Dictionary<string, object>
{
{ "userId", "Test" },
{ "email", "email@gmail.com" },
{ "subject", "SubjectTest" },
{ "body" , "BodyDescription" }
};
StartWorkflowRequest startWorkflowRequest = new StartWorkflowRequest()
{
Name = workflow.Name,
Input = testInput,
Version = workflow.Version,
WorkflowDef = workflow,
CreatedBy = Constants.OWNER_EMAIL,
};
_workflowClient.StartWorkflow(startWorkflowRequest);
var waitHandle = new ManualResetEvent(false);
var backgroundTask = System.Threading.Tasks.Task.Run(async () => await Utils.WorkerUtil.StartBackGroundTask(waitHandle));
waitHandle.WaitOne();
}
}
}