I’m trying to run an external Ruby script from my Rails app.
To do this i’m using this function
def fork_with_new_connection(config, my_class = ActiveRecord::Base)
pid = fork do
begin
my_class.establish_connection(config)
yield
ensure
my_class.remove_connection
end
end
Process.detach(pid)
end
Then, within my controller I do this:
# fork a process
config = ActiveRecord::Base.remove_connection
fork_with_new_connection(config) {
@environment = (RAILS_ENV == 'development') ? 'dev' : 'prod'
system("ruby ./public/utils/delicious_import.rb -u #{@user} -p
#{@pass} -r #{@rating} -d #{current_user} -e #{@environment}")
}
ActiveRecord::Base.establish_connection(config)
I have two questions. First, is this the proper/correct way to run an
external script from a rails controller. I just want to fire off the
script and continue; I don’t want to wait around for its completion or
status.
Second, when I do this, my mongrel.pid file seems to be deleted as soon
as the child process completes. It looks like the child process’s
parent is a mongrel_rails process (which makes sense since I am running
mongrel), but then when the process dies, it also removes the
mongrel.pid file from my log/ directory.