Sporking with needy plugins

28 views
Skip to first unread message

Zach

unread,
Jun 25, 2009, 6:56:11 PM6/25/09
to sporkgem
I'm using has_many_polymorphs and thinking-sphinx, both of which eager
load a ton of models (and thinking-sphinx, for some odd reason, some
completely unrelated helpers too). I tried turning them off as plugins
and adding

require File.expand_path(File.dirname(__FILE__) + "/../vendor/
plugins/has_many_polymorphs/init")
require File.expand_path(File.dirname(__FILE__) + "/../vendor/
plugins/thinking_sphinx/init")

to the each_run, but that doesn't work - I assume because the rails
plugin mechanism does more than just call the init (setups up the load
paths too I guess).

before i go off on a mad hacking binge, has anyone worked around in an
elegant fashion?

thanks.

Tim Harper

unread,
Jun 25, 2009, 10:27:23 PM6/25/09
to spor...@googlegroups.com

Hi Zach,

if you run the command "spork -d", it will give you a list of all of the files in your project that are being preloaded, along with a list of each file and that call stack that loaded it.


Look through the call stack and to see if you can find the method responsible for preloading your models (it'll be somewhere in your plug-ins directory), and then trap the method as follows:


Spork.trap_method(HasMayPolymorphs::ClassName, :method_responsible_for_loading_my_models)
 
If it turns out to be a class method that is doing this, use the following:
 
Spork.trap_class_method(etc. etc.
 
What this does is it hooks into the named method and causes the execution of it to be delayed until occur after the fork occurs.  This is what spork uses in order to work its magic with rails by default (not that magic is always a good thing, but in this case, it was the best that I could come up with).
 
Let me know how this works for you, and thank you for chiming in your experience with spork.
Thanks ,
Tim

Zach

unread,
Aug 23, 2009, 5:10:47 PM8/23/09
to sporkgem
Hi Tim,

Have tried this and I have a bit of a chicken and egg situation. To
call

Spork.trap_class_method(ThinkingSphinx::Configuration, :instance)

I need to require config/environment (otherwise
"ThinkingSphinx::Configuration" is not defined) but loading rails
obviously loads the plugin so by the time I get there its already done
its damage.

I tried adding:

require 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx'
before that, but it can't handle loading the sub files in as rails
adds the plugin's lib to its load paths (so next step is to try
reproducing all the load path stuff). I'll proceed with that, but one
nice feature would be a should_receive style trap that could check for
parameters, so I could just trap the Action controller:

ActionController::Dispatcher.to_prepare :thinking_sphinx do
ThinkingSphinx::Configuration.instance.load_models
end

Something like

Spork.trap_class_method(ActionController::Dispatcher, :to_prepare).with
(:thinking_sphinx)

Which would avoid having to get complicated requiring in lots of
specific files from the plugin (all before rails has loaded which is
probably going to be a problem when I get on to Has Many Polymorphs
given that does many active record tweaks...)

best,

Zach

On Jun 25, 10:27 pm, Tim Harper <timchar...@gmail.com> wrote:
> Hi Zach,
>
> if you run the command "spork -d", it will give you a list of all of the
> files in your project that are being preloaded, along with a list of each
> file and that call stack that loaded it.
>
> Look through the call stack and to see if you can find the method
> responsible for preloading your models (it'll be somewhere in your plug-ins
> directory), and then trap the method as follows:
>
> Spork.trap_method(HasMayPolymorphs::ClassName,
> :method_responsible_for_loading_my_models)
>
> If it turns out to be a class method that is doing this, use the following:
>
> Spork.trap_class_method(etc. etc.
>
> What this does is it hooks into the named method and causes the execution of
> it to be delayed until occur after the fork occurs.  This is what spork uses
> in order to work its magic with rails by default (not that magic is always a
> good thing, but in this case, it was the best that I could come up with).
>
> Let me know how this works for you, and thank you for chiming in your
> experience with spork.
> Thanks ,
> Tim
>

Zach

unread,
Aug 24, 2009, 9:17:33 AM8/24/09
to sporkgem
Success!

Turned out the best way to do this was to use an initializer (which
strangely, are run before the plugin after_initalize blocks, would
have thought they should run last.):

config/initializers/spork.rb:

Spork.trap_method(ThinkingSphinx::Configuration, :load_models)

class Rails::Initializer
def after_initialize_with_trap
Spork.trap_class_method(HasManyPolymorphs, :autoload)
after_initialize_without_trap
end
alias_method_chain :after_initialize, :trap
end

Cut my load time from 13secs to 5secs (most of which is still
HasManyPolyMorphs.autoload I think... might hack that to only load its
related models instead of everything).

Zach

Zach

unread,
Aug 24, 2009, 9:19:49 AM8/24/09
to sporkgem
(needless to say that'll require an "if defined?(Spork)" wrapper to
actually get script/server to boot)
Reply all
Reply to author
Forward
0 new messages