Ruby thumbnail generator

Posted by Bhushan Ahire | Posted in Rails, ruby | Posted on 18-03-2008

0

Ruby thumbnail generator is simple script which is ideal to use in your Ruby on Rails application to quickly generate thumbnails of any proportions. Just set width and height and get the image.

1. copy following code into /controllers/thumb_controller.rb

2. edit /config/routes.rb and add this line:
map.connect “thumb/*specs”, :controller => “thumb”, :action => “index”

3. create directory /imagelib/ in RoR’s /public/ directory and
/image_cache/ inside /imagelib/ directoory

Now you can call /thumb/photo.jpg?w=400&h=350 and you will
see resized picture “photo.jpg”. photo.jpg should be stored in
/public/imagelib/ directory. Off course you can change directory
structure if you wish just don’t forget to edit thumb_controller.rb
than.

visit www.cleverleap.com/ruby-thumbnail-generator/
for more information

class ThumbController < ApplicationController

  require 'gd2'
  include GD2

  def index

    path = "imagelib/"    # default image library directory

    widthx = 500          # default width of generated image
    heightx = 500         # default height of generated image

    if params[:w] then widthx = params[:w].to_i

    end

    if params[:h] then heightx = params[:h].to_i

    end

    filepath = path + params[:specs].join("/")    # Path to file

    format = filepath.split(".").last             # Format - extension

    filename = params[:specs].last.split(".").first # just file name without extension

    #require 'digest/md5'
    digest = Digest::MD5.hexdigest( filepath )      # md5 hash

    cachefile = digest + "-" + widthx.to_s + heightx.to_s + "." + format

    picfile = filepath
    cachedpicfile = path + "image_cache/" + cachefile

    if File.exists?(cachedpicfile) && (File.stat( cachedpicfile ).mtime.to_i > File.stat( picfile ).mtime.to_i)

      picsource = cachedpicfile
      cache = true
    elsif File.exists? picfile
      picsource = picfile

    end

    if cache == true    # Read from Cache
      @pic = File.new(picsource).read

    else                # Import an image
      i = Image.import(picsource)

      if i.size[0] > i.size[1]  # Horizontal proportion. width > height.

        if i.size[0] < widthx then width = i.size[0]     # preffer smaller image width

        else width = widthx
        end

        height = width * i.size[1] / i.size[0]

      else                      # Vertical proportions
        if i.size[1] < heightx then height = i.size[1]

        else height = heightx
        end

        width = i.size[0] /(i.size[1] / height)
      end

      i.resize! width, height

      if format == "gif" then @pic = i.gif

      elsif format == "png" then @pic = i.png

      else @pic = i.jpeg 80
      end

      i.export(path + 'image_cache/' + cachefile ) # export cache file

    end

    cgi = CGI.new
  	cgi.out("type"=>"image/jpeg") { @pic }

  	render :nothing => true

  end
end

god Server process monitoring and notifying tool written in ruby

Posted by Bhushan Ahire | Posted in Rails, ruby | Posted on 18-03-2008

0

Progress on god is moving along as quick as ever. Most interestingly you’ll find several useful new command line functions:

  • god status prints out the status of each Watch
  • god log shows realtime logs for a specific Watch (even if you don’t have god logging to file)
  • god load loads or reloads a config file into a running god instance
  • god terminate stops all Watches and then stops god (useful when testing your setup)

The logging system has been beefed up with proper timestamps and criticality levels. Log messages are more complete overall. You can also get the STDOUT/STDERR of a god-daemonized process written to a log file by specify ‘w.log = ‘ in your Watch config.

If you let god daemonize your process for you, there’s no need to provide a stop command. A default killing lambda will take care of gracefully (or not so gracefully if necessary) stopping your god-daemonized process.

The validity of your config file is checked better than previous versions to point you to the problem area of your config.

The bug that prevented group control from working has been fixed so you can now start/stop/etc groups of Watches.

Updated documentation is now available on the website:

http://god.rubyforge.org/

WHAT IS GOD?

God is an easy to configure, easy to extend monitoring framework written in Ruby.

Keeping your server processes and tasks running should be a simple part of your deployment process. God aims to be the simplest, most powerful monitoring application available.

INSTALL

sudo gem install god

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
  • Easily control non-daemonized processes

EXAMPLE

The easiest way to understand how god will make your life better is by looking at a sample config file. The following configuration file is to keep the mongrels running:

# file:      application_name.god
# run with:  god -c /path/to/application_name.god
# 
# This is the actual config file used to keep the mongrels of
# application.com running.
RAILS_ROOT = "/var/www/application_name/current"
%w{8001 8002 8003}.each do |port|
  God.watch do |w|
    w.name = "application_name-mongrel-#{port}"
    w.interval = 30.seconds # default
    w.start = "mongrel_rails cluster::start --only #{port} 
      -C #{RAILS_ROOT}/config/mongrel_cluster.yml"
    w.stop = "mongrel_rails cluster::stop --only #{port} 
      -C #{RAILS_ROOT}/config/mongrel_cluster.yml"
    w.grace = 10.seconds
    w.pid_file = File.join(RAILS_ROOT, "log/mongrel.#{port}.pid")    
    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

Configuration for nginx and mysql
I found a gr8 post for sample configuration here

DOCS

Detailed documentation is available at http://god.rubyforge.org/

Original Source…

Synchronize old svn with all revision history to new repository

Posted by Bhushan Ahire | Posted in Subversion | Posted on 04-03-2008

0

First, create a SVN user for svnsync to use – let’s call this the ’svnsync user’. The easiest (and best) way to do this is edit the svnserve.conf and passwd files:

conf/svnserve.conf


# Uncomment this line.
password-db = passwd

conf/passwd


svnsync = secret

This gives read and write access to the ’svnsync user’. The svnsync program will authenticate with our repositories as this user via the svn:// protocol (i.e. via svnserve).

Next, we need to create a pre-revprop-change hook for the destination repository. The svnsync documentation has a detailed explanation. Create a hooks/pre-revprop-change file under your destination repository’s directory.


#!/bin/sh
USER="$3"

if [ "$USER" = "svnsync" ]; then exit 0; fi

echo "Only the svnsync user can change revprops" >&2
exit 1

Make it executable, and then initialize the sync:


chmod +x hooks/pre-revprop-change
svnsync init file:///var/svn/repositories/destination_repos svn://source.host/source_repos

Don’t worry, this only sets up the sync – there’s no actual data copying yet. Syncing your repository data may take a long time if you have a big source repository, so I suggest using nohup to run the code overnight (or something), or at least saving the output in a log. Either way, the command to start the sync is:

svnsync sync --username svnsync file:///var/svn/repositories/testsync/

You should start seeing svnsync committing in changes from your source repository. Instant gratification (well, almost)! Should your svnsync process get aborted or killed, you can remove the hanging lock by running:

svn propdel svn:sync-lock --revprop -r 0

Setting up ‘on-the-fly’ syncing

So now you have your source and destination repositories synced, but what happens when you start committing changes to your source repository? Nothing! That’s because svnsync is merely a passive syncing tool (meaning you have to run it to sync, instead of it knowing when to sync automatically).

There are two ways you can setup ‘real-time’ syncing:

  1. Use cron (or a similar scheduler) on the destination repository server. Add something like this to your crontab:
    * * * * * /usr/local/bin/svnsync --non-interactive sync svn://source.host/source_repos

    This basically runs svnsync on your destination repository server every minute to pull down any changes to your source repository.

  2. Add a post-commit hook to the source repository. I found this svnsync entry by Paul Querna that has a sample post-commit hook. If I recall correctly I tried it but it didn’t work for me, so I settled on using cron to sync up my repositories.