-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrequest-simulation-model-versions.sh
More file actions
executable file
·194 lines (171 loc) · 5.33 KB
/
Copy pathrequest-simulation-model-versions.sh
File metadata and controls
executable file
·194 lines (171 loc) · 5.33 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/env bash
set -e
# Google Cloud Workflow execution script
# Usage: ./request-simulation-model-versions.sh -b <bucket_name> -us <us_version> -uk <uk_version> [-t timeout] [-i interval]
usage() {
echo "Usage: $0 -b <bucket_name> -us <us_version> -uk <uk_version> [-t timeout] [-i interval]"
echo ""
echo "Required flags:"
echo " -b bucket_name - GCS bucket name"
echo " -us us_version - US package version"
echo " -uk uk_version - UK package version"
echo ""
echo "Optional flags:"
echo " -t timeout_seconds - Maximum wait time in seconds (default: 300)"
echo " -i check_interval - Check interval in seconds (default: 10)"
echo " -h help - Show this help message"
echo ""
echo "Example:"
echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4"
echo " $0 -b my-bucket -us v1.2.3 -uk v1.2.4 -t 600 -i 15"
exit 1
}
# Initialize variables
BUCKET_NAME=""
US_VERSION=""
UK_VERSION=""
TIMEOUT_SECONDS="300"
CHECK_INTERVAL="10"
# Parse command line arguments
while [ $# -gt 0 ]; do
case "$1" in
-b)
if [ -z "$2" ]; then
echo "Error: -b requires a bucket name"
exit 1
fi
BUCKET_NAME="$2"
shift 2
;;
-us)
if [ -z "$2" ]; then
echo "Error: -us requires a US version"
exit 1
fi
US_VERSION="$2"
shift 2
;;
-uk)
if [ -z "$2" ]; then
echo "Error: -uk requires a UK version"
exit 1
fi
UK_VERSION="$2"
shift 2
;;
-t)
if [ -z "$2" ]; then
echo "Error: -t requires a timeout value"
exit 1
fi
TIMEOUT_SECONDS="$2"
shift 2
;;
-i)
if [ -z "$2" ]; then
echo "Error: -i requires an interval value"
exit 1
fi
CHECK_INTERVAL="$2"
shift 2
;;
-h)
usage
;;
*)
echo "Error: Unknown option $1"
usage
;;
esac
done
# Validate required arguments
if [ -z "$BUCKET_NAME" ] || [ -z "$US_VERSION" ] || [ -z "$UK_VERSION" ]; then
echo "Error: Missing required arguments"
echo "bucket_name (-b), us_version (-us), and uk_version (-uk) are required"
usage
fi
# Validate numeric arguments
if ! [[ "$TIMEOUT_SECONDS" =~ ^[0-9]+$ ]] || ! [[ "$CHECK_INTERVAL" =~ ^[0-9]+$ ]]; then
echo "Error: timeout_seconds and check_interval must be positive integers"
exit 1
fi
# Configuration
PROJECT_ID="prod-api-v2-c4d5"
WORKFLOW_LOCATION="us-central1"
WORKFLOW_NAME="wait-for-country-packages"
echo "Starting workflow execution..."
echo "Project: $PROJECT_ID"
echo "Location: $WORKFLOW_LOCATION"
echo "Workflow: $WORKFLOW_NAME"
echo "Bucket: $BUCKET_NAME"
echo "US Version: $US_VERSION"
echo "UK Version: $UK_VERSION"
echo "Timeout: ${TIMEOUT_SECONDS}s"
echo "Check Interval: ${CHECK_INTERVAL}s"
# Build input JSON
INPUT_JSON=$(cat <<EOF
{
"bucket_name": "$BUCKET_NAME",
"us_country_package_version": "$US_VERSION",
"uk_country_package_version": "$UK_VERSION",
"timeout_seconds": $TIMEOUT_SECONDS,
"check_interval": $CHECK_INTERVAL
}
EOF
)
echo "Input: $INPUT_JSON"
# Execute workflow
echo "Executing workflow..."
EXECUTION_RESULT=$(gcloud workflows execute "$WORKFLOW_NAME" \
--project="$PROJECT_ID" \
--location="$WORKFLOW_LOCATION" \
--data="$INPUT_JSON" \
--format="json")
# Extract execution name
EXECUTION_NAME=$(echo "$EXECUTION_RESULT" | jq -r '.name')
if [ -z "$EXECUTION_NAME" ] || [ "$EXECUTION_NAME" = "null" ]; then
echo "Failed to start workflow execution"
echo "$EXECUTION_RESULT"
exit 1
fi
echo "Execution started: $EXECUTION_NAME"
# Monitor execution state
START_TIME=$(date +%s)
echo "Monitoring execution state..."
while true; do
# Get current execution state
EXECUTION_STATUS=$(gcloud workflows executions wait "$EXECUTION_NAME" \
--location="$WORKFLOW_LOCATION" \
--format="json")
STATE=$(echo "$EXECUTION_STATUS" | jq -r '.state')
echo "Current state: $STATE"
# Check if execution is complete
if [ "$STATE" = "SUCCEEDED" ]; then
echo "SUCCESS: Workflow completed successfully"
RESULT=$(echo "$EXECUTION_STATUS" | jq -r '.result // empty')
if [ -n "$RESULT" ] && [ "$RESULT" != "null" ]; then
echo "Result: $RESULT"
fi
exit 0
elif [ "$STATE" = "FAILED" ]; then
echo "FAILED: Workflow execution failed"
ERROR=$(echo "$EXECUTION_STATUS" | jq -r '.error // empty')
if [ -n "$ERROR" ] && [ "$ERROR" != "null" ]; then
echo "Error: $ERROR"
fi
exit 1
elif [ "$STATE" = "CANCELLED" ]; then
echo "CANCELLED: Workflow execution was cancelled"
exit 1
fi
# Check monitoring timeout
CURRENT_TIME=$(date +%s)
ELAPSED=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED -ge $((TIMEOUT_SECONDS + 60)) ]; then
echo "TIMEOUT: Monitoring timeout exceeded"
echo "Workflow may still be running. Check Google Cloud Console for status."
exit 1
fi
# Wait before next check
sleep "$CHECK_INTERVAL"
done