--- ar_sendmail.rb      2008-03-06 15:32:46.000000000 -0800
+++ /usr/lib/ruby/gems/1.8/gems/ar_mailer-1.3.1/lib/action_mailer/ar_sendmail.rb 2008-03-06 16:43:44.000000000 -0800
@@ -96,6 +96,16 @@

attr_accessor :failed_auth_count

+ @@pid_file = nil
+
+ def self.remove_pid_file
+ if @@pid_file
+ require 'shell'
+ sh = Shell.new
+ sh.rm @@pid_file
+ end
+ end
+
##
# Creates a new migration using +table_name+ and prints it on stdout.

@@ -190,6 +200,7 @@
options[:Once] = false
options[:RailsEnv] = ENV['RAILS_ENV']
options[:TableName] = 'Email'
+ options[:Pidfile] = options[:Chdir] + '/log/ar_sendmail.pid'

opts = OptionParser.new do |opts|
opts.banner = "Usage: #{name} [options]"
@@ -236,6 +247,12 @@
options[:Daemon] = true
end

+ opts.on("-p", "--pidfile PIDFILE",
+ "Set the pidfile location",
+ "Default: #{options[:Chdir]}#{options[:Pidfile]}", String) do |pidfile|
+ options[:Pidfile] = pidfile
+ end
+
opts.on( "--mailq",
"Display a list of emails waiting to be sent") do |mailq|
options[:MailQ] = true
@@ -334,7 +351,22 @@

if options[:Daemon] then
require 'webrick/server'
+ @@pid_file = options[:Pidfile]
+ if File.exists? @@pid_file
+ # check to see if process is actually running
+ pid = ''
+ File.open(@@pid_file, 'r') {|f| pid = f.read.chomp }
+ if system("ps -p #{pid} | grep #{pid}") # returns true if process is running, o.w. false
+ $stderr.puts "Warning: The pid file #{@@pid_file} exists and ar_sendmail is running. Shutting down."
+ exit
+ else
+ # not running, so remove existing pid file and continue
+ self.remove_pid_file
+ log "ar_sendmail is not running. Removing existing pid file and starting up..."
+ end
+ end
WEBrick::Daemon.start
+ File.open(@@pid_file, 'w') {|f| f.write("#{Process.pid}\n")}
end

new(options).run
@@ -454,6 +486,7 @@

def do_exit
log "caught signal, shutting down"
+ self.remove_pid_file
exit
end