Forum: Ruby on Rails Background work

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
398793743ae611eb217bbdcfa5cec830?d=identicon&s=25 Matthew Fowle (Guest)
on 2006-02-20 04:41
(Received via mailing list)
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"
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-20 06:55
(Received via mailing list)
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 Fowle 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2006-02-20 08:43
(Received via mailing list)
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: rails-bounces@lists.rubyonrails.org
[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Ezra
Zygmuntowicz
Sent: Sunday, February 19, 2006 9:50 PM
To: rails@lists.rubyonrails.org
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 Fowle 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

Rails@lists.rubyonrails.org

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



-Ezra Zygmuntowicz

WebMaster

Yakima Herald-Republic Newspaper

ezra@yakima-herald.com

509-577-7732
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-20 09:38
(Received via mailing list)
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 Fakes 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.