Skip to content

Commit

Permalink
Make scheduler_cron.sh posix compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
edannenberg committed Oct 2, 2019
1 parent 13dd3ef commit b92537b
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions scheduler_cron.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/bin/bash
#!/bin/sh

# Generate an error if any variable doesn't exist
set -o nounset

die() { echo "$@" 1>&2; exit 1; }

delete_lock() {
LOCKDIR=$1
rm -rf "${LOCKDIR}"
if [ $? -ne 0 ]; then
echo "Could not remove lock dir '${LOCKDIR}'. (Check permissions...)"; >&2
exit 1;
fi
[ $? -ne 0 ] && die "Could not remove lock dir '${LOCKDIR}'. (Check permissions...)"
}

# @see http://wiki.bash-hackers.org/howto/mutex
Expand All @@ -18,7 +17,7 @@ acquire_lock () {
PIDFILE="${LOCKDIR}/PID"

#echo "Trying to acquire lock '${LOCKDIR}'."
if mkdir "${LOCKDIR}" &>/dev/null; then
if mkdir "${LOCKDIR}" >/dev/null 2>&1; then

#echo "Successfully created '${LOCKDIR}'. Lock acquired"

Expand Down Expand Up @@ -46,7 +45,7 @@ acquire_lock () {
delete_lock "${LOCKDIR}"
# now try acquire new lock recursively...
#echo "Now acquiring new lock"
acquire_lock $LOCKDIR;
acquire_lock "${LOCKDIR}";
return
fi
fi
Expand All @@ -60,12 +59,12 @@ acquire_lock () {
fi

# check is the other process is still alive
if ! kill -0 $OTHERPID &>/dev/null; then
if ! kill -0 "${OTHERPID}" >/dev/null 2>&1; then
# lock is stale, remove it and restart
#echo "removing stale lock of nonexistant PID ${OTHERPID}" >&2
delete_lock "${LOCKDIR}"
# now try acquire new lock recursively...
acquire_lock $LOCKDIR;
acquire_lock "${LOCKDIR}";
else
# lock is valid and OTHERPID is active - exit, we're locked!
#echo "Other process is alive. Still locked"
Expand All @@ -76,29 +75,35 @@ acquire_lock () {


# Location of the php binary
PHP_BIN=$(which php || true)
if [ -z "${PHP_BIN}" ]; then
echo "Could not find a binary for php" 1>&2
exit 1
fi
PHP_BIN=$(command -v php)
[ $? -ne 0 ] && die "Could not find a binary for php"

# Location of the md5sum binary
MD5SUM_BIN=$(which md5sum || true)
if [ -z "${MD5SUM_BIN}" ]; then
echo "Could not find a binary for md5sum" 1>&2
exit 1
fi
MD5SUM_BIN=$(command -v md5sum)
[ $? -ne 0 ] && die "Could not find a binary for md5sum"

# https://stackoverflow.com/a/12145443
self=$(
self=${0}
while [ -L "${self}" ]
do
cd "${self%/*}" || exit 1
self=$(readlink "${self}")
done
cd "${self%/*}" || exit 1
echo "$(pwd -P)/${self##*/}"
)

# Absolute path to Magento installation shell scripts
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/shell" && pwd)
if [[ -z "${DIR}" || ! -d "${DIR}" ]]; then
DIR=$(cd "$(dirname "${self}")/shell" && pwd)
if [ -z "${DIR}" ] || [ ! -d "${DIR}" ]; then
echo "Could not resolve base shell directory" 1>&2
exit 1
fi

# The scheduler.php script
SCHEDULER="scheduler.php"
if [[ ! -e "${DIR}/${SCHEDULER}" || ! -r "${DIR}/${SCHEDULER}" ]]; then
if [ ! -e "${DIR}/${SCHEDULER}" ] || [ ! -r "${DIR}/${SCHEDULER}" ]; then
echo "Could not find scheduler.php script" 1>&2
exit 1
fi
Expand Down Expand Up @@ -154,11 +159,11 @@ fi
# This is to prevent multiple processes for the same cron parameters (And the only reason we don't call PHP directly)

# Unique identifier for this cron job run
IDENTIFIER=$(echo -n "${DIR}|${MODE}|${INCLUDE_GROUPS}|${EXCLUDE_GROUPS}|${INCLUDE_JOBS}|${EXCLUDE_JOBS}" | "${MD5SUM_BIN}" - | cut -f1 -d' ')
IDENTIFIER=$(printf %s "${DIR}|${MODE}|${INCLUDE_GROUPS}|${EXCLUDE_GROUPS}|${INCLUDE_JOBS}|${EXCLUDE_JOBS}" | "${MD5SUM_BIN}" - | cut -f1 -d' ')
acquire_lock "/tmp/magento.aoe_scheduler.${IDENTIFIER}.lock";

# Needed because PHP resolves symlinks before setting __FILE__
cd "${DIR}"
cd "${DIR}" || die "Couldn't change to dir ${DIR}"

# Build the options
OPTIONS=""
Expand Down

0 comments on commit b92537b

Please sign in to comment.