|
| 1 | +--- |
| 2 | +title: Building a Blueprint Runner |
| 3 | +--- |
| 4 | + |
| 5 | +import GithubFileReaderDisplay from '/components/GithubFileReaderDisplay'; |
| 6 | + |
| 7 | +# Building a Blueprint Runner |
| 8 | + |
| 9 | +This guide provides a step-by-step approach to building a Blueprint Runner, the primary component of a Blueprint. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Before you start building your Blueprint Runner, ensure you have: |
| 14 | + |
| 15 | +1. [Rust](https://www.rust-lang.org/tools/install) installed |
| 16 | +2. [Tangle CLI](../cli/installation.mdx) installed |
| 17 | +3. A basic understanding of [Blueprints](../blueprints/introduction.mdx) |
| 18 | +4. A Blueprint created with the CLI as seen in [Step 1](#step-1-setting-up-the-project-structure) below |
| 19 | + |
| 20 | +## Blueprint Runner Structure |
| 21 | + |
| 22 | +A Blueprint Runner consists of: |
| 23 | + |
| 24 | +1. **Entry Point**: The runner is the primary component in a blueprint's `main.rs` |
| 25 | +2. **[Jobs](/developers/blueprint-runner/jobs) Configuration**: Build the jobs that will be executed |
| 26 | +3. **[Router](/developers/blueprint-runner/routers) Configuration**: Setup for directing job calls to the jobs themselves for handling |
| 27 | +4. **[Producer](/developers/blueprint-runner/producers) Configuration**: Setup for the source of events (e.g. blockchain, external APIs) |
| 28 | +5. **[Consumer](/developers/blueprint-runner/consumers) Configuration**: Setup for the handling of results from jobs |
| 29 | +6. **[Background Service](/developers/blueprint-runner/background-services) Configuration**: Setup for supporting services (e.g. databases, servers, etc.) |
| 30 | + |
| 31 | +## Step-by-Step Guide |
| 32 | + |
| 33 | +### Step 1: Setting Up the Project Structure |
| 34 | + |
| 35 | +If you haven't already created a Blueprint project, you can use the Tangle CLI (if you haven't installed it yet, see [here](../cli/installation.mdx)): |
| 36 | + |
| 37 | +```bash |
| 38 | +cargo tangle blueprint create --name <BLUEPRINT_NAME> |
| 39 | +``` |
| 40 | + |
| 41 | +This creates a workspace with two main packages: |
| 42 | + |
| 43 | +- **Library Package**: Contains your job definitions and core logic |
| 44 | +- **Binary Package**: Contains your Blueprint Runner implementation |
| 45 | + |
| 46 | +### Step 2: Defining Your Jobs |
| 47 | + |
| 48 | +Before building the runner, define the jobs that your Blueprint will execute. Jobs are defined in the library package: |
| 49 | + |
| 50 | +<GithubFileReaderDisplay |
| 51 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-lib/src/lib.rs" |
| 52 | + fromLine={7} |
| 53 | + toLine={21} |
| 54 | + title="Job Definition from our Incredible Squaring Example" |
| 55 | +/> |
| 56 | + |
| 57 | +For more details on defining jobs, see the [Jobs documentation](/developers/blueprint-runner/jobs). |
| 58 | + |
| 59 | +### Step 3: Creating a Producer and a Consumer |
| 60 | + |
| 61 | +To build a Blueprint Runner, you need a producer to listen for events and a consumer to handle the results from jobs. |
| 62 | + |
| 63 | +<GithubFileReaderDisplay |
| 64 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 65 | + fromLine={28} |
| 66 | + toLine={31} |
| 67 | + title="Creating a Producer and a Consumer" |
| 68 | +/> |
| 69 | + |
| 70 | +### Step 4: Configuring the Router |
| 71 | + |
| 72 | +The [router](/developers/blueprint-runner/routers) directs job calls to the appropriate handlers. In the example below, we configure the router with a single route for our defined job and specify that it is on Tangle with the `TangleLayer`: |
| 73 | + |
| 74 | +<GithubFileReaderDisplay |
| 75 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 76 | + fromLine={33} |
| 77 | + toLine={48} |
| 78 | + title="Configuring the Router" |
| 79 | +/> |
| 80 | + |
| 81 | +This configuration: |
| 82 | +1. Creates a new router |
| 83 | +2. Adds a route for the `square` job with ID `XSQUARE_JOB_ID` |
| 84 | +3. Applies the `TangleLayer` to add metadata to job results |
| 85 | +4. Adds a filter layer to only process jobs that match the service ID |
| 86 | + |
| 87 | +For more details on routers, see the [Routers documentation](/developers/blueprint-runner/routers). |
| 88 | + |
| 89 | +### Step 5: Defining a Background Service |
| 90 | + |
| 91 | +Some blueprints may require one or more services to run in the background. Any number of background services can be set to run for a Blueprint Runner. |
| 92 | + |
| 93 | +<GithubFileReaderDisplay |
| 94 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-lib/src/lib.rs" |
| 95 | + fromLine={23} |
| 96 | + toLine={34} |
| 97 | + title="Defining a Background Service" |
| 98 | +/> |
| 99 | + |
| 100 | +With a background service defined, it just needs to be added to the Blueprint Runner: |
| 101 | + |
| 102 | +<GithubFileReaderDisplay |
| 103 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 104 | + fromLine={36} |
| 105 | + toLine={49} |
| 106 | + title="Adding a Background Service to the Blueprint Runner" |
| 107 | +/> |
| 108 | + |
| 109 | +### Step 6: Configuring a Producer with the Blueprint Runner's Builder |
| 110 | + |
| 111 | +[Producers](/developers/blueprint-runner/producers) listen for events and prepare them for processing. The following example uses a `TangleProducer` as seen in [Step 3](#step-3-creating-a-producer-and-a-consumer): |
| 112 | + |
| 113 | +<GithubFileReaderDisplay |
| 114 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 115 | + fromLine={36} |
| 116 | + toLine={54} |
| 117 | + title="Adding a Producer to the Blueprint Runner" |
| 118 | +/> |
| 119 | + |
| 120 | +This producer listens for finalized blocks on the Tangle network and converts them into job calls that can be processed by the router. |
| 121 | + |
| 122 | +For more details on producers, see the [Producers documentation](/developers/blueprint-runner/producers). |
| 123 | + |
| 124 | +### Step 7: Configuring a Consumer with the Blueprint Runner's Builder |
| 125 | + |
| 126 | +[Consumers](/developers/blueprint-runner/consumers) handle the results of processed jobs. In the example above, we set up a Tangle consumer as seen in [Step 3](#step-3-creating-a-producer-and-a-consumer): |
| 127 | + |
| 128 | +<GithubFileReaderDisplay |
| 129 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 130 | + fromLine={36} |
| 131 | + toLine={60} |
| 132 | + title="Adding a Consumer to the Blueprint Runner" |
| 133 | +/> |
| 134 | + |
| 135 | +This consumer processes job results and can send transactions to the Tangle network based on those results. |
| 136 | + |
| 137 | +For more details on consumers, see the [Consumers documentation](/developers/blueprint-runner/consumers). |
| 138 | + |
| 139 | +### Step 8: Custom Shutdown Logic |
| 140 | + |
| 141 | +Implement customized shutdown logic to handle cleanup and resource release: |
| 142 | + |
| 143 | +<GithubFileReaderDisplay |
| 144 | + url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs" |
| 145 | + fromLine={36} |
| 146 | + toLine={65} |
| 147 | + title="Custom Shutdown Logic" |
| 148 | +/> |
| 149 | + |
| 150 | +The above example simply prints a message when the Blueprint Runner is shutting down. |
| 151 | + |
| 152 | +### Step 9: Running the Blueprint Runner |
| 153 | + |
| 154 | +Finally, we run the Blueprint Runner: |
| 155 | + |
| 156 | +```rust |
| 157 | +// Build and run the Blueprint Runner |
| 158 | +let result = BlueprintRunner::builder(tangle_config, env) |
| 159 | + // ... configuration ... |
| 160 | + .run() |
| 161 | + .await; |
| 162 | +``` |
| 163 | + |
| 164 | +The `run` method starts the Blueprint Runner and returns a result indicating whether it ran successfully or encountered an error. |
| 165 | + |
| 166 | +With this, the Blueprint Runner, the centerpoint of your Blueprint is ready to be used! |
| 167 | + |
| 168 | +## Next Steps |
| 169 | + |
| 170 | +After building your Blueprint Runner, you might want to explore: |
| 171 | + |
| 172 | +- [Advanced Router Features](/developers/blueprint-runner/routers#advanced-router-features) |
| 173 | +- [Producer Patterns](/developers/blueprint-runner/producers#producer-patterns) |
| 174 | +- [Consumer Patterns](/developers/blueprint-runner/consumers#consumer-patterns) |
| 175 | + |
| 176 | +## Conclusion |
| 177 | + |
| 178 | +Building a Blueprint Runner involves setting up various components that work together to execute your Tangle Blueprint. By following this guide and adhering to best practices, you can create a robust and efficient Blueprint Runner for your Actively Validated Service on the Tangle Network. |
0 commit comments