Skip to content

Commit 1242b01

Browse files
authored
Update 02-cron-often.txt
1 parent 310a8b1 commit 1242b01

1 file changed

Lines changed: 53 additions & 8 deletions

File tree

crons/02-cron-often.txt

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
## D. Source SS-Config + SS-Functions (After Validated)
2929
## E. Run SS-Check + SS-Worker In Case Overdue
3030
## F. Touch Timestamp File
31-
## G. Run WP-Cron (Conditional)
32-
## H. Run Custom Tasks (EDIT SOURCED FILES)
33-
## I. Run Scheduled Tasks (DO NOT EDIT)
34-
## J. Delete Lock File
31+
## G. Check + Restart MySQL
32+
## H. Run WP-Cron (Conditional)
33+
## I. Run Custom Tasks (EDIT SOURCED FILES)
34+
## J. Run Scheduled Tasks (DO NOT EDIT)
35+
## K. Delete Lock File
3536

3637
####################################################################################################
3738
#### A. 02-Cron-Often: Validate (Restore) SS-Config ################################################
@@ -251,7 +252,51 @@ fi
251252
ss_touch "${TIMESTAMP_02_CRON_OFTEN}"
252253

253254
####################################################################################################
254-
#### G. 02-Cron-Often: Run WP-Cron (Conditional) ###################################################
255+
#### G. 02-Cron-Often: Check + Restart MySQL #######################################################
256+
####################################################################################################
257+
258+
## below we use a cheap local socket ping to detect when MySQL becomes unresponsive ##
259+
## restart only after 3 failures to avoid reacting to brief traffic spikes ##
260+
261+
if [[ "${DB_REMOTE}" != "true" ]]; then
262+
263+
MYSQL_WATCHDOG_COUNT_FILE="/var/www/meta/mysql-watchdog.count"
264+
265+
## read previous failure count and reset bad values ##
266+
MYSQL_WATCHDOG_COUNT="$(cat "${MYSQL_WATCHDOG_COUNT_FILE}" 2> /dev/null)"
267+
if [[ ! "${MYSQL_WATCHDOG_COUNT}" =~ ^[0-9]+$ ]]; then
268+
MYSQL_WATCHDOG_COUNT="0"
269+
fi
270+
271+
## continue only if local mysql service exists ##
272+
if systemctl cat mysql.service > /dev/null 2>&1; then
273+
274+
## use root socket auth because this is a local service health check ##
275+
if timeout 5 mysqladmin --user=root --host=localhost --protocol=socket ping --silent > /dev/null 2>&1; then
276+
277+
## clear failure count after successful ping ##
278+
ss_rm "${MYSQL_WATCHDOG_COUNT_FILE}"
279+
280+
else
281+
282+
## increment failure count after failed or timed out ping ##
283+
MYSQL_WATCHDOG_COUNT=$((MYSQL_WATCHDOG_COUNT + 1))
284+
printf '%s\n' "${MYSQL_WATCHDOG_COUNT}" > "${MYSQL_WATCHDOG_COUNT_FILE}"
285+
286+
## restart mysql only after 3 consecutive failed checks ##
287+
if [[ "${MYSQL_WATCHDOG_COUNT}" -ge 3 ]]; then
288+
source "${PATH_SS_RESTART_MYSQL}"
289+
ss_rm "${MYSQL_WATCHDOG_COUNT_FILE}"
290+
fi
291+
292+
fi
293+
294+
fi
295+
296+
fi
297+
298+
####################################################################################################
299+
#### H. 02-Cron-Often: Run WP-Cron (Conditional) ###################################################
255300
####################################################################################################
256301

257302
## this snippet will run WP-Cron via the server if changed in your ss-config settings ##
@@ -282,7 +327,7 @@ if [[ "${WP_CRON_INTERVAL}" == "often" ]] || [[ "${WP_CRON_INTERVAL}" != @(minut
282327
fi
283328

284329
####################################################################################################
285-
#### H. 02-Cron-Often: Run Custom Tasks (EDIT SOURCED FILES) #######################################
330+
#### I. 02-Cron-Often: Run Custom Tasks (EDIT SOURCED FILES) #######################################
286331
####################################################################################################
287332

288333
## this will run custom shell commands that you can save in a reserved filename below ##
@@ -291,7 +336,7 @@ fi
291336
source "${PATH_02_CRON_OFTEN_CUSTOM}"
292337

293338
####################################################################################################
294-
#### I. 02-Cron-Often: Run Scheduled Tasks (DO NOT EDIT) ###########################################
339+
#### J. 02-Cron-Often: Run Scheduled Tasks (DO NOT EDIT) ###########################################
295340
####################################################################################################
296341

297342
## the below tasks will be called if configured to run at this interval in ss-config ##
@@ -301,7 +346,7 @@ source "${PATH_02_CRON_OFTEN_CUSTOM}"
301346
source "${PATH_SS_CHECK}"
302347

303348
####################################################################################################
304-
#### J. 02-Cron-Often: Delete Lock File ############################################################
349+
#### K. 02-Cron-Often: Delete Lock File ############################################################
305350
####################################################################################################
306351

307352
## here we delete the lock file associated with this cron job to clear the cron queue ##

0 commit comments

Comments
 (0)