Skip to content

Instantly share code, notes, and snippets.

@sfgeorge
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfgeorge/10618286 to your computer and use it in GitHub Desktop.
Save sfgeorge/10618286 to your computer and use it in GitHub Desktop.
godrb watch file for Adhearsion under JRuby

Adhearsion-JRuby monitored by god

A sample godrb watch for Adhearsion JRuby.

Caveats

  • Note that godrb does not currently work under JRuby. You'll need a ruby manager like rvm or rbenv. That way, you can run your god under CRuby and your adhearsion app under JRuby.
  • This script is not perfect. It occasionally produces empty or corrupt PID files. -_-
  • This script makes a few assumptions.. tweak as necessary if any of this is inaccurate:
    • It assumes you use rvm
    • It assumes you use CentOS 6
    • It assumes that you have Java installed at /opt/jdk1.7.0_51.
    • It assumes you use capistrano, with a directory structure like so:
/srv
└── phone
    └── apptastic
        ├── current         # Home of your app.  Gemfile, .ruby-version, etc. should live here.
        │   ├── Gemfile
        │   ├── Gemfile.lock
        │   ├── Procfile
        │   └── config
        │       └── god
        │           └── adhearsion.god
        └── shared
            ├── config
            │   └── god_environment.yml
            ├── log         # Where logs will be written
            └── pids        # Where adhearsion.pid will be written
# encoding: utf-8
# run with: rvmsudo god -c /srv/phone/myapp/current/config/god/adhearsion.god
# debug with: rvmsudo god quit && rvmsudo god -c /srv/phone/myapp/current/config/all.god -D
@deploy_path = '/srv/phone/myapp'
@shared_path = File.join @deploy_path, 'shared'
@app_path = File.join @deploy_path, 'current'
@log_path = File.join @shared_path, 'log'
@pid_path = File.join @shared_path, 'pids'
@pid_file = File.join @pid_path, 'adhearsion.pid'
@god_env = YAML.load_file("#{@shared_path}/config/god_environment.yml")
God.watch do |w|
w.name = "myapp-adhearsion"
w.group = "myapp"
w.env = @god_env['environment']
w.interval = 30.seconds
w.start_grace = 3.minutes
w.stop_timeout = 8.hours
w.dir = @app_path
w.log = "#{@log_path}/ahnctl_command.log"
w.start = ahnctl_start
w.pid_file = @pid_file
w.behavior :clean_pid_file
w.uid = "ahn"
w.gid = "ahn"
end
# Start Adhearsion in JRuby, while building a pidfile as soon as possible.
def ahnctl_start
"#{exec_ahn_async} && #{wait} && #{save_pid}"
end
# Run start in the background, since it is not daemonized.
# This also makes it easier for us to nab the PID.
def exec_ahn_async
"( #{exec_ahn} & )"
end
# Start Adhearsion
def exec_ahn
exec_ahn = 'rvm $(cat .ruby-version) exec bundle exec ahn start .'
# Add action-specific options to the invoked command
exec_ahn << ' --no-console'
end
# Wait a moment to ensure that Java has spawned.
def wait
'sleep 1'
end
# Save the process id / PID
def save_pid
"ps -fp $(/sbin/pidof java) | grep 'ahn start' | awk '{print $2}' > #{@pid_file}"
end
# Please Note: Your changes to these values will NOT be realized merely
# restarting Adhearsion.
#
# You must *first* restart god completely in order for these env variables to be
# refreshed...
# sudo su -
# sudo /sbin/service god restart
# rvmsudo god restart myapp-adhearsion
environment:
AHN_ENV: production
AHN_PLATFORM_ENVIRONMENT: production
AHN_PUNCHBLOCK_USERNAME: (username...)
AHN_PUNCHBLOCK_PASSWORD: (password...)
AHN_PUNCHBLOCK_HOST: (hostname...)
JRUBY_OPTS:
--server
-J-Xmn512m
-J-Xms2048m
-J-Xmx2048m
JAVA_HOME: /opt/jdk1.7.0_51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment