Skip to content

Instantly share code, notes, and snippets.

@santiycr
Last active March 25, 2016 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santiycr/7b57300ebfd3b9fc508f to your computer and use it in GitHub Desktop.
Save santiycr/7b57300ebfd3b9fc508f to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Restart SauceConnect via Rolling Restart
# The port it uses for listening for Selenium Relay
# is chosen automatically an ranges between 4441 and 4449
#
# Tunnel identifiers are optional, but removing them
# from this example would include some tweaking
#
TUNNEL_IDENTIFIER='your-identifier'
for POTENTIAL_PORT in {1..9};
do
NEW_PORT="444${POTENTIAL_PORT}"
curl -q "http://localhost:${NEW_PORT}" &> /dev/null || break
done
function start_sauce_connect() {
echo "[ROLLING RESTART] Starting new SC with identifier ${TUNNEL_IDENTIFIER}"
echo "[ROLLING RESTART] on port ${NEW_PORT}"
/Users/sso/Downloads/sc-4.3.14-osx/bin/sc -l sauce_connect.log \
--max-logsize 524288000 \
--se-port ${NEW_PORT} \
--logfile /tmp/sc_log_${TUNNEL_IDENTIFIER}_${NEW_PORT}.log \
--pidfile /tmp/sc_log_${TUNNEL_IDENTIFIER}_${NEW_PORT}.pid \
--tunnel-identifier ${TUNNEL_IDENTIFIER} \
--no-remove-colliding-tunnels \
--wait-tunnel-shutdown &
}
function stop_sauce_connect() {
echo "[ROLLING RESTART] Attempting stop SC with identifier ${TUNNEL_IDENTIFIER}"
echo "[ROLLING RESTART] skipping new instance running on port ${NEW_PORT}"
OLD_SC_PROCESSES=$(ps aux \
| grep -E "sc .*${TUNNEL_IDENTIFIER}" \
| grep -Ev "(${NEW_PORT}|grep)" \
| awk '{ print $2 }' \
| tr '\n' ' ')
if [ "${OLD_SC_PROCESSES}" != "" ]; then
echo "[ROLLING RESTART] Signalling old SC processes: ${OLD_SC_PROCESSES}"
kill ${OLD_SC_PROCESSES}
fi
}
start_sauce_connect
echo "[ROLLING RESTART] Waiting for the new tunnel to start before initiating"
echo "[ROLLING RESTART] graceful shutdown of old tunnels"
sleep 20
stop_sauce_connect
wait # come back to the original SC Process
@santiycr
Copy link
Author

And here is a two-pane shell showing a rolling restart happening on the left side, while 3 different batches of tests all run successfully along the way on the right:
https://www.evernote.com/l/ACMSj5qte1FMIqi3s-N_DZzRstzmFtZ20JgB/image.png!

@lukeis
Copy link

lukeis commented Mar 25, 2016

I'm assuming an example usage would be to put in a crontab with calling this script, which would launch a new tunnel, killing the other one after the main one is started (assuming it only takes 20 seconds to start up? might need to adjust that).

There's another scenario that would need to be handled though, when saucelabs kills any open tunnels (which has been happening frequently) we'd want this to auto-start again (why we had a while() { sc } loop)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment