-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-cron.sh
executable file
·279 lines (242 loc) · 7.58 KB
/
wp-cron.sh
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
# Script to run WordPress cron tasks via WP-CLI
# This script can be used in a cron job to ensure WordPress scheduled tasks are executed
# Function to display help
show_help() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -p, --path Path to WordPress installation (default: current directory)"
echo " -c, --cli WP-CLI command to use (default: 'wp')"
echo " -u, --url WordPress URL (optional, for multisite installations)"
echo " -q, --quiet Suppress output except for errors"
echo " -s, --sentry Send execution data to Sentry"
echo " -m, --monitor Sentry monitor slug (required if using Sentry)"
echo " -k, --key Sentry public key (required if using Sentry)"
echo " -e, --env Sentry environment (default: 'production')"
echo " -i, --instance Sentry instance URL (default: 'https://o0.ingest.sentry.io')"
echo " -o, --org-id Sentry organization/project ID (default: '0')"
echo " -t, --timeout Maximum execution time in seconds (default: 300)"
echo " -h, --help Display this help"
exit 0
}
# Function to log messages
log() {
local level="$1"
local message="$2"
if [ "$QUIET_MODE" != "true" ] || [ "$level" = "ERROR" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message"
fi
}
# Function to send check-in to Sentry
send_to_sentry() {
local status="$1"
local check_in_id="$2"
if [ "$USE_SENTRY" = "true" ]; then
if [ -z "$SENTRY_MONITOR" ] || [ -z "$SENTRY_KEY" ]; then
log "ERROR" "Sentry monitor slug and public key are required for Sentry integration."
return 1
fi
# Build Sentry URL
local sentry_url="${SENTRY_INSTANCE}/api/${SENTRY_ORG_ID}/cron/${SENTRY_MONITOR}/${SENTRY_KEY}/"
# Add parameters
local params="?status=${status}"
# Add environment if specified
if [ -n "$SENTRY_ENV" ]; then
params="${params}&environment=${SENTRY_ENV}"
fi
# Add check-in ID if provided
if [ -n "$check_in_id" ]; then
params="${params}&check_in_id=${check_in_id}"
fi
log "INFO" "Sending check-in to Sentry with status: ${status}"
log "INFO" "Sentry URL: ${sentry_url}${params}"
# Send the check-in to Sentry
if curl -s -o /dev/null -w "%{http_code}" "${sentry_url}${params}" | grep -q "20[0-9]"; then
log "INFO" "Check-in sent to Sentry successfully."
return 0
else
log "ERROR" "Failed to send check-in to Sentry."
return 1
fi
fi
return 0
}
# Default values
WP_PATH="."
WP_CLI="wp"
WP_URL=""
QUIET_MODE="false"
USE_SENTRY="false"
SENTRY_MONITOR=""
SENTRY_KEY=""
SENTRY_ENV="production"
SENTRY_INSTANCE="https://o0.ingest.sentry.io"
SENTRY_ORG_ID="0"
TIMEOUT=300
# Process arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-p|--path)
WP_PATH="$2"
shift 2
;;
-c|--cli)
WP_CLI="$2"
shift 2
;;
--cli=*)
WP_CLI="${1#*=}"
shift
;;
-u|--url)
WP_URL="$2"
shift 2
;;
--url=*)
WP_URL="${1#*=}"
shift
;;
-q|--quiet)
QUIET_MODE="true"
shift
;;
-s|--sentry)
USE_SENTRY="true"
shift
;;
-m|--monitor)
SENTRY_MONITOR="$2"
shift 2
;;
--monitor=*)
SENTRY_MONITOR="${1#*=}"
shift
;;
-k|--key)
SENTRY_KEY="$2"
shift 2
;;
--key=*)
SENTRY_KEY="${1#*=}"
shift
;;
-e|--env)
SENTRY_ENV="$2"
shift 2
;;
--env=*)
SENTRY_ENV="${1#*=}"
shift
;;
-i|--instance)
SENTRY_INSTANCE="$2"
shift 2
;;
--instance=*)
SENTRY_INSTANCE="${1#*=}"
shift
;;
-o|--org-id)
SENTRY_ORG_ID="$2"
shift 2
;;
--org-id=*)
SENTRY_ORG_ID="${1#*=}"
shift
;;
-t|--timeout)
TIMEOUT="$2"
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift
;;
-h|--help)
show_help
;;
*)
echo "Unknown option: $1"
show_help
;;
esac
done
# Validate WordPress path
if [ ! -d "$WP_PATH" ]; then
log "ERROR" "WordPress path '$WP_PATH' does not exist or is not a directory."
exit 1
fi
# Check if WP-CLI is available
if ! command -v $WP_CLI &> /dev/null && [[ ! "$WP_CLI" == *" "* ]]; then
log "ERROR" "WP-CLI command '$WP_CLI' not found. Please install WP-CLI or provide the correct command."
exit 1
fi
# Check Sentry parameters if Sentry is enabled
if [ "$USE_SENTRY" = "true" ]; then
if [ -z "$SENTRY_MONITOR" ] || [ -z "$SENTRY_KEY" ]; then
log "ERROR" "Sentry monitor slug and public key are required when using Sentry integration."
exit 1
fi
# Check if curl is available
if ! command -v curl &> /dev/null; then
log "ERROR" "curl command not found. It is required for Sentry integration."
exit 1
fi
# Remove trailing slash from Sentry instance URL if present
SENTRY_INSTANCE="${SENTRY_INSTANCE%/}"
log "INFO" "Using Sentry instance: $SENTRY_INSTANCE"
log "INFO" "Using Sentry organization/project ID: $SENTRY_ORG_ID"
fi
# Build the WP-CLI command
WP_COMMAND="$WP_CLI --path=$WP_PATH"
# Add URL if provided
if [ -n "$WP_URL" ]; then
WP_COMMAND="$WP_COMMAND --url=$WP_URL"
fi
log "INFO" "Starting WordPress cron execution..."
log "INFO" "Using WP-CLI command: $WP_COMMAND"
log "INFO" "WordPress path: $WP_PATH"
# Generate a check-in ID for Sentry
check_in_id=""
if [ "$USE_SENTRY" = "true" ]; then
check_in_id=$(uuidgen 2>/dev/null || cat /proc/sys/kernel/random/uuid 2>/dev/null || echo "manual-$(date +%s)")
log "INFO" "Generated check-in ID for Sentry: $check_in_id"
# Send in_progress status to Sentry
send_to_sentry "in_progress" "$check_in_id"
fi
# Record start time
start_time=$(date +%s)
# Run the cron command with timeout
if timeout $TIMEOUT $WP_COMMAND cron event run --due-now; then
# Record end time and calculate duration
end_time=$(date +%s)
duration=$((end_time - start_time))
log "INFO" "WordPress cron executed successfully in $duration seconds."
# Send success to Sentry
if [ "$USE_SENTRY" = "true" ]; then
send_to_sentry "ok" "$check_in_id"
fi
exit 0
else
# Get the exit code
exit_code=$?
# Record end time and calculate duration
end_time=$(date +%s)
duration=$((end_time - start_time))
# Check if it was a timeout
if [ $exit_code -eq 124 ]; then
log "ERROR" "WordPress cron execution timed out after $TIMEOUT seconds."
# Send timeout error to Sentry
if [ "$USE_SENTRY" = "true" ]; then
send_to_sentry "error" "$check_in_id"
fi
else
log "ERROR" "WordPress cron execution failed with exit code $exit_code after $duration seconds."
# Send error to Sentry
if [ "$USE_SENTRY" = "true" ]; then
send_to_sentry "error" "$check_in_id"
fi
fi
exit $exit_code
fi