grape-on-goliath
grape-on-goliath copied to clipboard
doesn't autoload in development mode
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.
TBH I couldn't fix it.
@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.
@patbaker82 Would you mind PR-ing this into this project? Much appreciated.
And also a README update to https://github.com/intridea/grape
@dblock No problem, however I will likely get to it tomorrow.
@patbaker82, is there any chance to see your solution in action?
@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.
@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.
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.
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.
@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.
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 I can't see the gist
I've fixed the link.
@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