Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active April 6, 2021 17:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxivak/af267e94bbd251b031514f888af8f756 to your computer and use it in GitHub Desktop.
Save maxivak/af267e94bbd251b031514f888af8f756 to your computer and use it in GitHub Desktop.
God in Rails app

God in Rails app

Install god

Install god with RVM

# use your version of Ruby
rvm use 2.2.4


#
gem install god

  • God startup script

create wrapper for god:

# startup script
rvm wrapper ruby-2.2.4 boot god

create a startup script for god, so that god runs automatically after system boots.

  • god main config file

file '/etc/init.d/god'

CONF_FILE=/opt/god/master.conf
DAEMON=/home/myuser/.rvm/bin/boot_god # CHANGE it from - which boot_god
PIDFILE=/var/run/god.pid
LOGFILE=/var/log/god.log
SCRIPTNAME=/etc/init.d/god

#DEBUG_OPTIONS="--log-level debug"
DEBUG_OPTIONS=""


...

Find the whole file with the script in this gist.

create an empty log file:

touch /var/log/god.log

set execute permissions for the script:

sudo chmod +x /etc/init.d/god

make the script run automically

sudo chkconfig god on
or
sudo update-rc.d god defaults

config file for god: /opt/god/master.conf:

load "/opt/god/file_touched.rb"

# app1
load "/var/www/apps/app1/current/config/god/resque.god.rb"

# app2
load "/var/www/apps/app2/current/config/god/resque.god.rb"
  • start god

to start god:

sudo service god start

Start god service after you set up your applications.

to restart god:

sudo service god restart

Lifecycle

  • If you do not provide a stop command, God will attempt to stop your process by first sending a SIGTERM. It will then wait for ten seconds for the process to exit. If after this time it still has not exited, it will be sent a SIGKILL.

Stop

  w.stop_signal = 'QUIT'
  w.stop_timeout = 20.seconds

Basic example

Monitoring of daemons (background processes)

  • If the process you're watching runs as a daemon, you'll need to set the pid_file attribute.
   w.pid_file = File.join(RAILS_ROOT, "tmp/pids/mydaemon.pid")
   w.behavior(:clean_pid_file)

Restart if file touched

Restart on custom condition

  • create custom condition
# /opt/god/not_work.rb

module God
  module Conditions

# Condition Symbol :not_work
# Type: Poll

    class NotWork < PollCondition
      attr_accessor :opt1


      def initialize
        super
        self.opt1 = nil
        
      end

      def valid?
        valid = true
        #valid &= complain("Attribute 'opt1' must be specified", self) if self.opt1.nil?
        valid
      end

      def test
        # check
        if your_cond
          # need restart
          return true
        end    

        # no restart needed
        false
      end
    end
  end
end

  • edit god master.conf
# /opt/god/master.conf

load "/opt/god/file_touched.rb"
load "/opt/god/not_work.rb"

# my apps
load "/var/www/apps/app1/config/god/smth.development.rb"


  • god monitoring for Rails app
# /config/god/smth.development.rb

#
user = 'root'
group = 'root'


God.watch do |w|
  w.uid = user
  w.gid = group
  
  ...
  
  
 w.restart_if do |restart|
    # dns not work
    restart.condition(:not_work) do |c|
      c.interval = 10.seconds
      c.opt1 = 'opt1_value'
      
    end
  end
end  
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment