Skip to content

doesn't autoload in development mode  #1

Closed

Description

@larryzhao

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.

Activity

  1. dblock commented on Dec 12, 2013

    @dblock
    Member

    TBH I couldn't fix it.

  2. wolfie82 commented on Jun 15, 2014

    @wolfie82

    @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.

  3. dblock commented on Jun 15, 2014

    @dblock
    Member

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

  4. dblock commented on Jun 15, 2014

    @dblock
    Member

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

  5. wolfie82 commented on Jun 15, 2014

    @wolfie82

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

  6. skydan commented on Jun 20, 2014

    @skydan

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

  7. wolfie82 commented on Jun 20, 2014

    @wolfie82

    @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.

  8. wolfie82 commented on Jun 20, 2014

    @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.

  9. skydan commented on Jun 20, 2014

    @skydan

    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.

  10. wolfie82 commented on Jun 20, 2014

    @wolfie82

    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.

  11. wolfie82 commented on Jun 20, 2014

    @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.

  12. skydan commented on Jun 20, 2014

    @skydan

    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?

  13. wolfie82 commented on Jun 20, 2014

    @wolfie82

    @skydan I can't see the gist

  14. skydan commented on Jun 20, 2014

    @skydan

    I've fixed the link.

  15. wolfie82 commented on Jun 20, 2014

    @wolfie82

    @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

  16. dblock commented on Oct 27, 2026

    @dblock
    Member

    Implemented in #5. Grape retains compiled routes and references to mounted API classes, so Rack::Reloader can reload a file while requests still use the old routes. The proposed development command, bundle exec rerun -- ruby app.rb -sv -e development, watches application Ruby files and restarts the server instead; normal startup is unchanged.

    A real HTTP regression verifies that editing an API changes its route and response and removes the old route. All hosted Ruby/Goliath matrix jobs, lint, and coverage pass. This issue will close when #5 merges.

    AI posting on behalf of @dblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions