Taking Care of Rails Support Tasks Through Custom Daemons Tammer Saleh
Saleh is talking about using a Daemon to get rails to talk to LDAP. The code for the LDAP deamon and the code to make it look like a model live here. I will add a bit of code here and there, but hopefully the presenter will upload the slides so I don’t have to retype everything. There is some great code in this presentation.
Daemons are a bit of a dark art.
def daemonize
Kernel.fork and Kernel.exit
Process.setsid
Kernel.fork and Kernel.exit
# system maintenance items so you don't hold up directory maintenance and so you don't have to deal with permissions
File.umask 0
Dir.chdir '/'
# you don't want to hold onto
ObjectSpace.each_object(IO) {|io| io.close rescue nil}
STIN.open('/dev/null')
end
or just use a gem
require daemons
Daemons.daemonize
loop {
# or whatever
conn = accept
}
You can interact directly with your Rails environment.
include environment file
Threads
ThreadsBase.allow_concurrency = true
Or you will get a nasty error:
Mysql::Error: Lost connection to MySQL server during query:
Start and Stop
use a unix init script that takes start/stop/restart
OSX got ride of init script Solaris is going to do the same. Use Launchd instead. It uses xml.
Make sure the process doesn't start twice so that it doesn't run over itself.
Use a Pid File. This is how the daemon finds out its own id for stopping. It is also how you keep a process from starting up twice
Configuration Files
Talk to your process. Configure with yml files. Read it in with YAML.Load
Logging
Daemon doesn’t have standard IO. Log it. Do it with Logger.new. Set log levels etc. Use logging for debugging.
Security
When the system boots all is root. You don’t want your app running as root. Drop privileges as soon as you can so that you become a lesser user.
def become_user( …. See the slides
Testing
Create a test suite. Testing a daemon is challenging. Spawn the daemon before tests.
Mocking makes testing safer. See slides for code under slide Testing – Mocking makes this much safer.
This was a great talk with real code. We need more code.
More from jbasdf
- A copy of ApplicationController has been removed from the module tree but is still active! And can’t dup NilClass
- How New Carpet and Rattlesnake turned me into a Consultant or What the Hell Happened?
- Ruby on Rails i18n with the Rails I18n Textmate bundle
- Mac OSX and the Samsung ML1740 on a network
- More Stuff You Shouldn’t Hit on a Bike
Justin Ball is a software consultant and entrepreneur with a passion for Ruby. He evolved from a C++ and .Net monkey into a python programmer and finally found Ruby. In the rare moments when he isn't writing code, talking about code or measuring his code productivity in profanity per hour, you can find him on his bike in the mountains or on the roads surrounding Cache Valley. 









