Never been to TextSnippets 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 (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed 

start mongrel daemon

from within the rails project directory, execute this in the shell

mongrel_rails start -d -p 8020 -e production


-d daemonizes the process
-e tells Rails proj. to use production enviromnent
-p port to use, increment by 10 past next closest port

stop mongrel daemon

Stop a daemonized mongrel serving a rails project.

PID file is normally found in /log/

mongrel_rails stop -P <rel. location of PID file>

mongrel_cluster monit script

Copyright (C) 2006 Peter J Jones (pjones@pmade.com).

################################################################################
# This script will update your monit configuration file to monitor your
# mongrel cluster by reading the mongrel cluster file and generating monit
# check entries.  It places delimiters in the configuration file so that you
# can run this script multiple times without generating duplicate entries.
#
# Run it like this:
#
#  mongrel_monit --mongrel=/path/to/mongrel/config.yml --monit=/path/to/monitrc
#
# You can edit the ERB template below to control the generated monit config.
#


TEMPLATE = <<EOT
check process mongrel_<%= @port %> with pidfile <%= @pidfile %>
  group mongrel
  start program = "/usr/local/bin/<%= @start %>"
  stop program = "/usr/local/bin/<%= @stop %>"

  if failed host 127.0.0.1 port <%= @port %> protocol http
    with timeout 10 seconds
    then restart

  if totalmem > 100 Mb then restart
  if cpu is greater than 60% for 2 cycles then alert
  #if cpu > 90% for 5 cycles then restart
  #if loadavg(5min) greater than 10 for 8 cycles then restart
  if 3 restarts within 5 cycles then timeout

EOT
################################################################################
require 'erb'
require 'optparse'
require 'fileutils'
require 'rubygems'
require 'mongrel_cluster/init'
################################################################################
module Kernel
  $commands = []

  # Hack Kernel::` so that we can capture what mongrel_cluster does
  def ` (cmd)
    $commands << cmd
    system(':') # to set $?
  end

  # Hack Kernel::puts to shut mongrel_cluster up
  def puts (str)
    true
  end
end
################################################################################
options = OptionParser.new

options.on('--mongrel=FILE', 'Mongrel configuration file') do |o|
  $mongrel_config = o
end

options.on('--monit=FILE', 'Monit configuration file') do |o|
  $monit_config = o
end

options.parse!

unless $mongrel_config and $monit_config
  puts "please give --mongrel and --monit"
  exit 1
end
################################################################################
mongrel_config_data = YAML.load_file($mongrel_config)
Dir.chdir(mongrel_config_data['cwd']) if mongrel_config_data['cwd']
################################################################################
# This is the only way to access the cmd that mongrel_cluster uses so that we
# can keep things DRY
start = Cluster::Start.new
start.instance_variable_set(:@config_file, $mongrel_config)
start.run
start_commands = $commands.dup
$commands.clear

stop = Cluster::Stop.new
stop.instance_variable_set(:@config_file, $mongrel_config)
stop.run
stop_commands = $commands.dup
$commands.clear
################################################################################
template = ERB.new(TEMPLATE)
config = ''

0.upto(start_commands.length - 1) do |i|
  @port = start_commands[i].match(/-p\s*(\d+)/)[1]
  @pidfile = File.expand_path(start_commands[i].match(/-P\s*(\S+)/)[1])
  @start = start_commands[i]
  @stop = stop_commands[i]
  config << template.result(binding)
end
################################################################################
monit_config_data = File.open($monit_config) {|f| f.read}

# TODO: if you have more than one cluster, this won't do
banner_start = "#= Mongrel_Monit Config Start =#\n"
banner_stop  = "#= Mongrel_Monit Config End =#\n"

if monit_config_data.match(/#{banner_start}.*#{banner_stop}/m)
  monit_config_data.sub!(/#{banner_start}.*#{banner_stop}/m, "#{banner_start}#{config}#{banner_stop}")
else
  monit_config_data << "#{banner_start}#{config}#{banner_stop}"
end

File.open($monit_config, 'w') {|f| f << monit_config_data}
################################################################################

make mongrel cleanup stale pidfiles

users of monitoring systems may have noticed that mongrel can leave stale PID files behind, which prevents automated monitors from restarting mongrels that have been detected as dead via a port or process check, as mongrel's default behaviour is to exit upon the detection of a pre-existing PID file, whether there is in fact a process of that id running or not.

a fix, in /usr/local/lib/ruby/gems/1.8/gems/mongrel-x.x.x/bin/mongrel_rails:


if File.exist? defaults[:pid_file]
# mongrels that crash can leave stale PID files behind, and these
# should not stop mongrel from being restarted by monitors...
pid = File.new(defaults[:pid_file]).readline
unless `ps -ef | grep #{pid} | grep -v grep`.length > 0
# use "ps ax" for freebsd
log "!!! PID file #{defaults[:pid_file]} exists, but is stale, and will be deleted so that this mongrel can run."
File.delete(defaults[:pid_file])
else
log "!!! PID file #{defaults[:pid_file]} already exists and the process id referred to in it is running. This mongrel is probably already running. #{defaults[:log_file]} for errors. EXITING."
exit 1
end
end


there is a call to at_exit in mongrel, which should be cleaning these files up.. but is not.

Apache + mongrel_cluster VirtualHost snippet

Put it in yr httpd.conf, or better yet, an included file. (that thing is monstrous as is)

<VirtualHost *:80>
        ServerName test.com
        DirectoryIndex index.html dispatch.fcgi
        DocumentRoot /domains/test.com/web/public
        RewriteEngine On
        RewriteRule ^(.*/)?.svn/ - [F,L]
        ProxyPass /images !
        ProxyPass /stylesheets !
        ProxyPass /javascripts !
        ProxyPass /examples !
        ProxyPass /map_data !
        ProxyPass /engine_files !
        ProxyPass /javascript !
        ProxyPass /feregions !
        ProxyPass / balancer://test/
        ProxyPassReverse / balancer://test
        ProxyPreserveHost On
        ErrorLog /opt/csw/apache2/var/log/test.com.error
        CustomLog /opt/csw/apache2/var/log/test.com common

        <Proxy balancer://test>
                BalancerMember http://127.0.0.1:8000
                BalancerMember http://127.0.0.1:8001
                BalancerMember http://127.0.0.1:8002
                BalancerMember http://127.0.0.1:8003
                BalancerMember http://127.0.0.1:8004
        Proxy>
</VirtualHost>

SMF Manifest for Mongrels on TXD Containers

An SMF manifest that works well on the TXD containers. More here, and also see the related Capistrano config

<?xml version='1.0'?>
DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<service_bundle type='manifest' name='mongrel/example'>
  <service name='network/mongrel/example' type='service' version='0'>
    <create_default_instance enabled='true'/>
    <single_instance/>
    
    <dependency name='fs' grouping='require_all' restart_on='none' type='service'>
      <service_fmri value='svc:/system/filesystem/local'/>
    dependency>
    
      />
      
      'svc:/network/nfs/client'/>
    
    'mongrel_example' restart_on='none' grouping='optional_all'>
      'svc:/milestone/multi-user'/>
    
    
    'start' type='method' exec='/opt/csw/bin/mongrel_rails cluster::start' timeout_seconds='60'>
      '/home/example/rails/example.org/current'>
        'example' group='example' />
        
          
        
      
    

    'stop' type='method' exec=':kill' timeout_seconds='60'>
      
    

  

« Newer Snippets
Older Snippets »
6 total  XML / RSS feed