sportrum-deploy/upgrade.sh
Claude a34ea9a9bc
Document known gap: storeall doesn't wait for the app's own background threads
InitServlet.destroy() now holds incoming requests and waits (briefly) for
HttpController's request count to hit zero before calling
Registry.storeAll() - but that counter only tracks threads servicing HTTP
requests. A thread the webapp spun up itself (e.g. the order schedulers
InitServlet.init() starts) isn't tracked by it at all, and can still be
running - and still writing - while storeall takes its snapshot.

storeAll()'s synchronization still keeps this from corrupting the DB, and
stopwaitsecs=300 (sportrum-ansible) still keeps supervisord from killing
Tomcat mid-write - neither of those is undone by this. But a write from an
untracked background thread can still land after storeall's pass and
never get persisted. Properly closing it needs a real thread manager in
the webapp that can halt everything it started, not just hold new
requests. Noting it here, next to the code that currently doesn't do that,
so it isn't forgotten before that manager exists.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
2026-10-01 09:37:31 +00:00

114 lines
5.2 KiB
Bash

#!/bin/bash
# upgrade.sh <migrate|hotfix-code> [tag]
#
# Replaces the old "-f/-w/-s flags + optional action argument" interface
# with named modes - a flag combination was too easy to get subtly wrong
# (nothing stopped you from skipping the SQL backup on a deploy that
# actually needed a DB migration), whereas a named mode says up front what
# kind of change this is and only allows the combination that actually
# makes sense for it:
#
# migrate Full careful path, for a deploy that changes the DB
# schema or data. Takes a SQL+WAR+logs backup (both the
# short-lived tier AND an indefinitely-kept copy tagged
# with the required <tag>), installs the new
# sportrum.war/manager.zip, runs the DB migration via
# manager-exec.sh (its full transcript saved next to the
# tagged backup - see upgrade-cmds.sh), takes a matching
# post-migration snapshot, restarts Tomcat.
#
# hotfix-code For a Java/resource change with NO DB migration. Takes a
# WAR+logs backup (short-lived tier only - no SQL dump, no
# indefinite copy: there's no schema change to protect
# against that the short-lived tier's existing history
# doesn't already cover), installs the new sportrum.war,
# restarts Tomcat (still needed - class files may have
# changed and the JVM can't hot-swap those). Does NOT run
# manager-exec.sh at all: the caller (sportrum-deploy's
# build.xml, via check-schema-diff.sh) is expected to have
# already confirmed there's no pending schema change
# BEFORE ever invoking this, so skipping the DB step here
# isn't a guess made by this script.
#
# hotfix-templates (a zero-downtime template-only sync with no Tomcat
# stop/restart, no storeall, no backup at all) is intentionally NOT
# implemented yet - which files count as "templates" and how they get
# synced into the live, already-running webapp directory is still an open
# design question, not something to guess at here.
#
# Also dropped in this refactor, neither carried forward nor replaced:
# the old storeonly ("-s") mode and the "leave Tomcat stopped instead of
# restarting" action argument. Both were undocumented, flag-only
# behaviors nobody had asked about when this was redesigned around named
# modes - easy to reintroduce as an explicit mode later if actually
# needed, rather than silently keep flag combinations nothing here
# exercises or tests.
. ./upgrade-funs.sh
pid=$$
mode="${1:?Usage: upgrade.sh <migrate|hotfix-code> [tag]}"
tag="${2:-}"
case "$mode" in
migrate)
if [ -z "$tag" ]; then
echo "ERROR: migrate mode requires a tag (second argument) - it's how the indefinitely-kept before/after backup gets named. See sportrum-deploy's build.xml migrate(@host) target, which resolves one for you." >&2
exit 1
fi
if [[ ! "$tag" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "ERROR: tag '$tag' contains characters other than letters, digits, dots, dashes, and underscores - it becomes a directory name on the server." >&2
exit 1
fi
;;
hotfix-code)
;;
*)
echo "ERROR: unknown mode '$mode' - expected 'migrate' or 'hotfix-code'." >&2
exit 1
;;
esac
# KNOWN GAP (2026-10-01): storeall is not actually guaranteed to see a
# fully quiesced app at the moment it runs. InitServlet.destroy() calls
# HttpController.setRequestsOnHold(true) and waits (up to 2s) for
# HttpController.getRequestCount() to hit zero before calling
# icp.Registry.storeAll() - but that only tracks threads servicing
# incoming HTTP requests through HttpController. Any background thread the
# webapp itself started (e.g. the order schedulers InitServlet.init() spins
# up - see "Starting order schedulers" in InitServlet.java) is invisible to
# that counter and can still be running, and potentially still mutating
# state, while storeall takes its snapshot. storeAll()'s own
# synchronization keeps this from corrupting the DB, but a write from one
# of these threads can still land after storeall's pass and never get
# persisted before shutdown.
#
# Not fixed by anything in this script or by Tomcat's stopwaitsecs (see
# sportrum-ansible's supervisord.conf) - that only protects storeall
# itself from being killed mid-write, it does nothing about threads other
# than the app's own request-handling threads. Properly closing this needs
# a real thread manager in the webapp that can be told to halt (or at
# least pause/join) every thread it started, not just hold new requests,
# called from InitServlet.destroy() before storeAll() runs. Not
# implemented yet - noted here so it isn't forgotten.
running=$(./tomcat_ctl.sh status | grep "tomcat8 is stopped")
if [ "x$running" == "x" ]; then
printf "Call server command storeall.."; sleep 2
runcmd result storeall
if [ "x$result" == "xsuccess" ]
then
echo " done!"
else
echo " failed! exiting..."
exit 1
fi
echo "Stopping tomcat.."; sleep 2
./tomcat_ctl.sh stop
else
echo "Tomcat is not running!"
fi
runcmds=$(cat upgrade-cmds.sh)
sudo su - sportrum -c "mode='$mode'; tag='$tag'; ${runcmds}"
echo "Starting tomcat..."; sleep 2
./tomcat_ctl.sh start