Skip to content

Commit

Permalink
Packaging: Cherry-pick changes to runners.sh from st2-packages (#6302)
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd authored Feb 22, 2025
2 parents d2d41b0 + 8c21b51 commit 49f61fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ Added
* Copy systemd files from st2-packages.git for future packaging via pants. #6303
Cherry-picked by @cognifloyd

* Cherry-pick changes to runners.sh from st2-packages git repo. #6302
Cherry-picked by @cognifloyd

3.8.1 - December 13, 2023
-------------------------
Fixed
Expand Down
71 changes: 36 additions & 35 deletions st2actions/bin/runners.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
#!/bin/sh

LSB_RELEASE=$(which lsb_release)
SYSTEMDCTL=/bin/systemctl
UPSTARTCTL=/sbin/initctl
SPAWNSVC=st2actionrunner
WORKERSVC=st2actionrunner-worker
# Default number of workers
WORKERS="${WORKERS:-10}"

# Set default number of workers
if [ -z "$WORKERS" ]; then
WORKERS=$(/usr/bin/nproc 2>/dev/null)
WORKERS="${WORKERS:-4}"
fi
# Choose init system to perform actions with a service.
choose_sysinit() {
local service="$1" svinit="unknown"
if [ -d /run/systemd/system ]; then
svinit=systemd
else
>&2 echo "Supported init systems: ONLY systemd"
exit 99
fi
echo $svinit
}

# Perform service action over the given number of workers.
spawn_workers() {
local action=$1 init= seq=
seq=$(bash -c "printf '%g\\n' {1..$WORKERS}")

# Choose init system and exit if it's not supported.
init=$(choose_sysinit st2actionrunner)
[ $? -gt 0 ] && exit $?

# 1. Choose init type
if [ -z "$sv" -a -x $SYSTEMDCTL ]; then
sv=systemd
svbin=$SYSTEMDCTL
elif [ -z "$sv" ] && ( /sbin/start 2>&1 | grep -q "missing job name" ); then
sv=upstart
svbin=$UPSTARTCTL
else
>&2 echo "Unknown platform, we support ONLY upstart and systemd!"
case $init in
systemd)
echo "$seq" | xargs -I{} /bin/systemctl $action \
st2actionrunner@{}
;;
esac
# return 1 in case if xargs failed any invoked commands.
retval=$?; [ $retval -ge 123 ] && return 1 || return $retval
}

# Perform service action on all actionrunners
if [ -z "$1" ]; then
echo >&2 "Usage: $0 action"
exit 99
fi

# 2. Spwan workers
action="$1"; shift;
rs=0
i=1
while [ $i -le $WORKERS ]; do
if [ $sv = systemd ]; then
$svbin $action $SPAWNSVC@$i
elif [ $sv = upstart ]; then
$svbin $action $WORKERSVC WORKERID=$i
fi
cmdrs=$?
[ $cmdrs -gt 0 ] && rs=$cmdrs
i=`expr $i + 1`
done

exit $rs
spawn_workers $1

0 comments on commit 49f61fc

Please sign in to comment.