Hi,
I have a long running task which generates JavaScript to files and I am
hoping to move this to a backgroundrb worker. The javascript is
currently generated with call to render_to_string:
File.open(File.join(target_folder, ‘all.js’), “w”) do |file|
file.puts(render_to_string :partial =>
‘partials/shared/javascript/tags’, :locals => { :namespaces =>
@namespaces, :tags => @tags, :parent_tags => @parent_tags, :show_counts
=> true })
end
It seems that the render_to_string is not available to the worker. Does
anyone know of a better way to achieve this using a partial? I can fall
back to generating the javascript text in a method, but this seems quite
an ugly solution (although will be much more performant than my current
in process solution).
Any tips or pointers would be much appreciated.
Thanks,
GiantCranes
Dan,
Thanks! That worked well. The only slight modification was the addition
of the following lines:
require ‘active_support’
require ‘action_controller’
require ‘action_view’
Thanks again,
GiantCranes
It is possible to render an action from backgroundrb, although it’s a
bit tedious. Backgroundrb doesn’t seem to load the controller layer
when it runs - I assume this is because it’s intended for manipulating
data, and not generating views, which makes sense. Anyway, in order to
get the rendering functions working you need to create a Dummy
controller that implements a few methods that render relies on. I’ve
included a stripped down version below which should work, although I
haven’t tested it. The example works on the older win32 compatible
version of backgroundrb, but I haven’t tried it on the newer *nix only
version.
====================================================
class SampleWorker < BackgrounDRb::Rails
require ‘action_view’
def do_work(args)
#redirect the rails default logger for active record to
backgroundrb
ActiveRecord::Base.logger= @logger
action_view =
ActionView::Base.new(Rails::Configuration.new.view_path, {},
DummyController.new)
action_view.render(:partial => “foo/bar”)
end
end
class DummyController
def logger
@logger
end
def headers
{‘Content-Type’ => ‘application/xml’}
end
def response
DummyResponse.new
end
def content_type
‘application/xml’
end
end
class DummyResponse
def content_type
‘application/xml’
end
end
====================================================
Hope that helps,
Dan