Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Mongrel and Apache fun with Capistrano 2.0

I got the Mongrel recipes from somewhere else -- I sadly don't remember where -- and modified them a bit.

namespace :deploy do
  namespace :mongrel do
    [ :stop, :start, :restart ].each do |t|
      desc "#{t.to_s.capitalize} the mongrel appserver"
      task t, :roles => :app do
        run "mongrel_rails cluster::#{t.to_s} --clean -C #{mongrel_conf}"
      end
    end
  end
  
  namespace :apache do
    desc "Start Apache"
    task :start, :roles => :web do
      sudo "/etc/init.d/httpd start > /dev/null"
    end

    desc "Stop Apache"
    task :stop, :roles => :web do
      sudo "/etc/init.d/httpd stop > /dev/null"
    end

    desc "Restart Apache"
    task :restart, :roles => :web do
      sudo "/etc/init.d/httpd restart > /dev/null"
    end
  end

  desc "Custom restart task for mongrel cluster"
  task :restart do
    deploy.mongrel.restart
    deploy.apache.restart
  end

  desc "Custom start task for mongrel cluster"
  task :start, :roles => :app do
    deploy.mongrel.start
    deploy.apache.start
  end

  desc "Custom stop task for mongrel cluster"
  task :stop, :roles => :app do
    deploy.apache.stop
    deploy.mongrel.stop
  end

end

Mongrel Startup Script

I couldn't find one that did exactly what I wanted, so I cooked up this ruby script. Be sure to change app_dir and apps near the top to match your environment. On my ubuntu/debian server, this script resides in /etc/init.d. I ran "sudo update-rc.d -f mongrel defaults" to make it run on startup.

#!/usr/bin/env ruby
#
# mongrel       Startup script for Mongrel by Tim Morgan
#
# chkconfig: - 85 15
# description: mongrel manages Mongrel
#

app_dir = '/var/rails'

apps = {
  'hpy' => 8001,
  '43verses' => 8002
}

if ['stop', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Stopping #{path}..."
    `mongrel_rails stop -c #{path} -P log/mongrel.pid`
  end
end

if ['start', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Starting #{path} on #{port}..."
    `mongrel_rails start -d -p #{port} -e production -c #{path} -P log/mongrel.pid`
  end
end

unless ['start', 'stop', 'restart'].include? ARGV.first
    puts "Usage: mongrel {start|stop|restart}"
    exit
end

Exempt directories from Apache's ProxyPass directive

Very useful if forwarding to Mongrel, etc.

        ProxyPass /stylesheets !
        ProxyPass /javascripts !
        ProxyPass /images !     
        ProxyPass / http://127.0.0.1:3010/
        ProxyPassReverse / http://127.0.0.1:3010/  

Mongrel Spinner and Restart tasks for Capistrano

These tasks should work with the default deploy.rb file. I assume you've at least read the Capistrano manual at rubyonrails.com, also, you should get your app running on mongrel first with manual booting and restarting before you try to automate.

h2. Spinner

desc <<-DESC
Spinner is run by the default cold_deploy task. Instead of using script/spinner, we're just gonna rely on Mongrel to keep itself up.
DESC
task :spinner, :roles => :app do
  application_port = xxxx #get this from your friendly sysadmin
  run "mongrel_rails start -e production -p #{application_port} -d -c #{current_path}"
end


h2. Restart

desc "Restart the web server"
task :restart, :roles => :app do
  begin
    run "cd #{current_path} && mongrel_rails restart"
  rescue RuntimeError => e
    puts e
    puts "Probably not a big deal, so I'll just keep trucking..."
  end
end


There's a more complete article on my blog.

Mongrel on Win32 as Service

Mongrel now has support for running as a Win32 service right out of the box. The support is still rough but works well enough that we decided to release it. You can thank Luis Lavena for working on this and making it so nice.

After you do the gem install, find a Rails application you want to run and do:
$ mongrel_rails_service install -n myapp -r c:\my\path\to\myapp -p 4000 -e production
$ mongrel_rails_service start -n myapp

Now hit the port and poof, works. Stopping the app is just done with:
$ mongrel_rails_service stop -n myapp

And, you can even set the CPU processor affinity for the service when yourun the install command. Can’t even do that on POSIX yet. Now that’s hot.

If you run into an app that’s not running right, my suggestion is to run it with the regular mongrel_rails runner:
$ cd c:\my\path\to\myapp
$ mongrel_rails start -p 4500

Since that will spit out error messages and stuff to the console. Use CTRL-Pause/Break to stop.
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS