Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start task invokes environment, environment errors if it can't connect to the server #14

Open
jamesprior opened this issue Sep 25, 2015 · 4 comments

Comments

@jamesprior
Copy link

Hi, I'm sorry to start this off with a vague statement and no test, hopefully this is the right project and sounds familiar enough.

In my project, after updating the rails application config I could not start the server via the rake task - instead I got an error that it could not connect to the server. I suspect it is because the start task is invoking the rails environment task somewhere and it is set to connect on boot.

Does that sound plausible? For now I've addressed it by monkey patching the Railtie to catch the exception and setting the connection timeout to very low.

@cheerfulstoic
Copy link
Contributor

Yup, I think you're in the right place ;)

So could you share some examples of the commands / output?

The rake tasks shouldn't depend at all on the Rails environment. The server is installed by default in db/neo4j/(development|test|production) and then the rake tasks just execute shell commands to Neo4j directly. These tasks were extracted from the other gems, though, so if there's a place when that is happening we should fix it!

Thanks!

@jamesprior
Copy link
Author

Okay great! In the course of writing this up I think I figured out what happened. Here's the start of my process:

What I've done:
rails new neo4jtest

Edit the Gemfile and add:
gem 'neo4j'

Per the "Install in Ruby on Rails" section at http://neo4jrb.io/ edit config/application.rb and add:

  config.neo4j.session_type = :server_db
  config.neo4j.session_path = ENV['NEO4J_URL'] || 'http://localhost:7475'

Then run:
bin/rake neo4j:install

The result is:

/Users/jamesprior/.gem/ruby/2.1.2/gems/railties-4.1.7/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `neo4j' for #<Rails::Application::Configuration:0x007fbc13393038> (NoMethodError)
    from /Users/jamesprior/Development/neotest/config/application.rb:12:in `<class:Application>'
    from /Users/jamesprior/Development/neotest/config/application.rb:10:in `<module:Neotest>'
    from /Users/jamesprior/Development/neotest/config/application.rb:9:in `<top (required)>'

Which is a problem I remember from working on it before. I think the fix I found was to add require 'neo4j/railtie' at the top of application.rb

That done, running the install rake task again gives me another error:

/Users/jamesprior/.gem/ruby/2.1.2/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:641:in `rescue in connection_for': connection refused: localhost:7475 (Faraday::ConnectionFailed)
erecura:neotest jamesprior$     from /Users/jamesprior/.gem/ruby/2.1.2/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:589:in `connection_for'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/adapter/net_http_persistent.rb:26:in `perform_request'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/adapter/net_http.rb:40:in `block in call'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/adapter/net_http_persistent.rb:22:in `with_net_http_connection'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/adapter/net_http.rb:32:in `call'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday_middleware-0.9.2/lib/faraday_middleware/response_middleware.rb:30:in `call'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday_middleware-0.9.2/lib/faraday_middleware/request/encode_json.rb:23:in `call'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/rack_builder.rb:139:in `build_response'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/connection.rb:377:in `run_request'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/faraday-0.9.1/lib/faraday/connection.rb:140:in `get'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-core-5.1.5/lib/neo4j-server/cypher_session.rb:50:in `open'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-core-5.1.5/lib/neo4j-server/cypher_session.rb:6:in `block in <module:Server>'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-core-5.1.5/lib/neo4j/session.rb:120:in `call'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-core-5.1.5/lib/neo4j/session.rb:120:in `create_session'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-core-5.1.5/lib/neo4j/session.rb:105:in `open'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-5.2.7/lib/neo4j/railtie.rb:69:in `open_neo4j_session'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-5.2.7/lib/neo4j/railtie.rb:102:in `block (2 levels) in <class:Railtie>'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-5.2.7/lib/neo4j/railtie.rb:101:in `each'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/neo4j-5.2.7/lib/neo4j/railtie.rb:101:in `block in <class:Railtie>'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/railties-4.1.7/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/railties-4.1.7/lib/rails/initializable.rb:30:in `run'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/railties-4.1.7/lib/rails/initializable.rb:55:in `block in run_initializers'

At this point I noticed that a lot of the rake tasks had definitions like task :install, :edition, :environment do |_, args| and I thought to check what version of Rake I had, in case something changed and it was trying to invoke those as dependent tasks. It was there that I got an error running bin/rake --version and sure enough, at the bottom of the trace I found this:

    from /Users/jamesprior/Development/neotest/config/environment.rb:5:in `<top (required)>'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:92:in `require'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:92:in `preload'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:143:in `serve'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:131:in `block in run'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:125:in `loop'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application.rb:125:in `run'
    from /Users/jamesprior/.gem/ruby/2.1.2/gems/spring-1.4.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/jamesprior/.rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/jamesprior/.rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

So what I take from this is that the app will fail to boot if it can't connect, and that using binstubs with spring will always boot the app even if the rake task doesn't call for it. I don't think that means anything to fix in this project. Do you think it makes sense to note this in another project or change the boot process?

@cheerfulstoic
Copy link
Contributor

I just realized that a number of the links on neo4jrb.io are pointing to our old documentation. The new docs are here: http://neo4jrb.readthedocs.org/

I've just fixed that now. The new documentation has a pretty good section which covers setup in Rails:

http://neo4jrb.readthedocs.org/en/stable/Setup.html#ruby-on-rails

It talks about adding the require 'neo4j/railtie' for existing applications, but there is also a line you can run to setup a new Rails app with a number of things already in place:

rails new myapp -m http://neo4jrb.io/neo4j/neo4j.rb -O

As to your problem: I was actually briefly fooled by the :environment bit myself. I think you understood it, but to make sure: That's one of the arguments, not the dependent task which would load the environment.

And yeah, that is really puzzling... I have an app that I just tested with and rake --version and rake neo4j:info worked just fine and ran very quickly so I don't think they loaded the Rails environment. I normally rip "spring" out (generally because I will load the neo4j.rb gems via path in my Gemfile and spring doesn't reload them). I put it back in and that didn't seem to break anything.

Everything that you wrote seems fine, but there's one small thing which might be wrong that I would suggest looking at: You said you added require 'neo4j/railtie' at the top of your config/application.rb. It should actually go below the require 'rails/all' (or whatever requires are there) above the Bundler.require line. Could you try moving that? The special rails new command above should also take care of that for you.

Small note: If you don't plan on using ActiveRecord then keep in mind that require 'rails/all' will load it. You can load just the parts of Rails you want like this if you want to keep ActiveRecord out of memory:

require "active_model/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require 'neo4j/railtie'
require "rails/test_unit/railtie"

@jamesprior
Copy link
Author

I reviewed the config/application.rb file and it looks like this:

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'neo4j/railtie'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

so I think the requires are in the right order. Just to confirm, when you springify an app are you invoking rake with the binstubs in the bin directory? With spring installed here are the working and not working commands side by side:

erecura:neotest jamesprior$ rake --version
rake, version 10.4.2
erecura:neotest jamesprior$ bin/rake --version
/Users/jamesprior/.gem/ruby/2.1.2/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:641:in `rescue in connection_for': connection refused: localhost:7475 (Faraday::ConnectionFailed)

Actually, looking at bin/rake it may not even be spring, here's the contents:

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require_relative '../config/boot'
require 'rake'
Rake.application.run

I'm satisfied that this is a setup issue in my environment. If someone brings it up again then at least we'll know why. Thanks for the support and the tips on involving it, FWIW my primary use now is as a backup DB for recommendations so I'm happy to have ActiveRecord in there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants