Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @sfgeorge sfgeorge created this gist Apr 12, 2014.
    33 changes: 33 additions & 0 deletions adhearsion-jruby-startup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # Start Adhearsion in JRuby, while building a pidfile as soon as possible.
    def ahnctl_start
    shell_command = "#{exec_ahn_async} && #{wait} && #{save_pid}"
    %x{shell_command}
    end


    private

    # 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
    pid_file = '/path/to/adhearsion.pid'
    "ps -fp $(/sbin/pidof java) | grep 'ahn start' | awk '{print $2}' > #{pid_file}"
    end