Skip to content

Commit afb202a

Browse files
committed
[docs] Example of workflow
1 parent cc12621 commit afb202a

5 files changed

Lines changed: 88 additions & 69 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ The configuration file may look like this:
148148
scheduler.queue.capacity: 0
149149
metrics.enabled: true
150150

151+
## Workflows
152+
153+
Serverledge supports the composition of multiple functions into workflows.
154+
More information [here](./docs/workflows.md)
151155

152156
## Additional Documentation
153157

docs/workflows.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Workflows
2+
3+
Serverledge accepts workflows defined by users through a subset of the JSON-based *Amazon States Language*, currently in use by AWS Step Functions.
4+
5+
## Tasks
6+
7+
Serverledge workflows currently comprise 4 types of *tasks*:
8+
- **Simple**: a task that wraps a function. This is the only task that executes user-defined functions.
9+
- **Choice**: a task with N alternative branches, each associated with a condition; execution control and input data are transferred to the first branch whose condition is evaluated as true
10+
- **FanOut**: a task with N outputs that copies (or scatters) the input to all the outputs (with subsequent nodes activated in parallel); *experimental*
11+
- **FanIn**: a task with N inputs that waits for the termination of all the parent nodes, and then merges the results in one output. The node fails after a specified timeout.
12+
13+
Other special types of tasks are always present and pre-built when using the APIs:
14+
- **Start**: the task from which the workflow starts executing (not associated with any function)
15+
- **End**: the final task of the workflow (not associated with any function)
16+
- **Success**: a task that -- as soon it is activated -- terminates workflow execution reporting success
17+
- **Fail**: a task that -- as soon it is activated -- terminates workflow execution reporting a failure
18+
19+
20+
## Writing Functions
21+
22+
*Simple* tasks execute a regular Serverledge function. Any function previously
23+
registered in Serverledge can be associated with a Simple task. The only
24+
additional requirement is that **functions used in workflows must be associated
25+
with a Signature upon creation** (which is optional in general).
26+
27+
### Signature
28+
A signature specifies the name and the type of the inputs accepted by the function, as well as the type of the returned outputs. For instance, a *Fibonacci* function might have a single integer input, and produce a single integer output.
29+
30+
The signature can be specified when creating a function through the CLI.
31+
32+
Inputs can be specified using the `--input` (or `-i` for short) option, while outputs are specified using the `--output` (or `-o` for short) option.
33+
34+
Both inputs and outputs are specified using the syntax `name:type`, where `type` is one of the following strings: `Int`, `Float`, `Text`, `Bool`.
35+
36+
Example:
37+
38+
bin/serverledge-cli create --function inc \
39+
--memory 128 \
40+
--src examples/inc.py \
41+
--runtime python310 \
42+
--handler "inc.handler"\
43+
--input "n:Int" --output "m:Int"
44+
45+
46+
47+
## Example
48+
49+
The file `examples/workflow-simple.json` contains an example ASL definition of
50+
a workflow. To register it:
51+
52+
bin/serverledge-cli create-workflow -s examples/workflow-simple.json -f myWorkflow
53+
54+
To execute it with input `n=2`:
55+
56+
bin/serverledge-cli invoke-workflow -f myWorkflow -p "input:2"
57+

docs/writing-function-compositions.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

examples/workflow-simple.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"Comment": "A simple state machine",
3+
"StartAt": "FirstState",
4+
"States": {
5+
"FirstState": {
6+
"Comment": "The first task",
7+
"Type": "Task",
8+
"Resource": "inc",
9+
"Next": "SecondState"
10+
},
11+
"SecondState": {
12+
"Comment": "The second task",
13+
"Type": "Task",
14+
"Resource": "inc",
15+
"Next": "Final"
16+
},
17+
"Final": {
18+
"Comment": "The end task",
19+
"Type": "Task",
20+
"Resource": "inc",
21+
"End": true
22+
}
23+
}
24+
}

internal/cli/cli.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ var paramsFile string
101101
var asyncInvocation bool
102102
var verbose bool
103103
var returnOutput bool
104-
var rmFnOnDeletion bool
105104

106105
func Init() {
107106
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
@@ -149,9 +148,8 @@ func Init() {
149148
compInvokeCmd.Flags().BoolVarP(&asyncInvocation, "async", "a", false, "Asynchronous workflow invocation")
150149

151150
rootCmd.AddCommand(compCreateCmd)
152-
compCreateCmd.Flags().StringVarP(&compName, "workflow", "f", "", "name of the function")
151+
compCreateCmd.Flags().StringVarP(&compName, "workflow", "f", "", "name of the workflow")
153152
compCreateCmd.Flags().StringVarP(&jsonSrc, "src", "s", "", "source Amazon States Language file that defines the workflow")
154-
compCreateCmd.Flags().BoolVarP(&rmFnOnDeletion, "deletion", "d", false, "flag to delete also functions associated with the FC")
155153

156154
rootCmd.AddCommand(compDeleteCmd)
157155
compDeleteCmd.Flags().StringVarP(&compName, "workflow", "f", "", "name of the workflow")
@@ -506,7 +504,7 @@ func createWorkflow(cmd *cobra.Command, args []string) {
506504

507505
src, err := os.ReadFile(jsonSrc)
508506
if err != nil {
509-
fmt.Errorf("Could not read source file: %s\n", jsonSrc)
507+
fmt.Printf("Could not read source file: %s\n", jsonSrc)
510508
os.Exit(1)
511509
}
512510
encoded := base64.StdEncoding.EncodeToString(src)
@@ -520,7 +518,7 @@ func createWorkflow(cmd *cobra.Command, args []string) {
520518
os.Exit(3)
521519
}
522520

523-
url := fmt.Sprintf("http://%s:%d/workflow/createASL", ServerConfig.Host, ServerConfig.Port)
521+
url := fmt.Sprintf("http://%s:%d/workflow/create", ServerConfig.Host, ServerConfig.Port)
524522
resp, err := utils.PostJson(url, requestBody)
525523
if err != nil {
526524
// TODO: check returned error code

0 commit comments

Comments
 (0)