Skip to content

Commit

Permalink
Add idempotent brew_launchctl_restart function
Browse files Browse the repository at this point in the history
Fixes thoughtbot#260

This is a soft error that occurs when Laptop is run and PostgreSQL isn't
currently loaded by `launchclt`, e.g. on the first run of Laptop:

    Starting Postgres ...
    /Users/wellmade/Library/LaunchAgents/homebrew.mxcl.postgresql.plist ->
    /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist
    launchctl: Error unloading: homebrew.mxcl.postgresql

The fix would probably be to check `launchctl list` to see if the service is
already loaded before calling `launchctl unload`.

This small added complexity probably justifies a function for running
appropriate `launchctl` commands. Homebrew has a strong convention for the
naming of `launchd` plists: "homebrew.mxcl.FORMULA_NAME.plist", e.g.
"homebrew.mxcl.postgresql.plist" and "homebrew.mxcl.redis.plist". This likely
makes a function to do the `launchctl` work fairly straightforward.
  • Loading branch information
gohanlon authored and Dan Croak committed Jul 25, 2014
1 parent 637f8d5 commit c9f1ab1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
21 changes: 15 additions & 6 deletions mac
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ brew_is_upgradable() {
brew_expand_alias() {
brew info "$1" 2>/dev/null | head -1 | awk '{gsub(/:/, ""); print $1}'
}

brew_launchctl_restart() {
local NAME=$(brew_expand_alias "$1")
local DOMAIN="homebrew.mxcl.$NAME"
local PLIST="$DOMAIN.plist"

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/$NAME/$PLIST ~/Library/LaunchAgents

if launchctl list | grep -q $DOMAIN; then
launchctl unload ~/Library/LaunchAgents/$PLIST >/dev/null
fi
launchctl load ~/Library/LaunchAgents/$PLIST >/dev/null
}
### end mac-components/mac-functions

if [[ -f /etc/zshenv ]]; then
Expand Down Expand Up @@ -137,13 +151,8 @@ fancy_echo "Installing NVM, Node.js, and NPM, for running apps and installing Ja
nvm alias default "$node_version"
### end mac-components/packages

fancy_echo "Ensure LaunchAgents exists ..."
mkdir -p ~/Library/LaunchAgents

fancy_echo "Starting Postgres ..."
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist >/dev/null
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist >/dev/null
brew_launchctl_restart postgresql
### end mac-components/start-services

fancy_echo "Installing rbenv, to change Ruby versions ..."
Expand Down
14 changes: 14 additions & 0 deletions mac-components/mac-functions
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ brew_is_upgradable() {
brew_expand_alias() {
brew info "$1" 2>/dev/null | head -1 | awk '{gsub(/:/, ""); print $1}'
}

brew_launchctl_restart() {
local NAME=$(brew_expand_alias "$1")
local DOMAIN="homebrew.mxcl.$NAME"
local PLIST="$DOMAIN.plist"

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/$NAME/$PLIST ~/Library/LaunchAgents

if launchctl list | grep -q $DOMAIN; then
launchctl unload ~/Library/LaunchAgents/$PLIST >/dev/null
fi
launchctl load ~/Library/LaunchAgents/$PLIST >/dev/null
}
7 changes: 1 addition & 6 deletions mac-components/start-services
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
fancy_echo "Ensure LaunchAgents exists ..."
mkdir -p ~/Library/LaunchAgents

fancy_echo "Starting Postgres ..."
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist >/dev/null
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist >/dev/null
brew_launchctl_restart postgresql

0 comments on commit c9f1ab1

Please sign in to comment.