# Thread problem

**URL:** <https://www.ruby-forum.com/t/thread-problem/171181>\
**Category:** Deployment\
**Created:** [June 27, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/thread-problem/171181 "2009-06-27T21:31:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![felipegiotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/13edae/32.png) [@felipegiotto](https://www.ruby-forum.com/u/felipegiotto)\
**Post date:** [June 27, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/thread-problem/171181/1 "2009-06-27T21:31:33Z")

</div>

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.
