Created
September 19, 2016 06:58
-
-
Save onnimonni/8f3738fd5902eec169550df0847591d9 to your computer and use it in GitHub Desktop.
Helper bash script which you can use to run wp all import synchronously. This also outputs problems which you may have with wpai.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
# Script to run WP All Import scripts sychronously with cron | |
## | |
# This is default wp core directory for us | |
ABSPATH="/var/www/project/wp" | |
function usage() | |
{ | |
echo "wpai-run-import" | |
echo "- scipt to run WP All Import triggers synchronously" | |
echo "" | |
echo "Parameters:" | |
echo "WP All Import key:" | |
echo "--import_key=xxxxyyy" | |
echo "" | |
echo "WP All Import jobs:" | |
echo "--import_ids=1,2,3,4" | |
echo "" | |
echo "WP Core path:" | |
echo "--path=/path/to/wp/core" | |
echo "" | |
echo "Example:" | |
echo "$0 --path=/data/code/web/wp/ --import_key=xxxxx --import_ids=16,14" | |
echo "" | |
} | |
## | |
# Parse all flags | |
## | |
while [ "$1" != "" ]; do | |
PARAM=`echo $1 | awk -F= '{print $1}'` | |
VALUE=`echo $1 | awk -F= '{print $2}'` | |
case $PARAM in | |
-h | --help) | |
usage | |
exit | |
;; | |
--path) | |
ABSPATH=$VALUE | |
;; | |
--import_key) | |
IMPORT_KEY=$VALUE | |
;; | |
--import_ids) | |
IMPORT_IDS=$VALUE | |
;; | |
*) | |
echo "ERROR: unknown parameter \"$PARAM\"" | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
## | |
# Run all imports | |
## | |
for import_id in $(echo $IMPORT_IDS | sed "s/,/ /g") | |
do | |
echo "INFO: Running import with id: $import_id" | |
# Start import | |
echo -n $(date +"%d/%m/%y %T > "); | |
php -e -r "parse_str('import_key=$IMPORT_KEY&import_id=$import_id&action=trigger', \$_GET); include '$ABSPATH/wp-cron.php';" | |
# Wait until importer is ready | |
errors="0" | |
wait_seconds="20" | |
while [ $errors -lt 5 ]; do | |
# Get importer status | |
importer_result=$(php -e -r "parse_str('import_key=$IMPORT_KEY&import_id=$import_id&action=processing', \$_GET); include '$ABSPATH/wp-cron.php';") | |
# Output time for better logs | |
echo "" | |
echo -n $(date +"%d/%m/%y %T > "); | |
if [ "$importer_result" == "" ]; then | |
errors=$[$errors+1] | |
elif echo $importer_result | grep '"status":200' | grep 'Import' | grep 'complete'; then | |
break | |
elif echo $importer_result | grep '"status":403' | grep 'is not triggered'; then | |
# 403 could mean "already processing." | |
errors=$[$errors+2] # Stop sooner if we get these messages twice | |
sleep $wait_seconds | |
elif echo $importer_result | grep '"status":500'; then | |
errors=$[$errors+1] | |
sleep $wait_seconds | |
else | |
echo $importer_result | |
sleep $wait_seconds | |
fi | |
# Increase waiting time with each loop so that we can create smaller logs | |
wait_seconds=$[$wait_seconds+20] | |
done | |
if [ $errors -lt 5 ]; then | |
echo "ERROR: Aborting import $import_id after 5 errors..." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment