23 July 2007

Timeout a ruby system call

I was rather disappointed that I could not set a timeout on a system call. This is especially a problem because of the very poor threading model that ruby currently has. But, at least there is a way to get the job done. The following is a method I wrote to make a system call and then kill it if the timeout is reached.

require 'timeout'
class System
def self.system_with_timeout(timeout, *args)
if ( (pid = fork) == nil )
#child process
@@logger.debug(args.join(' '))
exec(*args)
else
success = false
#parent process
begin
#TODO if the process fails return false
success = Timeout::timeout(timeout){ Process.waitpid(pid) }
rescue Timeout::Error
@@logger.error "***** Timeout error"
Process.kill("HUP", pid)
Process.detach(pid)
end
success
end
end
end

04 April 2007

ActionMalier and GMail

This post has been in my queue to write about for over a month now. I just wanted to give thanks to Stephen Chu for posting about how to use ActionMailer with gmail. At FamilyLearn we use Google Apps for all of our collaboration services. It has been great to hook our application into the same system. This helps so I do not have to maintain a mail server. Here is the link to his excellent article.

http://www.stephenchu.com/2006/06/how-to-use-gmail-smtp-server-to-send.html