Thread problem

Hi!

I’m having a strange problem. I need to open a mongrel server (with
mongrel_service and Windows) and I also need to run tasks in parallel
with mongrel. So, first I tried a Thread.new do … end in my
environment.rb. Soon I realized it would never work, because even
running tasks like rake db:migrate, rake test, my thread would run. That
was not my intention… I just need to run when I start mongrel. So, I
override the “run” method from mongrel, this way:

module Mongrel
class Configurator
alias_method :original_mongrel_run, :run
def run
original_mongrel_run
Thread.new
$my_object = MyClass.new
$my_object.run_task
end
end
end
end

My run_task method is something like this:

def run_task
while true
begin
RAILS_DEFAULT_LOGGER.debug ‘Running task’
do_something
sleep(60)
RAILS_DEFAULT_LOGGER.debug ‘Task Done’
rescue Exception => e
RAILS_DEFAULT_LOGGER.error e.message
end
end
end

It seemed to work fine. But, sometimes, I’m having some weird problems.
Sometimes, my thread suddenly “dies” after a few hours, during the
“sleep” command. According to my log, the task isn’t stopping inside the
“do_something” method. Instead, it is stopping during my sleep time.

There’s another problem. The task is running OK for some time. But, if I
open my web browser and navigate to a single page in my app, I start to
get thousands of “stack level too deep” errors, in my rescue block! I
tried (almost) everything (at least everything I know) to solve this,
but I couldn’t figure out the problem!

Maybe I should be running this thread and these tasks in another way.
Any suggestions?

Thanks!

Felipe.