Background work

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:inrender_without_benchmark’
/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:53:in
render' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:53:in
render' /vendor/rails/actionpack/lib/action_controller/base.rb:636:inrender_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”

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

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: [email protected]
[mailto:[email protected]] On Behalf Of Ezra
Zygmuntowicz
Sent: Sunday, February 19, 2006 9:50 PM
To: [email protected]
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

[email protected]

http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.

WebMaster

Yakima Herald-Republic Newspaper

[email protected]

509-577-7732

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 :wink:

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732