Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support to access the rails console with write access at the sa…

…me time as the rails application is running #99

Looks like it works !
You need to include the neo4j-enterprise and neo4j-advanced gem as development deps (see license)
Then you include the ha_console railitie in your application.rb.
This is all that is needed to get the rails console working using a Neo4j HA cluster.
  • Loading branch information...
commit de0353723490bb96332cf8bcc64a3d7be8719af4 1 parent c44ad69
Andreas Ronge andreasronge authored
3  Gemfile
View
@@ -2,9 +2,6 @@ source :gemcutter
gemspec
-gem 'neo4j-core', :path => '/home/andreas/projects/neo4j-core'
-gem 'neo4j-wrapper', :path => '/home/andreas/projects/neo4j-wrapper'
-
group 'development' do
gem 'guard'
gem 'ruby_gntp', :require => false # GrowlNotify for Mac
2  example/ha-cluster/Gemfile
View
@@ -1,7 +1,5 @@
source 'http://rubygems.org'
-gem 'neo4j-core', :path => '/home/andreas/projects/neo4j-core'
-gem 'neo4j-wrapper', :path => '/home/andreas/projects/neo4j-wrapper'
gem 'neo4j', :path => "../.."
gem 'neo4j-advanced'
gem 'neo4j-enterprise'
1  example/ha-cluster/clean.sh
View
@@ -3,3 +3,4 @@
rm -rf data/zookeeper1/version-2
rm -rf data/zookeeper2/version-2
rm -rf data/zookeeper3/version-2
+rm -rf db
0  lib/neo4j/rails/ha_console.rb
View
No changes.
68 lib/neo4j/rails/ha_console/ha_console.rb
View
@@ -0,0 +1,68 @@
+require 'neo4j-enterprise'
+require 'fileutils'
+require 'tmpdir'
+
+module Neo4j
+
+ module Rails
+
+ # Configures Neo4j HA and Zookeeper in order to be used from a rails console
+ # @see Railtie
+ module HaConsole
+ class << self
+ def machine_id
+ (defined? IRB) ? 2 : 1
+ end
+
+ def proj_root
+ Object::Rails.root
+ end
+
+ def storage_path(id = machine_id)
+ File.expand_path("db/ha_neo_#{id}", proj_root)
+ end
+
+ def config_machine(id = machine_id)
+ puts "config_machine #{id}"
+ # override this default config with this machine configuration
+ Neo4j.config['ha.db'] = true
+ Neo4j.config['ha.server_id'] = id
+ Neo4j.config['ha.server'] = "localhost:600#{machine_id}"
+ Neo4j.config['ha.pull_interval'] = "2"
+ Neo4j.config[:storage_path] = storage_path(id)
+
+ copy_config unless File.exist?(config_dir)
+ require "#{config_dir}/zookeeper"
+ end
+
+ def config_dir
+ File.expand_path("neo4j_ha_console/zookeeper", Dir.tmpdir)
+ end
+
+ def copy_config
+ source_dir = File.expand_path("zookeeper", File.dirname(__FILE__))
+ system("mkdir -p #{File.expand_path("..", config_dir)}")
+ system("cp -r #{source_dir} #{config_dir}")
+ end
+
+ def zookeeper_running?
+ Zookeeper.pid_file?
+ end
+
+ def start_zookeeper
+ Zookeeper.start unless zookeeper_running?
+ end
+
+ def shutdown_zookeeper
+ if zookeeper_running?
+ Zookeeper.shutdown
+ else
+ puts "Can't shutdown zookeeper - no PID file found for zookeeper process at #{Zookeeper.pid_file}"
+ end
+ end
+
+ end
+ end
+ end
+end
+
21 lib/neo4j/rails/ha_console/railtie.rb
View
@@ -0,0 +1,21 @@
+require File.expand_path('../ha_console', __FILE__)
+
+module Neo4j
+
+ module Rails
+
+ module HaConsole
+
+ # Include this in your config/application.rb in order to run a rails console
+ # It avoids the Neo4j limitation of only having one process accessing the database by using HA clustering/neo4j-enterprise
+ class Railtie < Object::Rails::Railtie
+ config.before_configuration do
+ Neo4j::Rails::HaConsole.config_machine
+ Neo4j::Rails::HaConsole.start_zookeeper
+ config.neo4j.storage_path = Neo4j::Rails::HaConsole.storage_path
+ puts "HA: #{Neo4j.config['ha.db']}, server_id: #{Neo4j.config['ha.server_id']}, master: #{Neo4j.ha_master?}, storage_path=#{config.neo4j.storage_path}"
+ end
+ end
+ end
+ end
+end
4 lib/neo4j/rails/ha_console/zookeeper/clean.sh
View
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+rm -rf db
+rm -rf data/zookeeper1/version-2
11 lib/neo4j/rails/ha_console/zookeeper/conf/server1.cfg
View
@@ -0,0 +1,11 @@
+#server1.cfg
+tickTime=2000
+initLimit=10
+syncLimit=5
+
+dataDir=data/zookeeper1
+clientPort=2181
+
+server.1=localhost:2888:3888
+#server.2=localhost:2889:3889
+#server.3=localhost:2890:3890
1  lib/neo4j/rails/ha_console/zookeeper/data/zookeeper1/myid
View
@@ -0,0 +1 @@
+1
17 lib/neo4j/rails/ha_console/zookeeper/start_zookeeper.sh
View
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# Only follow symlinks if readlink supports it
+if readlink -f "$0" > /dev/null 2>&1
+then
+ ZOOBIN=`readlink -f "$0"`
+else
+ ZOOBIN="$0"
+fi
+
+ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
+
+ZOOBINDIR=`dirname "$ZOOBIN"`
+JARDIR=`cd "${ZOOBINDIR}/lib"; pwd`
+
+CLASSPATH=$JARDIR/log4j-1.2.16.jar:$JARDIR/zookeeper-3.3.2.jar
+java -cp "$CLASSPATH" $ZOOMAIN conf/server1.cfg
62 lib/neo4j/rails/ha_console/zookeeper/zookeeper.rb
View
@@ -0,0 +1,62 @@
+module Neo4j
+ module Rails
+ module HaConsole
+ module Zookeeper
+ class << self
+ def shutdown
+ return unless pid_file?
+ file = File.open(pid_file, "rb")
+ pid = file.read.to_s.chomp
+ IO.popen("pkill -TERM -P #{pid}")
+ puts "Zookeeper: shutdown #{pid}"
+ system("rm -f #{pid_file}")
+ end
+
+ def clean
+ dir = File.expand_path("data/zookeeper1/version-2", File.dirname(__FILE__))
+ puts "Zookeeper: clean #{dir}"
+ system("rm -rf #{dir}")
+ end
+
+ def pid_file
+ File.expand_path("zookeeper.pid", File.dirname(__FILE__)).to_s
+ end
+
+ def pid_file?
+ x = File.exist?(pid_file)
+ puts "Zookeeper: pid file #{x} at #{pid_file}"
+ x
+ end
+
+ def lib_dir
+ File.expand_path("lib", File.dirname(__FILE__))
+ end
+
+ def copy_jars
+ puts "Zookeeper: Copy JARs"
+ files = $CLASSPATH.find_all{|x| x =~ /\.jar$/}.collect{|y| y.sub('file:', '')}
+ zookeeper = files.find{|f| f =~ /\/zookeeper/}
+ raise "zookeper JAR not found in a GEM, did you forget to include neo4j-enterprise in your Gemfile (development)" unless zookeeper
+
+ log4j = files.find{|f| f =~ /\/log4j/}
+ raise "log4j not found in a GEM, did you forget to include neo4j-enterprise in your Gemfile (development)" unless log4j
+
+
+ system("mkdir -p #{lib_dir}")
+ FileUtils.cp(zookeeper, lib_dir)
+ FileUtils.cp(log4j, lib_dir)
+ end
+
+ def start
+ puts "Zookeeper: start, check jars?"
+ copy_jars unless File.exist?(lib_dir)
+ zookeeper_exec = File.expand_path("start_zookeeper.sh", File.dirname(__FILE__)).to_s
+ Dir.chdir(File.dirname(__FILE__))
+ pipe = IO.popen(zookeeper_exec)
+ File.open(pid_file, 'w') { |f| f.write(pipe.pid) }
+ end
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.