Skip to content

Instantly share code, notes, and snippets.

@colin-kiegel
Last active March 18, 2016 23:30
Show Gist options
  • Save colin-kiegel/40432d62c70c2ddf7354 to your computer and use it in GitHub Desktop.
Save colin-kiegel/40432d62c70c2ddf7354 to your computer and use it in GitHub Desktop.
cargo-wait
#!/bin/bash
# This FIRST WRAPPER only calls cargo - but it has a different name, so we can search for its PID in the second wrapper
#
# This is only meant to be called by cargo-wait (=second wrapper)
/usr/local/bin/cargo $@
#!/bin/bash
# This SECOND WRAPPER script waits for existing runs of cargo-exec (=first wrapper) to finish
#
# This is meant to be called instead of cargo to avoid concurrency issues
function getpid() {
# get all process IDs, and trim whitespaces - i.e. result is equivalent to
# echo "$PID1 $PID2 .. $PIDXy"
ps -C $PROCESS -o pid= | tr -d "[:blank:]" | sed ':a;N;$!ba;s/\n/ /g'
}
PROCESS="cargo-exec"
PID=$(getpid)
if [ -n "$PID" ]; then
echo -n "Waiting for $PROCESS to finish (PID: $PID)"
while [ -n "$PID" ]; do
echo -n "."
sleep 1
PID=$(getpid)
done
echo "DONE"
fi
$PROCESS $@
@colin-kiegel
Copy link
Author

Setup (Linux)

Step 1

cd /usr/local/bin

sudo wget https://gist.githubusercontent.com/colin-kiegel/40432d62c70c2ddf7354/raw/cargo-wait -O cargo-wait
sudo wget https://gist.githubusercontent.com/colin-kiegel/40432d62c70c2ddf7354/raw/cargo-exec -O cargo-exec
sudo chmod +x cargo-wait
sudo chmod +x cargo-exec

You can now use cargo-wait for safe concurrent calls to cargo.

Step 2 (optional)

If you want to shadow the cargo binary (for third party tools like IDE-extensions) - I suggest to do this for each user individually:

First make sure ~/bin is in your PATH in ~/.profile, then:

ln -s /usr/local/bin/cargo-wait ~/bin/cargo
hash -r # refresh bash-cache

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