Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recipe post #82

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions blog/2022-04-18-workflows-recipe-analogy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
slug: workflows-as-recipe
title: What are workflows? A recipe analogy
authors: doug
tags: [Netflix Conductor, Orkes, microservices, workflow, 2022]
image: https://orkes.io/content/img/blogassets/recipe.jpg
---

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."

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:


"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."

## Workflows as recipes

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.

<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>


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..

> 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/

The steps are (skipping some substeps for clarity):

1. Gather Your Ingredients for the Sandwich
2. Pull Out Two Slices of Bread
3. Open Peanut Butter and Jelly
4. Spread the Peanut Butter Onto One Slice of Bread
5. Spread the Jelly Onto the Other Slice of Bread
6. Combine the Two Slices
7. Clean Up Your Workspace
8. Enjoy Your Sandwich

(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)

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.

## Building a Conductor Workflow

We can turn these 8 steps into a Conductor workflow.
<p align="center"><img src="/content/img/blogassets/pbj1.png" alt="PB&J example workflow" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>

> 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.


This workflow shows clear steps with arrows pointing to the next step - so it is visually possible to see how the workflow will progress.

## Improving the workflow

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.

### Independent tasks

Let's remove the PB dependency from the jelly spreading - as the order of PB vs. jelly does not matter.

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.

We can now apply a fork to apply PB, and a second fork to apply the jelly:

<p align="center"><img src="/content/img/blogassets/pbj2.jpg" alt="PB&J example workflow" width="800" style={{paddingBottom: 40, paddingTop: 40}} /></p>

This is version 2 of the workflow, and you can see it in the [playground](https://play.orkes.io/workflowDef/PBJ/2)

Now, these operations are independent, and if there is space in the jelly task queue - that can be completed ahead of the PB.

## Recipe variations

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}.


### Cooking a burrito

Let's look at a common example - from a burrito in my freezer. The directions vary depending on the cooking method.

<p align="center"><img src="/content/img/blogassets/burrito.jpg" alt="burrito instructions" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>


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.

<p align="center"><img src="/content/img/blogassets/burrito_workflow.png" alt="burrito workflow" width="400" style={{paddingBottom: 40, paddingTop: 40}} /></p>


## Conclusion

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.

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.

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!
Binary file added blog/assets/burrito.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blog/assets/burrito_workflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blog/assets/pbj1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blog/assets/pbj2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/how-tos/sdks/conductor-clojure/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

## Get the SDK
https://clojars.org/io.orkes/conductor-clojure

## Quick Guide

1. Create connection options
Expand Down
112 changes: 81 additions & 31 deletions docs/how-tos/sdks/conductor-csharp/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ To find out more about Conductor visit: [https://github.com/Netflix/conductor](h
`conductor-csharp` repository provides the client SDKs to build Task Workers and Clients in C#

## Quick Start
1. [Get Secrets](#Get-Secrets)
2. [Write workers](#Write-workers)
3. [Run workers](#Run-workers)
4. [Worker Configurations](#Worker-Configurations)
5. [Starting workflow](#Starting-workflow)

1. [Write workers](#Write-workers)
2. [Run workers](#Run-workers)
3. [Worker Configurations](#Worker-Configurations)
4. [Starting workflow](#Starting-workflow)

### Get Secrets
Executing workflow or polling a task from a playground requires keyId and keySecret.
Please follow [guide](https://orkes.io/content/docs/codelab/helloworld#application-permissions) to provision one for your application.

### Write workers

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

### Run workers
Create main method that does the following:
1. Adds configurations such as metrics, authentication, thread count, Conductor server URL
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
Microsoft.Extensions.Hosting is also required.
2. Add your workers
3. Start the workers to poll for work
```
Expand All @@ -66,28 +70,33 @@ using Conductor.Client.Extensions;
using Conductor.Client.Interfaces;

using Task = System.Threading.Tasks.Task;
using Conductor.Client;
using System.Collections.Concurrent;

namespace SimpleConductorWorker
namespace TestOrkesSDK
{
class Program
{
static async Task Main(string[] args)
static void Main(string[] args)
{
await new HostBuilder()
.ConfigureServices((ctx, services) =>
{

Configuration configuration = new Configuration(new ConcurrentDictionary<string, string>(), "keyId", "keySecret");
services.AddConductorWorker(configuration);
services.AddConductorWorkflowTask<MyWorkflowTask>();
services.AddHostedService<WorkflowsWorkerService>();
})
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddConsole();
})
.RunConsoleAsync();
new HostBuilder()
.ConfigureServices((ctx, services) =>
{
// First argument is optional headers which client wasnt to pass.
Configuration configuration = new Configuration(new ConcurrentDictionary<string, string>(),
"keyId",
"keySecret");
services.AddConductorWorker(configuration);
services.AddConductorWorkflowTask<MyWorkflowTask>();
services.AddHostedService<WorkflowsWorkerService>();
})
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddConsole();
})
.RunConsoleAsync();
Console.ReadLine();
}
}

Expand Down Expand Up @@ -115,6 +124,21 @@ namespace SimpleConductorWorker
await workflowTaskCoordinator.Start();
}
}

internal class MyWorkflowTask : IWorkflowTask
{
public MyWorkflowTask() { }

public string TaskType => "my_ctask";
public int? Priority => null;

public async Task<TaskResult> Execute(Conductor.Client.Models.Task task, CancellationToken token)
{
Dictionary<string, object> newOutput = new Dictionary<string, object>();
newOutput.Add("output", 1);
return task.Completed(task.OutputData.MergeValues(newOutput));
}
}
}
```
Save above code with workers code in Program.cs and run it using consoleApplication.
Expand All @@ -124,16 +148,42 @@ Alternatively it can also be hosted as windows service.
Worker configuration is handled via Configuraiton object passed when initializing TaskHandler

### Starting workflow
Below is the code snippet in order to start the workflow,
Below is the code snippet in order to register and start the workflow,
```
//Optional headers clients wants to pass.
Dictionary<string, Object> optionalHeaders = new Dictionary<string, Object>();
Configuration configuration = new Configuration(optionalHeaders, "keyId", "keySecret", "https://play.orkes.io/");
WorkflowResourceApi workflowResourceApi = new WorkflowResourceApi(configuration);
Dictionary<string, Object> input = new Dictionary<string, Object>();
//Fill the input map which workflow consumes.
workflowResourceApi.StartWorkflow("Stack_overflow_sequential_manan", input, 1);
IDictionary<string, string> optionalHeaders = new ConcurrentDictionary<string, string>();
Configuration configuration = new Configuration(optionalHeaders, "keyId", "keySecret");

//Create task definition
MetadataResourceApi metadataResourceApi = new MetadataResourceApi(configuration);
TaskDef taskDef = new TaskDef(name: "test_task");
taskDef.OwnerEmail = "test@test.com";
metadataResourceApi.RegisterTaskDef(new List<TaskDef>() { taskDef});

//Create workflow definition
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.Name = "test_workflow";
workflowDef.OwnerEmail = "test@test.com";
workflowDef.SchemaVersion = 2;

WorkflowTask workflowTask = new WorkflowTask();
workflowTask.Type = "HTTP";
workflowTask.Name = "test_"; //Same as registered task definition.
IDictionary<string, string> requestParams = new Dictionary<string, string>();
requestParams.Add("uri", "https://www.google.com"); //adding a key/value using the Add() method
requestParams.Add("method", "GET");
Dictionary<string, object> request = new Dictionary<string, object>();
request.Add("http_request", requestParams);
workflowTask.InputParameters = request;
workflowDef.Tasks = new List<WorkflowTask>() { workflowTask };
//Run a workflow
WorkflowResourceApi workflowResourceApi = new WorkflowResourceApi(configuration);
Dictionary<string, Object> input = new Dictionary<string, Object>();
//Fill the input map which workflow consumes.
workflowResourceApi.StartWorkflow("test_workflow", input, 1);
Console.ReadLine();
```
Please go through Conductor.Api package to find out supported apis


### Running Conductor server locally in 2-minute
More details on how to run Conductor see https://netflix.github.io/conductor/server/
Expand Down
Binary file added static/img/blogassets/burrito.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/blogassets/burrito_workflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/blogassets/pbj1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/blogassets/pbj2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/blogassets/recipe.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.