Setup ruby god for rails application || Daemons setup for rails application

 1) Install god on Ubuntu/Linux machine

    sudo apt update

    sudo apt install ruby-god

2) Create new rails application if you don't have existing

3) In your Gemfile add new gem

    gem 'god', '~> 0.13.7', require: false

4) Then run bundle install

5) create a folder named daemons inside your  lib folder

6) create a file name daemons.god inside daemons folder (this file name or folder name can be anything its totally up to you)

7) Then paste this on you daemons.god file

    ENV["RAILS_ENV"] = "development"

    RAILS_ENV  = ENV.fetch('RAILS_ENV', 'production')

    RAILS_ROOT = File.expand_path('../../..', __FILE__)

    require 'shellwords'

    # Create non-default log/daemons directory.

    require 'fileutils'

    FileUtils.mkdir_p "#{RAILS_ROOT}/log/daemons"

    def daemon(name, options = {})

      God.watch do |w|

        command        = "bundle exec ruby lib/daemons/#{options.fetch(:script)}"

        command       += ' ' + options[:arguments].join(' ') if options.key?(:arguments)

        filesafe_name  = name.gsub(/\W/, '_')


        w.name  = name

        w.start = command

        w.dir   = RAILS_ROOT

        w.env   = { 'RAILS_ENV' => RAILS_ENV, 'RAILS_ROOT' => RAILS_ROOT }

        # God, by default, doesn't wait before resuming normal monitoring operations.

        # So we need to adjust this variable so God will wait 10 seconds on start/restart operations.

        w.grace = 10.seconds

        # God will send SIGTERM to the process and wait 10 seconds.

        # If process has still not exited it will be killed by sending SIGKILL.

        w.stop_signal  = 'TERM'

        w.stop_timeout = 10.seconds


        # God will always keep process running unless it was manually terminated.

        w.keepalive


        # In production Docker environment logs go to /dev/stdout.

        if RAILS_ENV == 'production'

          # w.log_cmd = "#{RAILS_ROOT}/bin/logger #{name.shellescape}"

        else

            # In non-production environment logs go to files.

          w.log = "#{RAILS_ROOT}/log/daemons/#{filesafe_name}.log"

        end


        # Allow customizations.

        yield(w) if block_given?

      end

    end

    Dir.glob "#{File.dirname(__FILE__)}/**/*.rb" do |file|

      script = File.basename(file)

      next if %w[ amqp_daemon.rb ].include?(script)

      daemon File.basename(script, '.*'), script: script

    end


8) create a file named text.rb inside daemons folder and paste this

    require File.join(ENV.fetch('RAILS_ROOT'), 'config', 'environment')

    running = true

    Signal.trap(:TERM) { running = false }

    while running do

        # we can call our library/service methods from here

bot = Text::PutText.new

bot.texting

    end


9) Now create a folder called text inside library folder (this all we are doing for testing purpose you can take any name of file or folder)

10) create new file put_text.rb inside that folder and paste this

    module Text
class PutText
def texting
puts "\n\n\n god setup done \n\n\n"
end
end
    end

 we are calling this file from text.rb, located inside daemons folder.

11) Now start god/deamons god -c lib/daemons/daemons.god

12) Now you can check logs tail -f log/daemons/text.rb

13) for terminate god terminate

reference link: http://godrb.com/

Feel free to write in comment box if you have any query.

Comments

Popular posts from this blog

Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined