|
| 1 | +# Image Workflow Parser & Executor |
| 2 | + |
| 3 | +This example implements a workflow that executes various AI image processing steps, |
| 4 | +like image generation and transformations. |
| 5 | + |
| 6 | +The workflow is specified dynamically in a json-based workflow definition. |
| 7 | +The workflow can contain steps that call different image processing services: |
| 8 | +- to generate images by taking a screenshot with Puppeteer |
| 9 | +- to rotate images |
| 10 | +- to blur images |
| 11 | + |
| 12 | +```txt |
| 13 | ++--------+ Request +------------------------+ |
| 14 | +| Client | ------> | ImageProcessingWorkflow| |
| 15 | ++--------+ +------------------------+ |
| 16 | + ^ | |
| 17 | + | | Parse JSON workflow definition |
| 18 | + | v |
| 19 | + | +------------------------+ |
| 20 | + | | Workflow Execution |----+ |
| 21 | + | +------------------------+ | |
| 22 | + | | | |
| 23 | + | | +------------------------+ |
| 24 | + | | | Optional Services | |
| 25 | + | | | (any order) | |
| 26 | + | | | +-----------------+ | |
| 27 | + | | | | PuppeteerService| | |
| 28 | + | | | +-----------------+ | |
| 29 | + | | | | TransformerSvc | | |
| 30 | + | | | | (/blur) | | |
| 31 | + | | | +-----------------+ | |
| 32 | + | | | | TransformerSvc | | |
| 33 | + | | | | (/rotate) | | |
| 34 | + | | | +-----------------+ | |
| 35 | + | | +------------------------+ |
| 36 | + | | | |
| 37 | + | <-----------------+ |
| 38 | + | | |
| 39 | + | | Concatenate |
| 40 | + | v |
| 41 | + | +------------------------+ |
| 42 | + | | Result Assembly | |
| 43 | + | +------------------------+ |
| 44 | + | | |
| 45 | + +<-------------------------| |
| 46 | + Result Report |
| 47 | +``` |
| 48 | + |
| 49 | +**Note:** For simplicity, this example stores images locally in the folder `generated-images`. |
| 50 | + |
| 51 | +The example deploys each of the possible steps (generation, transformation) as |
| 52 | +a separate service, and then has a workflow service call them as specified in the |
| 53 | +workflow definition. |
| 54 | + |
| 55 | +## Download the example |
| 56 | + |
| 57 | +You can clone the example repository (`git clone https://github.com/restatedev/examples`) or just download this example via |
| 58 | + |
| 59 | +- **CLI:** `restate example java-workflow-interpreter` |
| 60 | + |
| 61 | +- **Zip archive:** https://github.com/restatedev/examples/releases/latest/download/java-workflow-interpreter.zip |
| 62 | + |
| 63 | +## Running the example |
| 64 | + |
| 65 | +1. Start Restate Server in a separate shell: `restate-server` |
| 66 | + |
| 67 | +2. Start the workflow and image transformation services: `mvn compile spring-boot:run` |
| 68 | + |
| 69 | +3. Register the example at Restate server by calling |
| 70 | + `restate -y deployment register "localhost:9080"`. |
| 71 | + |
| 72 | +## Demo scenario |
| 73 | + |
| 74 | +Here is list of example workflow execution requests that you can send to the workflow executor: |
| 75 | + |
| 76 | +### Example workflow |
| 77 | +Puppeteer screenshot -> rotate -> blur: |
| 78 | + |
| 79 | +```shell |
| 80 | +curl localhost:8080/ImageProcessingWorkflow/user123-wf1/run -H 'content-type: application/json' \ |
| 81 | + -d '[ |
| 82 | + {"action":"puppeteer","parameters":{"url":"https://restate.dev"}}, |
| 83 | + {"action":"rotate","parameters":{"angle":90}}, |
| 84 | + {"action":"blur","parameters":{"blur":5}} |
| 85 | + ]' |
| 86 | +``` |
| 87 | + |
| 88 | +Have a look at the `generated-images` folder to see the end result. |
| 89 | + |
| 90 | +### Retrieving state |
| 91 | +You can retrieve the workflow state via via the CLI. |
| 92 | +For example for id `user123-wf1`, do: |
| 93 | + |
| 94 | +```shell |
| 95 | +restate kv get ImageProcessingWorkflow user123-wf1 |
| 96 | +``` |
| 97 | +Result: |
| 98 | +``` |
| 99 | +🤖 State: |
| 100 | +――――――――― |
| 101 | + |
| 102 | + Service ImageProcessingWorkflow |
| 103 | + Key user123-wf1 |
| 104 | +
|
| 105 | + KEY VALUE |
| 106 | + status { |
| 107 | + "imgName": "c7879b2b-c213-f723-60c1-593422c98bc2", |
| 108 | + "output": [ |
| 109 | + "[Took screenshot of website with url: https://restate.dev]", |
| 110 | + "[Rotated image with angle: 90]", |
| 111 | + "[Blurred image with strength param 5]" |
| 112 | + ], |
| 113 | + "status": "Finished" |
| 114 | + } |
| 115 | +``` |
| 116 | + |
| 117 | +### More complex workflows |
| 118 | + |
| 119 | +Try more complex workflows, and have a look at the end result. |
| 120 | + |
| 121 | +- Puppeteer screenshot -> blur -> rotate -> rotate -> rotate -> rotate: |
| 122 | + |
| 123 | +```shell |
| 124 | +curl localhost:8080/ImageProcessingWorkflow/user123-wf2/run -H 'content-type: application/json' \ |
| 125 | + -d '[ |
| 126 | + {"action":"puppeteer","parameters":{"url":"https://restate.dev"}}, |
| 127 | + {"action":"blur","parameters":{"blur":5}}, |
| 128 | + {"action":"rotate","parameters":{"angle":90}}, |
| 129 | + {"action":"rotate","parameters":{"angle":90}}, |
| 130 | + {"action":"rotate","parameters":{"angle":90}}, |
| 131 | + {"action":"rotate","parameters":{"angle":90}} |
| 132 | + ]' |
| 133 | +``` |
| 134 | + |
| 135 | +- Invalid workflow definition (no image source is defined): |
| 136 | + |
| 137 | +```shell |
| 138 | +curl localhost:8080/ImageProcessingWorkflow/user123-wf3/run -H 'content-type: application/json' \ |
| 139 | + -d '[ |
| 140 | + {"action":"invalid","parameters":{"angle":90}}, |
| 141 | + {"action":"blur","parameters":{"blur":5}}]}' |
| 142 | +``` |
0 commit comments