Routine
Routine hooks allow you to execute reusable step sequences loaded from external YAML files. This is particularly useful for creating shared templates, standardizing common workflows, and maintaining DRY (Don't Repeat Yourself) principles across your data pipelines.
Configuration
- type: routine
routine: "my_routine_name" # Required: Name of the routine to execute
params: # Optional: Parameters to pass to the routine
param1: "value1"
param2: "value2"
env: # Optional: Environment variables for all steps
ENV_VAR1: "value1"
ENV_VAR2: "value2"
on_failure: abort # Optional: abort/warn/quiet/skip
id: my_id # Optional. Will be generated. Use `log` hook with {runtime_state} to view state.Properties
routine
Yes
Name of the routine to execute (must exist in a routine file)
params
No
Map of parameters to pass to the routine steps
env
No
Map of environment variables available to all steps in the routine
on_failure
No
What to do if any step fails (abort/warn/quiet/skip)
Routine File Structure
Routines are loaded from YAML files in the directory specified by the SLING_ROUTINES_DIR environment variable. Each file can contain multiple named routines:
Required Parameters
You can specify required parameters for a routine using a comment above the routine name. This ensures that all necessary parameters are provided when the routine is called:
If a required parameter is not provided when calling the routine, the execution will fail with an error message listing the missing parameters:
Error message:
Notes about required_params:
Must be specified as a YAML comment directly above the routine name
Use YAML array syntax:
# required_params: [param1, param2, param3]Parameter names should match exactly what you reference in the routine steps
Validation happens before the routine steps are executed
Helps prevent runtime errors and provides clear feedback about missing configuration
Now call in a replication or pipeline:
Important Notes:
The
SLING_ROUTINES_DIRenvironment variable must be setOnly
.yamland.ymlfiles are consideredRoutine names must be unique across all files in the directory
The directory is scanned recursively, so feel free to create sub-folders
You can set the env var in the env section of a replication or pipeline, or as a regular environment variables before running sling:
Output
When the routine hook executes successfully, it returns the following output that can be accessed in subsequent hooks:
You can access these values in subsequent hooks using the following syntax (jmespath):
{state.hook_id.status}- Status of the hook execution{state.hook_id.routine}- The routine name that was executed
Returning Custom Output from Routines
Routines can return custom output values using the store hook with the output. prefix. Any key starting with output. will be accessible to the caller after the routine completes:
In the routine definition:
Calling the routine and accessing output:
Key Points:
Use
storehook withkey: output.<name>to define return valuesAccess via
{state.routine_id.<name>}after the routine completesAny data type can be returned (strings, numbers, objects, arrays)
Multiple output values can be set using multiple
storehooksOutput values are only accessible if the routine completes successfully
Within routine steps, you can access:
{params.param_name}- Parameters passed to the routine{env.ENV_VAR}- Environment variables set for the routine
Examples
Basic Routine Usage
Execute a simple routine without parameters:
Routine with Parameters
Pass dynamic parameters to a reusable routine:
Routine with Environment Variables
Set environment variables for all steps in the routine:
Conditional Routine Execution
Execute a routine based on conditions:
Chaining Routines
Execute multiple routines in sequence:
Routine with Output Values
Create a routine that returns custom values for use in subsequent steps:
Define the routine:
Use the routine and its outputs:
Complex Routine Example
Create a comprehensive routine file for data processing:
Usage:
Last updated
Was this helpful?