Tuesday 21 October 2014

Process monitoring with God in Rails

God: A Ruby framework for process management.

The core issue we face before using the explanation of the use of process managment tools/gems:

While I have implemented sidekiq to monitor the threads in our application for sending emails in certain time frames. The sidekiq stops if server reboots and the queued processes goes in a indefinite period of time without execution. So this needs the sidekiq should be restarted automatically when it stops.

God is a framework which can automate the process.

FEATURES
  • Config file is written in Ruby
  • Easily write your own custom conditions in Ruby
  • Supports both poll and event based conditions
  • Different poll conditions can have different intervals
  • Integrated notification system (write your own too!)
  • Easily control non-daemonizing scripts
Installation

Add gem 'god' in Gemfile .
Run $ bundle install

Or

Run $ [sudo] install gem god


Version

$ god –version

God Learning Curve

Let us start with basic examples, how to write codes to execute the processes using God.

Let us create a new file god_demo.rb in application folder.

Write the following code in the same file

loop do
puts 'Hello'
sleep 1
end

Now we'll write a god config file that tells god about our process. Place it in the same directory and call it god_demo .god:



God.watch do |w|
w.name = "god_demo"
w.start = "ruby /full/path/to/god_demo.rb"
w.keepalive
end

God configuration should start with a God.watch block. A watch in god represents a process that we want to watch and control. Each watch must have, at minimum, a unique name and a command that tells god how to start the process. The keepalive declaration denotes god to keep this process alive. If the process is not running when god starts, it will be started. If the process dies, it will be restarted.

To run god, we give it the configuration file we wrote with -c. To see what's going on, we can ask it to run foreground with -D:

There are two ways that god can monitor your process. The first and better way is with process events. Not every system supports it, but those that do will automatically use it. With events, god will know immediately when a process exits. For those systems without process event support, god will use a polling mechanism. The output you see throughout this section will show both ways.

This is the example in http://godrb.com/

# Events

I [2011-12-10 15:24:34] INFO: Loading simple.god
I [2011-12-10 15:24:34] INFO: Syslog enabled.
I [2011-12-10 15:24:34] INFO: Using pid file directory: /Users/tom/.god/pids
I [2011-12-10 15:24:34] INFO: Started on drbunix:///tmp/god.17165.sock
I [2011-12-10 15:24:34] INFO: simple move 'unmonitored' to 'init'
I [2011-12-10 15:24:34] INFO: simple moved 'unmonitored' to 'init'
I [2011-12-10 15:24:34] INFO: simple [trigger] process is not running (ProcessRunning)
I [2011-12-10 15:24:34] INFO: simple move 'init' to 'start'
I [2011-12-10 15:24:34] INFO: simple start: ruby /Users/tom/dev/mojombo/god/simple.rb
I [2011-12-10 15:24:34] INFO: simple moved 'init' to 'start'
I [2011-12-10 15:24:34] INFO: simple [trigger] process is running (ProcessRunning)
I [2011-12-10 15:24:34] INFO: simple move 'start' to 'up'
I [2011-12-10 15:24:34] INFO: simple registered 'proc_exit' event for pid 23298
I [2011-12-10 15:24:34] INFO: simple moved 'start' to 'up'

# Polls

I [2011-12-07 09:40:18] INFO: Loading simple.god
I [2011-12-07 09:40:18] INFO: Syslog enabled.
I [2011-12-07 09:40:18] INFO: Using pid file directory: /Users/tom/.god/pids
I [2011-12-07 09:40:18] INFO: Started on drbunix:///tmp/god.17165.sock
I [2011-12-07 09:40:18] INFO: simple move 'unmonitored' to 'up'
I [2011-12-07 09:40:18] INFO: simple moved 'unmonitored' to 'up'
I [2011-12-07 09:40:18] INFO: simple [trigger] process is not running (ProcessRunning)
I [2011-12-07 09:40:18] INFO: simple move 'up' to 'start'
I [2011-12-07 09:40:18] INFO: simple start: ruby /Users/tom/dev/mojombo/god/simple.rb
I [2011-12-07 09:40:19] INFO: simple moved 'up' to 'up'
I [2011-12-07 09:40:19] INFO: simple [ok] process is running (ProcessRunning)
I [2011-12-07 09:40:24] INFO: simple [ok] process is running (ProcessRunning)
I [2011-12-07 09:40:29] INFO: simple [ok] process is running (ProcessRunning)




Wednesday 8 October 2014

Custom routing for restricting sidekiq pages in rails.

After integrating sidekiq with rails . It provides the web access to manage the processes/jobs . User can view Scheduled jobs from "Scheduled" tab.

Here is the screenshot of the sidekiq page.  This will be more easier to understand with this page.


Here we can see the jobs are being listed in the table. These jobs are in queue to be scheduled.

These listed jobs have some individual checkbox to select each jobs. We can delete the jobs at any point. Also deschedule from the site . Below is the "Delete" button to delete the jobs .


If we will view it to pubic it will be miss-utilized.

What we can do?

We can restrict the url from routes. Below is the routes for sidekiq.

require 'sidekiq/web'

Myapp::Application.routes.draw do
     mount Sidekiq::Web, at: '/sidekiq'
end

This is the general route for sidekiq. All the routes are generated from the same route configurations.

If we will do  $rake routes   in our rails console, we will get the following routes
 sidekiq_web     /sidekiq           Sidekiq::Web

How to restrict?

Example 1-

We can use a lambda call for the specific user group or emails. For example :

authenticate :user, lambda { |u| u.is_super_admin? || u.email == 'your@email.com'} do
    mount Sidekiq::Web, at: '/sidekiq'

end

Here it is restricting through the user group as if the user is super admin then it will show to the user.
Also it can be opened to a specific user email.

Example 2

Through Restful Authentication

Checks a User model instance that responds to admin?

#lib/admin_constraint.rb
class AdminConstraint
    def matches?(request)
        return false unless request.session[:user_id]
        user = User.find request.session[:user_id]
        user && user.admin?
    end
end

#config/routes.rb
require "admin_constraint"
mount Sidekiq::Monitor::Engine => '/sidekiq', :constraints => AdminConstraint.new



Also we can follow all the examples explained in
 Sidekiq wiki : https://github.com/mperham/sidekiq/wiki/Monitoring

Thanks :)