-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path020-execution-layer-reference.mdc
More file actions
149 lines (117 loc) · 5 KB
/
Copy path020-execution-layer-reference.mdc
File metadata and controls
149 lines (117 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
description: Reference implementation for the resilient execution layer pattern
globs: "**/*.yaml"
alwaysApply: false
---
# Execution Layer Reference Implementation
## Full Template
Copy this template when creating new pipeline steps:
```bash
#!/usr/bin/env bash
# ============================================================
# EXECUTION LAYER - Resilient wrapper for observability
# ============================================================
SCRIPT_START=$(date +%s)
declare -a ACTION_LOG=()
FINAL_EXIT_CODE=0
# Enable debug mode if requested
if [[ "$(params.DEBUG)" == "true" ]]; then
set -x
fi
exec_action() {
local action_name="$1"
local timeout_secs="${2:-60}"
shift 2
local cmd="$*"
local start_ts=$(date +%s)
echo ""
echo "┌─────────────────────────────────────────────────────────────"
echo "│ ACTION: $action_name"
echo "│ CMD: $cmd"
echo "│ TIMEOUT: ${timeout_secs}s"
echo "├─────────────────────────────────────────────────────────────"
local output exit_code
output=$(timeout "$timeout_secs" bash -c "$cmd" 2>&1)
exit_code=$?
local end_ts=$(date +%s)
local duration=$((end_ts - start_ts))
if [[ -n "$output" ]]; then
echo "$output" | sed 's/^/│ /'
fi
local status
if [[ $exit_code -eq 0 ]]; then
status="OK"
elif [[ $exit_code -eq 124 ]]; then
status="TIMEOUT"
else
status="FAILED"
fi
echo "├─────────────────────────────────────────────────────────────"
echo "│ STATUS: $status (exit: $exit_code, duration: ${duration}s)"
echo "└─────────────────────────────────────────────────────────────"
ACTION_LOG+=("$status|$action_name|exit=$exit_code|${duration}s")
return $exit_code
}
execution_summary() {
local script_exit_code=$?
local script_end=$(date +%s)
local total_duration=$((script_end - SCRIPT_START))
echo ""
echo "╔═════════════════════════════════════════════════════════════"
echo "║ EXECUTION SUMMARY - <step-name>"
echo "║ Total Duration: ${total_duration}s"
echo "║ Final Exit Code: ${FINAL_EXIT_CODE:-$script_exit_code}"
echo "╠═════════════════════════════════════════════════════════════"
local ok_count=0 fail_count=0 timeout_count=0
for entry in "${ACTION_LOG[@]}"; do
local status=$(echo "$entry" | cut -d'|' -f1)
local action=$(echo "$entry" | cut -d'|' -f2)
local details=$(echo "$entry" | cut -d'|' -f3-)
case "$status" in
OK) ok_count=$((ok_count + 1)); echo "║ ✓ $action ($details)" ;;
FAILED) fail_count=$((fail_count + 1)); echo "║ ✗ $action ($details)" ;;
TIMEOUT) timeout_count=$((timeout_count + 1)); echo "║ ⏱ $action ($details)" ;;
esac
done
echo "╠═════════════════════════════════════════════════════════════"
echo "║ OK: $ok_count | FAILED: $fail_count | TIMEOUT: $timeout_count"
echo "╚═════════════════════════════════════════════════════════════"
exit ${FINAL_EXIT_CODE:-$script_exit_code}
}
trap execution_summary EXIT
# ============================================================
# RUN LOGIC - Business logic with fail-fast behavior
# ============================================================
set -eo pipefail
# Your business logic here...
```
## Usage Patterns
### Wrapping Commands
```bash
# With exec_action (preferred for external calls)
if ! exec_action "Fetch data" 60 "kubectl get pod -ojson"; then
echo "ERROR: Failed to fetch data"
FINAL_EXIT_CODE=1
exit 1
fi
# Quick validation (no external call)
if [[ -z "$REQUIRED_VAR" ]]; then
echo "ERROR: REQUIRED_VAR is empty"
FINAL_EXIT_CODE=1
exit 1
fi
ACTION_LOG+=("OK|Validate REQUIRED_VAR|exit=0|0s")
```
### Early Exit (Success)
```bash
if [[ "$CONDITION" == "skip" ]]; then
ACTION_LOG+=("OK|Skip - condition met|exit=0|0s")
FINAL_EXIT_CODE=0
exit 0
fi
```
## Key Principles
1. **Always set FINAL_EXIT_CODE before exit** - Ensures trap reports correct status
2. **Log manual validations** - Add to ACTION_LOG for visibility
3. **Timeout all external calls** - Prevent indefinite hangs
4. **Validate before use** - Check files/vars exist before operations