Skip to content

Commit aeae444

Browse files
authored
Merge pull request #82 from orkes-io/staging
recipe post
2 parents 1507191 + a35ab27 commit aeae444

File tree

12 files changed

+177
-31
lines changed

12 files changed

+177
-31
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
slug: workflows-as-recipe
3+
title: What are workflows? A recipe analogy
4+
authors: doug
5+
tags: [Netflix Conductor, Orkes, microservices, workflow, 2022]
6+
image: https://orkes.io/content/img/blogassets/recipe.jpg
7+
---
8+
9+
What is a workflow? Wikipedia says "A workflow consists of an orchestrated and repeatable pattern of activity, enabled by the systematic organization of resources into processes that transform materials, provide services, or process information."
10+
11+
None of that is incorrect. But it is certainly a mouthful. If I am asked in an elevator what a workflow orchestration is, I like to use analogies to make the point as approachable as possible:
12+
13+
14+
"It's like a recipe for code. A recipe has a series of steps, that must be run in a specific order. Iyt can also have different options for food allergies, or different ingredients you might have on hand? A workflow is a recipe for your code, and can be built to handle many of the changes that might reasonably occur when the code runs."
15+
16+
## Workflows as recipes
17+
18+
You may have seen one of the name videos out there of parents teaching kids code by writing out the steps to create a Peanut butter and Jelly sandwich.
19+
20+
<p align="center"><iframe width="560" height="315" src="https://www.youtube.com/embed/Ct-lOOUqmyY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
21+
22+
23+
We're not going to be quite as silly as that Dad, but we'll run through some instructions on how to [create a PB&J](https://www.instructables.com/How-to-Make-a-Peanut-Butter-and-Jelly-Sandwich-4/) from Instructables..
24+
25+
> If you look at the URL of that recipe - it appears it took 4 tries to get it right :D https://www.instructables.com/How-to-Make-a-Peanut-Butter-and-Jelly-Sandwich-4/
26+
27+
The steps are (skipping some substeps for clarity):
28+
29+
1. Gather Your Ingredients for the Sandwich
30+
2. Pull Out Two Slices of Bread
31+
3. Open Peanut Butter and Jelly
32+
4. Spread the Peanut Butter Onto One Slice of Bread
33+
5. Spread the Jelly Onto the Other Slice of Bread
34+
6. Combine the Two Slices
35+
7. Clean Up Your Workspace
36+
8. Enjoy Your Sandwich
37+
38+
(if you read closely, I skipped 3 steps - wearing gloves, removing the crust and cutting the sandwich in half... because, well - that's all just ridiculous)
39+
40+
The eight steps above are a workflow. They must be performed in that order to create a sandwich - you cannot spread the PB before you lay out the bread.
41+
42+
## Building a Conductor Workflow
43+
44+
We can turn these 8 steps into a Conductor workflow.
45+
<p align="center"><img src="/content/img/blogassets/pbj1.png" alt="PB&J example workflow" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>
46+
47+
> If you'd like to see the definition of this workflow, you can check out the code on the [Orkes Playground](https://play.orkes.io/workflowDef/PBJ/1). It's free to sign up.
48+
49+
50+
This workflow shows clear steps with arrows pointing to the next step - so it is visually possible to see how the workflow will progress.
51+
52+
## Improving the workflow
53+
54+
Each task is run by a microservice, so making changes and adding tasks is easy to do. In this example - you see that the recipe calls for PB to be spread first, and then the jelly. This works for a single human - these steps each take 2 hands. But there is no reason that the jelly cannot go first, and THEN the PB. Or - if you had more hands - the PB and the Jelly could be spread simultaneously.
55+
56+
### Independent tasks
57+
58+
Let's remove the PB dependency from the jelly spreading - as the order of PB vs. jelly does not matter.
59+
60+
We can do this in Conductor with a [FORK](https://orkes.io/content/docs/reference-docs/fork-task). A fork splits your workflow into 2 asynchronous tasks, and then a [JOIN](https://orkes.io/content/docs/reference-docs/join-task) reconnects the the workflow into a single path.
61+
62+
We can now apply a fork to apply PB, and a second fork to apply the jelly:
63+
64+
<p align="center"><img src="/content/img/blogassets/pbj2.jpg" alt="PB&J example workflow" width="800" style={{paddingBottom: 40, paddingTop: 40}} /></p>
65+
66+
This is version 2 of the workflow, and you can see it in the [playground](https://play.orkes.io/workflowDef/PBJ/2)
67+
68+
Now, these operations are independent, and if there is space in the jelly task queue - that can be completed ahead of the PB.
69+
70+
## Recipe variations
71+
72+
Often, recipes have variations to preparation, and they can be read like an IF statement in programming (If (fresh tomatoes) {do x}, else if (tinned tomatoes) {do y}.
73+
74+
75+
### Cooking a burrito
76+
77+
Let's look at a common example - from a burrito in my freezer. The directions vary depending on the cooking method.
78+
79+
<p align="center"><img src="/content/img/blogassets/burrito.jpg" alt="burrito instructions" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>
80+
81+
82+
We can emulate this in a workflow using a switch task. The Switch task takes in the workflow ovenType input ```${workflow.input.ovenType}``` and based on this value will make a decision. The default case is for the microwave, and then second ```decisionCase``` is set to "oven". From that input, the different tasks can be run.
83+
84+
<p align="center"><img src="/content/img/blogassets/burrito_workflow.png" alt="burrito workflow" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>
85+
86+
87+
## Conclusion
88+
89+
Workflows are a series of tasks that must be followed in a certain order. In this post, we used cooking recipes as an analogy to a workflow - they too are a series of tasks that must be followed in a certain order.
90+
91+
We created sample workflows for making a peanut and jelly sandwich ([version 1](https://play.orkes.io/workflowDef/PBJ/1) and [version2](https://play.orkes.io/workflowDef/PBJ/2)) and another workflow to cook a [frozen burrito](https://play.orkes.io/workflowDef/burrito) with microwave or oven instructions.
92+
93+
If you're curious about how to build a workflow orchestration - it might be a fun exercise to try your favorite recipe as a workflow. You can build on the workflows from this post in our free playground. Feel free to share what you came up with in our [Discord](https://discord.gg/pYYdYsYTAw). We love seeing creative uses of workflows!

blog/assets/burrito.jpg

123 KB
Loading

blog/assets/burrito_workflow.png

78.2 KB
Loading

blog/assets/pbj1.png

66.2 KB
Loading

blog/assets/pbj2.jpg

109 KB
Loading

docs/how-tos/sdks/conductor-clojure/main/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Software Development Kit for Netflix Conductor, written on and providing support for Clojure
44

5+
## Get the SDK
6+
https://clojars.org/io.orkes/conductor-clojure
7+
58
## Quick Guide
69

710
1. Create connection options

docs/how-tos/sdks/conductor-csharp/main/README.md

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ To find out more about Conductor visit: [https://github.com/Netflix/conductor](h
66
`conductor-csharp` repository provides the client SDKs to build Task Workers and Clients in C#
77

88
## Quick Start
9+
1. [Get Secrets](#Get-Secrets)
10+
2. [Write workers](#Write-workers)
11+
3. [Run workers](#Run-workers)
12+
4. [Worker Configurations](#Worker-Configurations)
13+
5. [Starting workflow](#Starting-workflow)
914

10-
1. [Write workers](#Write-workers)
11-
2. [Run workers](#Run-workers)
12-
3. [Worker Configurations](#Worker-Configurations)
13-
4. [Starting workflow](#Starting-workflow)
14-
15+
### Get Secrets
16+
Executing workflow or polling a task from a playground requires keyId and keySecret.
17+
Please follow [guide](https://orkes.io/content/docs/codelab/helloworld#application-permissions) to provision one for your application.
1518

1619
### Write workers
1720

@@ -50,7 +53,8 @@ To find out more about Conductor visit: [https://github.com/Netflix/conductor](h
5053

5154
### Run workers
5255
Create main method that does the following:
53-
1. Adds configurations such as metrics, authentication, thread count, Conductor server URL
56+
1. Search for package called conductor-csharp in microsoft nuget package manager and install it as dependencies. If you are planning to run worker then
57+
Microsoft.Extensions.Hosting is also required.
5458
2. Add your workers
5559
3. Start the workers to poll for work
5660
```
@@ -66,28 +70,33 @@ using Conductor.Client.Extensions;
6670
using Conductor.Client.Interfaces;
6771
6872
using Task = System.Threading.Tasks.Task;
73+
using Conductor.Client;
74+
using System.Collections.Concurrent;
6975
70-
namespace SimpleConductorWorker
76+
namespace TestOrkesSDK
7177
{
7278
class Program
7379
{
74-
static async Task Main(string[] args)
80+
static void Main(string[] args)
7581
{
76-
await new HostBuilder()
77-
.ConfigureServices((ctx, services) =>
78-
{
79-
80-
Configuration configuration = new Configuration(new ConcurrentDictionary<string, string>(), "keyId", "keySecret");
81-
services.AddConductorWorker(configuration);
82-
services.AddConductorWorkflowTask<MyWorkflowTask>();
83-
services.AddHostedService<WorkflowsWorkerService>();
84-
})
85-
.ConfigureLogging(logging =>
86-
{
87-
logging.SetMinimumLevel(LogLevel.Debug);
88-
logging.AddConsole();
89-
})
90-
.RunConsoleAsync();
82+
new HostBuilder()
83+
.ConfigureServices((ctx, services) =>
84+
{
85+
// First argument is optional headers which client wasnt to pass.
86+
Configuration configuration = new Configuration(new ConcurrentDictionary<string, string>(),
87+
"keyId",
88+
"keySecret");
89+
services.AddConductorWorker(configuration);
90+
services.AddConductorWorkflowTask<MyWorkflowTask>();
91+
services.AddHostedService<WorkflowsWorkerService>();
92+
})
93+
.ConfigureLogging(logging =>
94+
{
95+
logging.SetMinimumLevel(LogLevel.Debug);
96+
logging.AddConsole();
97+
})
98+
.RunConsoleAsync();
99+
Console.ReadLine();
91100
}
92101
}
93102
@@ -115,6 +124,21 @@ namespace SimpleConductorWorker
115124
await workflowTaskCoordinator.Start();
116125
}
117126
}
127+
128+
internal class MyWorkflowTask : IWorkflowTask
129+
{
130+
public MyWorkflowTask() { }
131+
132+
public string TaskType => "my_ctask";
133+
public int? Priority => null;
134+
135+
public async Task<TaskResult> Execute(Conductor.Client.Models.Task task, CancellationToken token)
136+
{
137+
Dictionary<string, object> newOutput = new Dictionary<string, object>();
138+
newOutput.Add("output", 1);
139+
return task.Completed(task.OutputData.MergeValues(newOutput));
140+
}
141+
}
118142
}
119143
```
120144
Save above code with workers code in Program.cs and run it using consoleApplication.
@@ -124,16 +148,42 @@ Alternatively it can also be hosted as windows service.
124148
Worker configuration is handled via Configuraiton object passed when initializing TaskHandler
125149

126150
### Starting workflow
127-
Below is the code snippet in order to start the workflow,
151+
Below is the code snippet in order to register and start the workflow,
128152
```
129-
//Optional headers clients wants to pass.
130-
Dictionary<string, Object> optionalHeaders = new Dictionary<string, Object>();
131-
Configuration configuration = new Configuration(optionalHeaders, "keyId", "keySecret", "https://play.orkes.io/");
132-
WorkflowResourceApi workflowResourceApi = new WorkflowResourceApi(configuration);
133-
Dictionary<string, Object> input = new Dictionary<string, Object>();
134-
//Fill the input map which workflow consumes.
135-
workflowResourceApi.StartWorkflow("Stack_overflow_sequential_manan", input, 1);
153+
IDictionary<string, string> optionalHeaders = new ConcurrentDictionary<string, string>();
154+
Configuration configuration = new Configuration(optionalHeaders, "keyId", "keySecret");
155+
156+
//Create task definition
157+
MetadataResourceApi metadataResourceApi = new MetadataResourceApi(configuration);
158+
TaskDef taskDef = new TaskDef(name: "test_task");
159+
taskDef.OwnerEmail = "test@test.com";
160+
metadataResourceApi.RegisterTaskDef(new List<TaskDef>() { taskDef});
161+
162+
//Create workflow definition
163+
WorkflowDef workflowDef = new WorkflowDef();
164+
workflowDef.Name = "test_workflow";
165+
workflowDef.OwnerEmail = "test@test.com";
166+
workflowDef.SchemaVersion = 2;
167+
168+
WorkflowTask workflowTask = new WorkflowTask();
169+
workflowTask.Type = "HTTP";
170+
workflowTask.Name = "test_"; //Same as registered task definition.
171+
IDictionary<string, string> requestParams = new Dictionary<string, string>();
172+
requestParams.Add("uri", "https://www.google.com"); //adding a key/value using the Add() method
173+
requestParams.Add("method", "GET");
174+
Dictionary<string, object> request = new Dictionary<string, object>();
175+
request.Add("http_request", requestParams);
176+
workflowTask.InputParameters = request;
177+
workflowDef.Tasks = new List<WorkflowTask>() { workflowTask };
178+
//Run a workflow
179+
WorkflowResourceApi workflowResourceApi = new WorkflowResourceApi(configuration);
180+
Dictionary<string, Object> input = new Dictionary<string, Object>();
181+
//Fill the input map which workflow consumes.
182+
workflowResourceApi.StartWorkflow("test_workflow", input, 1);
183+
Console.ReadLine();
136184
```
185+
Please go through Conductor.Api package to find out supported apis
186+
137187

138188
### Running Conductor server locally in 2-minute
139189
More details on how to run Conductor see https://netflix.github.io/conductor/server/

static/img/blogassets/burrito.jpg

123 KB
Loading
78.2 KB
Loading

static/img/blogassets/pbj1.png

66.2 KB
Loading

0 commit comments

Comments
 (0)