[Ruby] God monitor

Par défaut

Update: this script, from September 2007, is a bit out-dated, you can get more info on official God gem website.

Looking for a ruby programmer? Contact me

After many ways to monitor Feevy updaters, we’re finally using God gem today to manage everything with Ruby on many platforms.

Documentation is really clear, and it’s easy to adapt it to your own code. The nicest point was to be able to control our non-daemonized updaters, and monitor them all in the same tool.

Here is our script:

require 'rubygems'
require 'god'

ROOT = File.dirname(__FILE__)

God.init do |god|
  god.pid_file_directory = "#{ROOT}/pids"
end

[*0...20].each do |updater|
  God.watch do |w|
    # watch with no pid_file attribute set
    w.name = "updater_#{updater}"
    w.interval = 30.seconds # default
    w.start = "ruby #{ROOT}/updater.rb"
    w.grace = 10.seconds

    w.behavior(:clean_pid_file)

    w.start_if do |start|
      start.condition(:process_running) do |c|
        c.interval = 5.seconds
        c.running = false
      end
    end

    w.restart_if do |restart|
      restart.condition(:memory_usage) do |c|
        c.above = 150.megabytes
        c.times = [3, 5] # 3 out of 5 intervals
      end

      restart.condition(:cpu_usage) do |c|
        c.above = 50.percent
        c.times = 5
      end
    end
  end
end

[updated]: thanks to apeiros on irc://#ruby@irc.freenode.net for quick array initialization tips!

Rails 1.2: keep working with version 1.1.6

Par défaut

Two days ago, www.rubyonrails.com released the new version of rails, with lots of new cool stuff to play with.

And if your old projects are complaining after installing the new version, you just have to un-comment a simple line in your « config/environment.rb »:

  # Specifies gem version of Rails to use when vendor/rails is not present
  RAILS_GEM_VERSION = '1.1.6'

Using RJS templates

Par défaut

Ruby on Rails is a great platform to work with, there is so much niceties for geeks like me. Today has been a great day to work with RJS templates.
You must use RJS templates, just because when you don’t know where to put the view behavior after an action, and because filling a link_to_remote helper with too much parameters is really ugly.

Changing DOM className

Because my application needs to display some dynamic user selection of an avatar, a great way has been to change the CSS className of the selected avatar.
Here is how to do it with RJS:

page.select(".avatar").all() do |element, index|
  page.call 'Element.removeClassName', element, 'selected_avatar'
end

page.call 'Element.addClassName', @avatar, 'selected_avatar'