grape-on-goliath icon indicating copy to clipboard operation
grape-on-goliath copied to clipboard

doesn't autoload in development mode

Open larryzhao opened this issue 11 years ago • 15 comments

Thanks @dblock for the example on how to use Grape on Goliath.

But I found that this example could not autoload modified files in development mode. And I tried a simpler approach like this: https://gist.github.com/larryzhao/7913394 , after I modified the api, I could see the Rack::Reloader is trigger to load the file, but the response is still the same.

But grape-on-rails works just fine. Is there anything I need to configure in Grape to achieve that?

Thank you very much.

larryzhao avatar Dec 12 '13 03:12 larryzhao

TBH I couldn't fix it.

dblock avatar Dec 12 '13 14:12 dblock

@dblock @larryzhao I was able to get this to work in my own project. Instead of manually requiring your development files, you need to have them autoloaded. Active Support has nice exposure for this.

  • You need active_support/dependencies
require 'active_support/dependencies'
  • Select the directories you want constants autoloaded from
relative_load_paths = Dir[
  "#{PROJECT_ROOT}/app/api/**/",
  "#{PROJECT_ROOT}/app/models/**/",
  "#{PROJECT_ROOT}/app/helpers/**/"
]
ActiveSupport::Dependencies.autoload_paths += relative_load_paths
  • Then in your Goliath::API
  class Server < Goliath::API
    def response(env)
      Base.call(env)
    end

    def on_close(env)
      if Goliath.env == :development
        ActiveSupport::Dependencies::clear
      end
    end
  end

Once the connection has closed for a Fiber it should reload just your application code. Atleast it's been working for me. Hope this helps.

wolfie82 avatar Jun 15 '14 08:06 wolfie82

@patbaker82 Would you mind PR-ing this into this project? Much appreciated.

dblock avatar Jun 15 '14 19:06 dblock

And also a README update to https://github.com/intridea/grape

dblock avatar Jun 15 '14 19:06 dblock

@dblock No problem, however I will likely get to it tomorrow.

wolfie82 avatar Jun 15 '14 21:06 wolfie82

@patbaker82, is there any chance to see your solution in action?

skydan avatar Jun 20 '14 19:06 skydan

@skydan Sorry, I haven't had time to merge this into this repo. I started a new project with Goliath and Grape, without using this structure. However, I did see this issue when I was looking at examples, thus my update when I got it working. I've now dropped Goliath and Grape and have moved to something different.

I committed https://github.com/patbaker82/grape_juice which shows how I had it working.

wolfie82 avatar Jun 20 '14 20:06 wolfie82

@skydan Note the project is a bit out of whack. You can look at https://github.com/patbaker82/grape_juice/blob/master/lib/grape_juice.rb specifically and you can basically put that into the application.rb in this project. All in all its pretty simple. You just need to autoload the dependencies with active support and modify the Goliath on_close event.

wolfie82 avatar Jun 20 '14 20:06 wolfie82

Yes, I've added my dependencies in autoload_paths and also on_close event triggers Activesupport::Dependency.clear as expected. But unfortunately this is doesn't affect reloading api modules and classes.

skydan avatar Jun 20 '14 20:06 skydan

Is the on_close event firing? Can you put a logger/puts statement in there? You would also need to make sure that the paths that are in autoload_paths are relative to where the application thinks it is. I would put the full path in to see if that helps and then figure out the relative bit later.

wolfie82 avatar Jun 20 '14 20:06 wolfie82

@skydan I would also look at https://github.com/rails/docrails/blob/master/activesupport/lib/active_support/dependencies.rb

You can print out what's in Activesupport::Dependency.loaded() and autoloaded_constants() to see if they've even been loaded.

wolfie82 avatar Jun 20 '14 20:06 wolfie82

Hmm, loaded and autoloaded_constants are empty during clear method call. But relative_load_paths looks like correct. What should I do to populate loaded?

skydan avatar Jun 20 '14 20:06 skydan

@skydan I can't see the gist

wolfie82 avatar Jun 20 '14 21:06 wolfie82

I've fixed the link.

skydan avatar Jun 20 '14 21:06 skydan

@skydan It's interesting to point out that the files wont be "loaded" until they're needed.

Also I would try:

<full_path>/app/models/**/
<full_path>/app/helpers/**/
<full_path>/app/presenters/**/

etc

wolfie82 avatar Jun 20 '14 21:06 wolfie82