# Background work

**URL:** <https://www.ruby-forum.com/t/background-work/70729>\
**Category:** Rails\
**Created:** [February 20, 2006, 4:41am UTC](https://www.ruby-forum.com/t/background-work/70729 "2006-02-20T04:41:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 20, 2006, 4:41am UTC](https://www.ruby-forum.com/t/background-work/70729/1 "2006-02-20T04:41:54Z")

</div>

Greetings,

I’ve got a rails app which is basically an admin interface to a xml  
dataset. Currently I’ve got rails rigged to call a function to spit the  
entire dataset to file with every change. This would actually be ok,  
its  
fairly infrequently updated, but when the dataset grows large the user  
sits  
there waiting for it to dump with no feedback as to whats going on. Oh  
yes,  
nearly forgot; I’m on Lighttpd with FastCGI.

I was thinking maybe just maybe I could dump to file after doing the  
render() or redirect\_to() calls and perhaps the client would get their  
response before the method call terminated, that perhaps the render() /  
redirect\_to() was actually ther response being sent. I’m pretty sure  
the  
this is wrong, that the method call termiantion signals the response to  
client, but I got a very odd error when I tried it;

ActionController::DoubleRenderError (Can only render or redirect once  
per  
action):  
/vendor/rails/actionpack/lib/action\_controller/base.rb:591:in  
`render_with_no_layout' /vendor/rails/actionpack/lib/action_controller/layout.rb:228:in`render\_without\_benchmark’  
/vendor/rails/actionpack/lib/action\_controller/benchmarking.rb:53:in  
`render' /usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/vendor/rails/actionpack/lib/action\_controller/benchmarking.rb:53:in  
`render' /vendor/rails/actionpack/lib/action_controller/base.rb:636:in`render\_to\_string’

I use render\_to\_string(:partial =\> ‘datapoint’) to render the individual  
datapoints; whats odd is that render\_to\_string isnt working after  
redirect\_to and render(). If I move the data-dump before these, it  
works  
fine and no errors appear. But whatever, although the error is just  
plain  
wrong and should not happen(there’s no reason rener\_to\_string should  
stop  
working after redirect\_to or render), I’m pretty sure it wouldnt fix  
anything even if it was working.

Anyways, I gotta do something. I’ve really got two key important  
factors;  
#1 the user needs to not have to wait while data gets dumped, they  
should  
get their response instantaneously  
#2 the data dump must occur ASAP. Even a 1 minute delay is not  
acceptable,  
so no Cron.

Thanks!  
Rektide “CronHater”

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 20, 2006, 6:55am UTC](https://www.ruby-forum.com/t/background-work/70729/2 "2006-02-20T06:55:31Z")

</div>

Rektide-

```
This looks like a job for ruby threads. If you do it like this the

```

thread will run to completion in the background and your method will  
continue right away onto the rest of its body and render or  
redirect\_to calls.

def foo\_action  
Thread.new {  
xml = render\_to\_string(:partial =\>  
‘datapoint’)  
File.open(“#{RAILS\_ROOT}/public/  
xml\_files/data.xml”, “w+”){ |f| f.write(xml) }  
}  
#now you can render or redirect\_to and your file will  
#get written to disk in the backgroundconcurrently  
redirect\_to :controller =\> ‘foo’, :Action =\> ‘bar’  
end

Cheers-  
-Ezra

On Feb 19, 2006, at 7:39 PM, Matthew F. wrote:

> I was thinking maybe just maybe I could dump to file after doing  
> `render\_with\_no\_layout’  
> I use render\_to\_string(:partial =\> ‘datapoint’) to render the  
> #1 the user needs to not have to wait while data gets dumped, they  
> should get their response instantaneously  
> #2 the data dump must occur ASAP. Even a 1 minute delay is not  
> acceptable, so no Cron.
> 
> Thanks!  
> Rektide “CronHater”
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 20, 2006, 8:43am UTC](https://www.ruby-forum.com/t/background-work/70729/3 "2006-02-20T08:43:54Z")

</div>

You must be careful what you do inside a thread. Rails has a  
allow\_concurrency flags for ActionController and ActiveRecord, and for  
best  
safety, this should be set to true for your application.

Confusingly, it seems this is false for ActionController and true for  
ActiveRecord by default!

For ActionController, this value seems to only be used by the caching  
code  
right now, but that could change and break an app that doesn’t set this  
value correctly.

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ezra  
Zygmuntowicz  
Sent: Sunday, February 19, 2006 9:50 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Rails] Background work

Rektide-

```
        This looks like a job for ruby threads. If you do it like 

```

this  
the thread will run to completion in the background and your method will  
continue right away onto the rest of its body and render or redirect\_to  
calls.

def foo\_action

Thread.new {

xml = render\_to\_string(:partial =\> ‘datapoint’)

File.open(“#{RAILS\_ROOT}/public/xml\_files/data.xml”, “w+”){ |f|  
f.write(xml)  
}

}

#now you can render or redirect\_to and your file will

#get written to disk in the backgroundconcurrently

redirect\_to :controller =\> ‘foo’, :Action =\> ‘bar’

end

Cheers-

-Ezra

On Feb 19, 2006, at 7:39 PM, Matthew F. wrote:

Greetings,

I’ve got a rails app which is basically an admin interface to a xml  
dataset.  
Currently I’ve got rails rigged to call a function to spit the entire  
dataset to file with every change. This would actually be ok, its fairly  
infrequently updated, but when the dataset grows large the user sits  
there  
waiting for it to dump with no feedback as to whats going on. Oh yes,  
nearly  
forgot; I’m on Lighttpd with FastCGI.

I was thinking maybe just maybe I could dump to file after doing the  
render() or redirect\_to() calls and perhaps the client would get their  
response before the method call terminated, that perhaps the render() /  
redirect\_to() was actually ther response being sent. I’m pretty sure the  
this is wrong, that the method call termiantion signals the response to  
client, but I got a very odd error when I tried it;

ActionController::DoubleRenderError (Can only render or redirect once  
per  
action):  
/vendor/rails/actionpack/lib/action\_controller/base.rb:591:in  
`render_with_no_layout' /vendor/rails/actionpack/lib/action_controller/layout.rb:228:in `render\_without\_benchmark’  
/vendor/rails/actionpack/lib/action\_controller/benchmarking.rb:53:in  
`render' /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’  
/vendor/rails/actionpack/lib/action\_controller/benchmarking.rb:53:in  
`render' /vendor/rails/actionpack/lib/action_controller/base.rb:636:in `render\_to\_string’

I use render\_to\_string(:partial =\> ‘datapoint’) to render the individual  
datapoints; whats odd is that render\_to\_string isnt working after  
redirect\_to and render(). If I move the data-dump before these, it works  
fine and no errors appear. But whatever, although the error is just  
plain  
wrong and should not happen(there’s no reason rener\_to\_string should  
stop  
working after redirect\_to or render), I’m pretty sure it wouldnt fix  
anything even if it was working.

Anyways, I gotta do something. I’ve really got two key important  
factors;  
#1 the user needs to not have to wait while data gets dumped, they  
should  
get their response instantaneously  
#2 the data dump must occur ASAP. Even a 1 minute delay is not  
acceptable,  
so no Cron.

Thanks!  
Rektide “CronHater”

* * *

Rails mailing list

removed\_email\_address@domain.invalid

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.

WebMaster

Yakima Herald-Republic Newspaper

removed\_email\_address@domain.invalid

509-577-7732

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [February 20, 2006, 9:38am UTC](https://www.ruby-forum.com/t/background-work/70729/4 "2006-02-20T09:38:16Z")

</div>

Tom is right. There are some issues to worry about with rails and  
threading. Usually something like this will be fine though. But do  
test thouroughly. Another way to do it is with fork. But with fork  
you have to be careful, if you dont use the right hack, then you will  
get the message “Mysql has gone away” or whatever db you are using.  
But with this little hack it wont happen:

def foo\_action  
fork do  
xml = render\_to\_string(:partial =\> ‘datapoint’)  
File.open(“#{RAILS\_ROOT}/public/xml\_files/data.xml”, “w+”){ |  
f| f.write(xml) }  
Kernal.exec “echo -n”  
end  
#now you can render or redirect\_to and your file will  
#get written to disk in the backgroundconcurrently  
redirect\_to :controller =\> ‘foo’, :Action =\> ‘bar’  
end

```
That Kernal.exec "echo -n" part is the hack that will keep your

```

database from “goping away” as rails puts it 😉

Cheers-  
-Ezra

On Feb 19, 2006, at 11:44 PM, Tom F. wrote:

> Subject: Re: [Rails] Background work  
> render or redirect\_to calls.  
> f.write(xml) }
> 
> the entire dataset to file with every change. This would actually  
> signals the response to client, but I got a very odd error when I  
> /usr/lib/ruby/1.8/benchmark.rb:293:in `measure’  
> (there’s no reason rener\_to\_string should stop working after  
> Thanks!
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732
