lib/neo4j/railtie.rb in neo4j-7.2.3 vs lib/neo4j/railtie.rb in neo4j-8.0.0.alpha.1

- old
+ new

@@ -1,9 +1,13 @@ require 'active_support/notifications' require 'rails/railtie' +require 'neo4j/session_manager' # Need the action_dispatch railtie to have action_dispatch.rescue_responses initialized correctly require 'action_dispatch/railtie' +require 'neo4j/core/cypher_session/adaptors/http' +require 'neo4j/core/cypher_session/adaptors/bolt' +require 'neo4j/core/cypher_session/adaptors/embedded' module Neo4j class Railtie < ::Rails::Railtie config.neo4j = ActiveSupport::OrderedOptions.new @@ -37,133 +41,48 @@ rake_tasks do load 'neo4j/tasks/migration.rake' end - class << self - def java_platform? - RUBY_PLATFORM =~ /java/ - end + console do |app| + Neo4j::Config[:logger] = ActiveSupport::Logger.new(STDOUT) - def setup_default_session(cfg) - setup_config_defaults!(cfg) + register_neo4j_cypher_logging(app.config.neo4j.sessions.map { |s| s[:type] }) + end - return if !cfg.sessions.empty? + # Starting Neo after :load_config_initializers allows apps to + # register migrations in config/initializers + initializer 'neo4j.start', after: :load_config_initializers do |app| + cfg = app.config.neo4j + # Set Rails specific defaults + Neo4j::SessionManager.setup! cfg - cfg.sessions << {type: cfg.session_type, path: cfg.session_path, options: cfg.session_options.merge(default: true)} - end + Neo4j::Config[:logger] ||= Rails.logger - def setup_config_defaults!(cfg) - cfg.session_type ||= default_session_type - cfg.session_path ||= default_session_path - cfg.session_options ||= {} - cfg.sessions ||= [] - end + session_types = cfg.sessions.map { |session_opts| session_opts[:type] } - def config_data - @config_data ||= if yaml_path - HashWithIndifferentAccess.new(YAML.load(ERB.new(yaml_path.read).result)[Rails.env]) - else - {} - end - end - - def yaml_path - @yaml_path ||= %w(config/neo4j.yml config/neo4j.yaml).map do |path| - Rails.root.join(path) - end.detect(&:exist?) - end - - def default_session_type - type = ENV['NEO4J_TYPE'] || config_data[:type] || :server_db - type.to_sym - end - - def default_session_path - ENV['NEO4J_URL'] || ENV['NEO4J_PATH'] || - config_data[:url] || config_data[:path] || - 'http://localhost:7474' - end - - def start_embedded_session(session) - # See https://github.com/jruby/jruby/wiki/UnlimitedStrengthCrypto - security_class = java.lang.Class.for_name('javax.crypto.JceSecurity') - restricted_field = security_class.get_declared_field('isRestricted') - restricted_field.accessible = true - restricted_field.set nil, false - session.start - end - - def open_neo4j_session(options, wait_for_connection = false) - type, name, default, path = options.values_at(:type, :name, :default, :path) - - if !java_platform? && type == :embedded_db - fail "Tried to start embedded Neo4j db without using JRuby (got #{RUBY_PLATFORM}), please run `rvm jruby`" - end - - session = wait_for_value(wait_for_connection) do - if options.key?(:name) - Neo4j::Session.open_named(type, name, default, path) - else - Neo4j::Session.open(type, path, options[:options]) - end - end - - start_embedded_session(session) if type == :embedded_db - end + register_neo4j_cypher_logging(session_types) end - def wait_for_value(wait) - session = nil - Timeout.timeout(60) do - until session - begin - if session = yield - puts - return session - end - rescue Faraday::ConnectionFailed => e - raise e if !wait + TYPE_SUBSCRIBERS = { + http: Neo4j::Core::CypherSession::Adaptors::HTTP.method(:subscribe_to_request), + bolt: Neo4j::Core::CypherSession::Adaptors::Bolt.method(:subscribe_to_request), + embedded: Neo4j::Core::CypherSession::Adaptors::Embedded.method(:subscribe_to_transaction) + } - putc '.' - sleep(1) - end - end - end - end - - def register_neo4j_cypher_logging + def register_neo4j_cypher_logging(session_types) return if @neo4j_cypher_logging_registered Neo4j::Core::Query.pretty_cypher = Neo4j::Config[:pretty_logged_cypher_queries] - Neo4j::Server::CypherSession.log_with do |message| - (Neo4j::Config[:logger] || Rails.logger).debug message + logger_proc = ->(message) do + (Neo4j::Config[:logger] ||= Rails.logger).debug message end - - @neo4j_cypher_logging_registered = true - end - - console do - Neo4j::Config[:logger] = ActiveSupport::Logger.new(STDOUT) - - register_neo4j_cypher_logging - end - - # Starting Neo after :load_config_initializers allows apps to - # register migrations in config/initializers - initializer 'neo4j.start', after: :load_config_initializers do |app| - cfg = app.config.neo4j - # Set Rails specific defaults - Neo4j::Railtie.setup_default_session(cfg) - - cfg.sessions.each do |session_opts| - Neo4j::Railtie.open_neo4j_session(session_opts, cfg.wait_for_connection) + Neo4j::Core::CypherSession::Adaptors::Base.subscribe_to_query(&logger_proc) + session_types.map(&:to_sym).uniq.each do |type| + TYPE_SUBSCRIBERS[type].call(&logger_proc) end - Neo4j::Config.configuration.merge!(cfg.to_hash) - Neo4j::Config[:logger] ||= Rails.logger - - register_neo4j_cypher_logging + @neo4j_cypher_logging_registered = true end end end