# Mutex::Synchronize with backgroundrb

**URL:** <https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427>\
**Category:** Rails\
**Created:** [July 7, 2006, 12:42am UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427 "2006-07-07T00:42:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 12:42am UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/1 "2006-07-07T00:42:41Z")

</div>

I have a question concerning where to use the synchronize function. The  
point of it is to put all of the code blacks passed to this function in  
a queue. This make sure that code gets ran one at a time. This is  
required when doing any kind of database activity in backgrondrb and  
multiple threads. Otherwise I lose the connection to my database.

So here is my question:

Do I need to do:  
@lock = Mutext.new

And do this for EVERY database interaction WITHIN background rb:  
@lock.synchronize do  
#some db activity, like model.save!, model.find, anything that  
queries the db  
end

Or do I need to wrap ALL database activity in a sychronize method?  
Including various database activity in my controllers that will never be  
called by backgroundrb.

Because what if someone makes a request to  
[somedomain.com](http://somedomain.com/products/create) while my backgroundrb script is calling  
Product.find\_all at the same time? Even though they aren’t both in the  
same process would this cause a problem? Or is synchronize ONLY meant  
for using in a single process environment where other processes are  
irrelevant?

Sorry if this is confusing, any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 1:16am UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/2 "2006-07-07T01:16:38Z")

</div>

On Jul 6, 2006, at 3:42 PM, Ben J. wrote:

> Do I need to do:  
> never be  
> called by backgroundrb.
> 
> Because what if someone makes a request to  
> [somedomain.com](http://somedomain.com/products/create) while my backgroundrb script is calling  
> Product.find\_all at the same time? Even though they aren’t both in the  
> same process would this cause a problem? Or is synchronize ONLY meant  
> for using in a single process environment where other processes are  
> irrelevant?
> 
> Sorry if this is confusing, any help is greatly appreciated.

Hey Ben-

```
You have it right. You do not need to use a mutex in rails at all

```

since the drb server is a separate process with its own db  
connection. So don’t worry about it on the rails side of things. I am  
still experimenting with different techniques for allowing  
concurrency with active record so you won’t need a mutex. Are you  
using the latest version of the plugin? I added this line recently:

ActiveRecord::Base.allow\_concurrency = true

```
This is supposed to allow for thread safe access to AR objects. BUt

```

I have had mixed results. Maybe you can send me a copy of your worker  
class and I can help you get it working. Also consider joining the  
backgroundrb mailing list:

\*Mailing list: [http://rubyforge.org/mailman/listinfo/backgroundrb-devel](http://rubyforge.org/mailman/listinfo/backgroundrb-devel)

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/3 "2006-07-07T19:15:41Z")

</div>

Where exactly would I add this line?

ActiveRecord::Base.allow\_concurrency = true

Thanks a lot for the help.

Ezra Z. wrote:

> On Jul 6, 2006, at 3:42 PM, Ben J. wrote:
> 
> > Do I need to do:  
> > never be  
> > called by backgroundrb.
> > 
> > Because what if someone makes a request to  
> > [somedomain.com](http://somedomain.com/products/create) while my backgroundrb script is calling  
> > Product.find\_all at the same time? Even though they aren’t both in the  
> > same process would this cause a problem? Or is synchronize ONLY meant  
> > for using in a single process environment where other processes are  
> > irrelevant?
> > 
> > Sorry if this is confusing, any help is greatly appreciated.
> 
> Hey Ben-
> 
> You have it right. You do not need to use a mutex in rails at all  
> since the drb server is a separate process with its own db  
> connection. So don’t worry about it on the rails side of things. I am  
> still experimenting with different techniques for allowing  
> concurrency with active record so you won’t need a mutex. Are you  
> using the latest version of the plugin? I added this line recently:
> 
> ActiveRecord::Base.allow\_concurrency = true
> 
> This is supposed to allow for thread safe access to AR objects. BUt  
> I have had mixed results. Maybe you can send me a copy of your worker  
> class and I can help you get it working. Also consider joining the  
> backgroundrb mailing list:
> 
> \*Mailing list: [http://rubyforge.org/mailman/listinfo/backgroundrb-devel](http://rubyforge.org/mailman/listinfo/backgroundrb-devel)
> 
> Cheers-  
> -Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/4 "2006-07-07T20:29:22Z")

</div>

Ben-

```
Take a look in the script/backgroundrb/start file. You should see

```

that line in there like this:

if CONFIG[‘load\_rails’]  
ActiveRecord::Base.allow\_concurrency = true  
ActiveRecord::Base.establish\_connection(YAML.load(ERB.new(IO.read("#  
{RAILS\_ROOT}/#{CONFIG[‘database\_yml’]}")).result)[CONFIG  
[‘environment’]])  
end

```
So if you don't see that line in the start script then you need to

```

add it or upgrade your version of the plugin.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/5 "2006-07-07T21:00:16Z")

</div>

How do I update the backgrondrb plugin?

Sorry for the noob question.

Ezra Z. wrote:

> Ben-
> 
> Take a look in the script/backgroundrb/start file. You should see  
> that line in there like this:
> 
> if CONFIG[‘load\_rails’]  
> ActiveRecord::Base.allow\_concurrency = true  
> ActiveRecord::Base.establish\_connection(YAML.load(ERB.new(IO.read("#  
> {RAILS\_ROOT}/#{CONFIG[‘database\_yml’]}")).result)[CONFIG  
> [‘environment’]])  
> end
> 
> So if you don’t see that line in the start script then you need to  
> add it or upgrade your version of the plugin.
> 
> Cheers-  
> -Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/6 "2006-07-07T20:30:36Z")

</div>

Also,

I have the following method in my worker class. I ran that in 6  
different threads at once. No allow\_concurrency and no mutex.synchronize  
and I didn’t have a problem. They all ran over night for about 8 hours  
without a problem. The updated\_at field was changing every second.  
Shouldn’t it lose connection to my database is 6 different threads are  
accessing the database as fast as they can?

def monitor\_event(event\_id)  
event = Event.find event\_id  
@event\_monitor\_threads[event.id] = Thread.new(event) do |event|  
while Event.exists? event.id  
event.save!  
end  
end  
end

Ezra Z. wrote:

> On Jul 6, 2006, at 3:42 PM, Ben J. wrote:
> 
> > Do I need to do:  
> > never be  
> > called by backgroundrb.
> > 
> > Because what if someone makes a request to  
> > [somedomain.com](http://somedomain.com/products/create) while my backgroundrb script is calling  
> > Product.find\_all at the same time? Even though they aren’t both in the  
> > same process would this cause a problem? Or is synchronize ONLY meant  
> > for using in a single process environment where other processes are  
> > irrelevant?
> > 
> > Sorry if this is confusing, any help is greatly appreciated.
> 
> Hey Ben-
> 
> You have it right. You do not need to use a mutex in rails at all  
> since the drb server is a separate process with its own db  
> connection. So don’t worry about it on the rails side of things. I am  
> still experimenting with different techniques for allowing  
> concurrency with active record so you won’t need a mutex. Are you  
> using the latest version of the plugin? I added this line recently:
> 
> ActiveRecord::Base.allow\_concurrency = true
> 
> This is supposed to allow for thread safe access to AR objects. BUt  
> I have had mixed results. Maybe you can send me a copy of your worker  
> class and I can help you get it working. Also consider joining the  
> backgroundrb mailing list:
> 
> \*Mailing list: [http://rubyforge.org/mailman/listinfo/backgroundrb-devel](http://rubyforge.org/mailman/listinfo/backgroundrb-devel)
> 
> Cheers-  
> -Ezra

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 7, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/mutex-synchronize-with-backgroundrb/97427/7 "2006-07-07T22:03:29Z")

</div>

Ben-

```
If you are not getting errors then you are good to go.Threading with

```

ActiveRecord is a tough subject that no one seems to give a straight  
answer to. You can only really just test your solution with real  
world type loads and see if it falls on its face. But the  
allow\_concurreny does seem to help a lot for me.

```
To update the plugin you need to do this:

```

delete the RAILS\_ROOT/script/backgroundrb directory from your rails app.  
delete the RAILS\_ROOT/config/backgroundrb.yml file  
Do not delete your workers in lib/workers.

Then delete the plugin from RAILS\_ROOT/vendor/plugins/backgroundrb.  
Then reinstall the plugin with:  
$ script/plugin install svn://rubyforge.org//var/svn/backgroundrb

Then you just need to run the setup rake task.

$ rake backgroundrb:setup

Cheers-  
-Ezra
