# BackgrounDRb question

**URL:** <https://www.ruby-forum.com/t/backgroundrb-question/89751>\
**Category:** Rails\
**Created:** [March 22, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751 "2007-03-22T14:13:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 22, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/1 "2007-03-22T14:13:28Z")

</div>

Hi,

I am going to use BackgrounDRb for to handle some processing, but have  
one question that I haven’t been able to find in the documentation. I  
have an application that accepts XML docs via a HTTP POST. I am going  
to hand the processing of the XML doc to worker, so that I can send  
back a HTTP REPLY right away. It seems that you need to make an  
explicit call to worker.delete to stop it from running. My question  
is, is there an easy way for the worker to delete itself after running  
its do\_work method, or a way to notify the main application that it  
has completed the processing and is ready to be deleted?

Thanks,

Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 22, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/2 "2007-03-22T14:35:30Z")

</div>

Hi Simon,

Simon wrote:

> is, is there an easy way for the worker to delete itself after running  
> its do\_work method, or a way to notify the main application that it  
> has completed the processing and is ready to be deleted?

Add the following two lines at the bottom of your do\_work method.

ActiveRecord::Base.connection.disconnect!  
::BackgrounDRb::MiddleMan.instance.delete\_worker @\_job\_key

The first ensures that the database connection has been closed and the  
second kills the worker.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [April 5, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/3 "2007-04-05T19:22:15Z")

</div>

Hi,

I tried adding those two lines to the bottom of my do\_work method, and  
am now getting the following error on the second line  
(::BackgrounDRb::MiddleMan.instance.delete\_worker @\_job\_key)

20070405-13:15:15 (19653) In do work  
20070405-13:15:18 (19653) You have a nil object when you didn’t expect  
it!  
The error occured while evaluating nil.shutdown - (NoMethodError)  
20070405-13:15:18 (19653) /usr/local/src/rails/434Wireless/vendor/  
plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:373:in  
`delete_worker' 20070405-13:15:18 (19653) /usr/local/src/rails/434Wireless/lib/workers/ parser_worker.rb:657:in`do\_work’

Any ideas what the issue might be?

Thanks again,

Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [April 5, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/4 "2007-04-05T19:30:14Z")

</div>

Simon wrote:

> Hi,
> 
> I tried adding those two lines to the bottom of my do\_work method, and  
> am now getting the following error on the second line  
> (::BackgrounDRb::MiddleMan.instance.delete\_worker @\_job\_key)

It seems like @\_job\_key is nil.

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [April 5, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/5 "2007-04-05T19:40:18Z")

</div>

Yes, it does. What I’m trying to do is to delete the worker from  
inside its own do\_work method, so that the main Rails app creates a  
new worker instance, and then the worker does some work, and deletes  
itself. Is there another method to accomplish this?

Thanks,

Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [October 18, 2007, 4:53am UTC](https://www.ruby-forum.com/t/backgroundrb-question/89751/6 "2007-10-18T04:53:12Z")

</div>

Simon wrote:

> Yes, it does. What I’m trying to do is to delete the worker from  
> inside its own do\_work method, so that the main Rails app creates a  
> new worker instance, and then the worker does some work, and deletes  
> itself. Is there another method to accomplish this?
> 
> Thanks,
> 
> Simon

I’ve done a lot of playing around and so I’ll post this for others who  
might find this page via google.

I noticed that when I used the above method, delete\_worker that the  
worker wouldn’t be deleted. It would just hang around. Even though the  
worker was done working, but Middleman.get\_work(key) would still return  
a worker!!! _sigh_

Anyway, I then dug around the code of backgrounDRb and found kill\_worker  
which i added to the of the do\_work method. And low and behold, workers  
were dying!

ActiveRecord::Base.connection.disconnect!  
::BackgrounDRb::MiddleMan.instance.kill\_worker @\_job\_key

Also FYI, I also noticed in other posts that @\_job\_key was returning nil  
while @job\_key wasn’t. Just putting that out there.

Cheers
