? Earlier 33 items total Later ?

On this page: 

Rails MySQL Session Sweeper

1) Download the launchd GUI, Lingon, from: http://lingon.sourceforge.net/

2) Add hourly job:

$RAILS_APP_DIR/script/runner 'ActiveRecord::Base.connection.delete("DELETE FROM sessions WHERE updated_at < now() - INTERVAL 1 HOUR")'


3) My launchd task plist file (yours will vary):





        Label
        us.boygeni.mysql-session-sweeper
        LowPriorityIO
        
        ProgramArguments
        
                /users/defeated/Sites/rails_apps/boygenius/script/runner
                'ActiveRecord::Base.connection.delete("DELETE FROM sessions WHERE updated_at < now() - INTERVAL 1 HOUR")'
        
        RunAtLoad
        
        StartInterval
        3600


Debugging RJS




Remember to add
to your page

Is this error in installing rails via rubygems important?

Horatio:~ Rahul$ sudo gem install rails --include-dependencies
Attempting local installation of 'rails'
Local gem file not found: rails*.gem
Attempting remote installation of 'rails'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed rails-1.0.0
Successfully installed rake-0.6.2
Successfully installed activesupport-1.2.5
Successfully installed activerecord-1.13.2
Successfully installed actionpack-1.11.2
Successfully installed actionmailer-1.1.5
Successfully installed actionwebservice-1.0.0
Installing RDoc documentation for rake-0.6.2...

lib/rake.rb:733:18: unexpected token: '#'
Installing RDoc documentation for activesupport-1.2.5...
Installing RDoc documentation for activerecord-1.13.2...
Installing RDoc documentation for actionpack-1.11.2...

lib/action_controller/code_generation.rb:77:31: unexpected token: '#'

lib/action_controller/code_generation.rb:168:31: unexpected token: '#'
ERROR:  While executing gem ... (NoMethodError)
    undefined method `find_module_named' for nil:NilClass

Updated lighttpd control script to kill FCGIs

#!/bin/sh

LIGHTTPD_CONF=/home/USERNAME/etc/lighttpd.conf
PIDFILE=/home/USERNAME/var/run/lighttpd.pid

case "$1" in
    start)
      # Starts the lighttpd deamon
      echo "Starting Lighttpd"
      /usr/local/sbin/lighttpd -f $LIGHTTPD_CONF
    
  ;;
    stop)
      # stops the daemon bt cat'ing the pidfile
      echo "Stopping Lighttpd"
      kill -9 `cat $PIDFILE`
      # kills the dispatch.fcgis
      ps axww | grep dispatch | egrep -v 'grep' | awk '{ print $1 }' | xargs kill -9 
  ;;
    restart)
        ## Stop the service regardless of whether it was
        ## running or not, start it again.
        echo "Restarting Lighttpd"
        $0 stop
        $0 start
  ;;
    reload)
      # reloads the config file by sending HUP
      echo "Reloading config"
      kill -HUP `cat $PIDFILE`
  ;;
    *)
            echo "Usage: lighttpdctl (start|stop|restart|reload)"
            exit 1
        ;;
esac

Johan Sörensen's lighttpd control script

#!/bin/sh

LIGHTTPD_CONF=/home/jarkko/domains/piforienteering.fi/sites/piffen-new/config/lighttpd.conf
PIDFILE=/home/jarkko/var/run/lighttpd.pid

case "$1" in
    start)
      # Starts the lighttpd deamon
      echo "Starting Lighttpd"
      lighttpd -f $LIGHTTPD_CONF
    
  ;;
    stop)
      # stops the daemon bt cat'ing the pidfile
      echo "Stopping Lighttpd"
      kill `cat $PIDFILE`
  ;;
    restart)
        ## Stop the service regardless of whether it was
        ## running or not, start it again.
        echo "Restarting Lighttpd"
        $0 stop
        $0 start
  ;;
    reload)
      # reloads the config file by sending HUP
      echo "Reloading config"
      kill -HUP `cat $PIDFILE`
  ;;
    *)
            echo "Usage: lighttpdctrl (start|stop|restart|reload)"
            exit 1
        ;;
esac

Daedalus Config to Keep Lighttpd Up Nicely

This daedalus config as well as my previous snippet is meant to supplement chapter 5 of "Lighttpd the painless way".

This config is dependent on having a ~/lighttpd directory setup as described in that manual. Also, you must have the restart.rb script (again from my previous snippet) in the lighttpd folder.

As before, remember to swap out your USERNAME, DOMAIN, and PORT.

The first check here is that a lighttpd process is running. If not then it calls restart.rb which will kill any zombie dispatchers and restart lighttpd.

The second check is more of a departure from the original suggestion. Rather than checking that a specific file is generating the expected output, I go right to the source and verify that lighttpd is responding with an HTTP header on the port you were assigned. This is a pure indication of whether lighttpd is responsive on the correct port. It won't be fouled up by application errors or apache forwarding misconfiguration. More importantly, it won't generate a flood of restarts due to an unrelated code error.

Also, you will notice I set the checkinterval to 10 minutes. I felt this was a good compromise between keeping lighttpd up and saturating the server with unnecessary processing.

name: lighttpd
checkcommand: /bin/ps axww
checkregex: /lighttpd/
onfailcommand: /home/USERNAME/lighttpd/restart.rb
checkinterval: 600 
aftercommandwait: 120

name: lighttpd-external
checkcommand: /usr/local/bin/curl -I http://YOURDOMAIN.com:YOURPORT
checkregex: /^HTTP/s
onfailcommand: /usr/bin/killall -9 lighttpd; /home/USERNAME/lighttpd/restart.rb
checkinterval: 600 
aftercommandwait: 120

Killing rogue dispatch.fcgi processes and starting lighttpd on TextDrive

By using this script to start lighttpd you can be sure you won't be leaving rogues dispatchers around sucking up memory and generally pissing jason off. Don't get caught with your pants down!

Make sure to change USERNAME below, as well as any paths that may be different in your case.

#!/usr/local/bin/ruby
#Modified from Julik's original code posted to TextDrive forums.

pstab = `ps axww`

def kill_fcgi_proc(line)
  its = line.strip.split(/\s+/)
  pid = its[0]
  puts "KILLING #{line}"
  `kill -9 #{pid}`
  sleep(3)
end

if pstab =~ /\/usr\/local\/sbin\/lighttpd -f/
  
  puts "Lighttpd still running."

else

  pstab.scan(/^.*dispatch\.fcgi\s*$/) do |line|
    kill_fcgi_proc line
  end

  pstab = `ps axww`
  
  if pstab =~ /dispatch\.fcgi/
    puts "Error, rogue dispatch.fcgi's still pissing jason off."
  else
    puts "Rogue dispatch.fcgi's cleared, starting lighty!"
    `/usr/local/sbin/lighttpd -f /home/USERNAME/lighttpd/lighttpd.conf`
  end
  
end

emerge rails -va

ebuilds for rails

[ebuild N ] dev-lang/ruby-config
[ebuild N ] dev-lang/ruby-1.8.3 -cjk -doc -socks5 -tcltk -threads 0 kB
[ebuild N ] dev-ruby/rubygems-0.8.10 143 kB
[ebuild N ] dev-ruby/activesupport-1.1.1-r1 26 kB
[ebuild N ] dev-ruby/actionpack-1.9.1 248 kB
[ebuild N ] dev-ruby/actionmailer-1.0.1 72 kB
[ebuild N ] dev-ruby/rake-0.5.3 106 kB
[ebuild N ] dev-ruby/activerecord-1.11.1 266 kB
[ebuild N ] dev-ruby/mysql-ruby-2.5 18 kB
[ebuild N ] dev-ruby/actionwebservice-0.8.1 60 kB
[ebuild N ] dev-ruby/rails-0.13.1 -fastcgi +mysql -postgres -sqlite 84 kB

Add a New Rails App (Textdrive & Lighttpd)

Based on: http://forum.textdrive.com/viewtopic.php?id=5094

Get Lighttpd working on Textdrive first (http://manuals.textdrive.com/read/book/9), then to add new Rails apps (ex: typo.domain.tld) follow the steps below...

1. ssh into your account
ssh [email protected]


2. Create a new directory for your rails apps (if you don't have one already):
mkdir ~/apps/ (or whatever you want to name it)


3. Move into that directory:
cd ~/apps/


4. svn checkout the app you want:
Example:
svn checkout svn://leetsoft.com/typo/trunk typo


5. Move into that new app's directory:
Example:
cd typo


6. Change the first line of appname/public/dispatch.rb and appname/public/dispatch.fcgi to:
#!/usr/local/bin/ruby18


7. Set-up MySQL database:

a.
mysql -u username -p

b.
create database textdriveusername_appname;

c.
exit;

d.
mysql -u username -p textdriveusername_appname < db/schema.mysql.sql


8. Create a symbolic link to your app's public folder, inside of your public_html folder:
ln -s /home/username/apps/appname/public /home/username/public_html/appname


9. Add an $HTTP["host"] entry into your lighttpd.conf for the new app (ex: http://textsnippets.com/posts/show/6 and http://textsnippets.com/posts/show/187)

10. Kill all your dispatch.fcgi processes and your lighttpd process (ex: ttp://textsnippets.com/posts/show/206)

11. Restart lighttpd:
/usr/local/sbin/lighttpd -f /home/username/lighttpd/lighttpd.conf


12. Navigate to http://appname.yourdomain.tld


suppressing NoMethodError when receiver is nil, within a block

Often in rails you'll do things like

<%= @post.author.name %>


but it may be that you have no post, or the post has no author, and even if that's ok, the above would raise. I came up with a solution that lets you do the following:

<%= ifnil("no author"){ @post.author.name } %>


If at any point the receiver is nil (either @post or #author), execution of the block is stopped and the default value is returned ("no author" in this case, but defaults to nil)

NoMethodError is still raised if you send an invalid message to a non-nil receiver.

Here's the code for ifnil

def ifnil(value=nil)
  yield
  rescue NoMethodError
  raise unless $!.message =~ /:NilClass$/
  value
end

? Earlier 33 items total Later ?