namespace :n4j do desc "Create n4j.yml" task :create_n4j_config do require 'psych' conf_file = Psych.dump({'development' => {'port' => 7480, 'secure_port' => 7481}, 'test' => {'port' => 7481, 'secure_port' => 7483}}) FileUtils.mkdir_p 'config' File.open('config/n4j.yml', 'w') {|f| f.write(conf_file) } end desc "Clear development database." task :clear_development do port = YAML.parse_file('config/n4j.yml').to_ruby['development']['port'] result = `curl --request DELETE http://localhost:#{port}/cleandb/all-gone` puts result end desc "Status of Neo4j servers" task :status do number_of_neo4j_instances = `ps aux | grep ne[o]4j | wc -l` number_of_neo4j_instances.strip! puts "#{number_of_neo4j_instances} total instances of Neo4j running, system wide." unless number_of_neo4j_instances.to_i < 1 YAML.parse_file("config/n4j.yml").to_ruby.each_pair do |environment, conf| pid_filename = "tmp/pids/n4j_#{environment}_pid" if File.exist?(pid_filename) pid = IO.read "tmp/pids/n4j_#{environment}_pid" pid.chomp! result = `ps p#{pid} | grep #{pid} | wc -l` result.strip! puts "#{result} instances running for #{environment} (pid: #{pid})." end end end end desc 'Start servers' task :start do |task_name| require 'erb' YAML.parse_file('config/n4j.yml').to_ruby.each_pair do |environment, conf| @environment = environment @port = conf['port'] @secure_port = conf['secure_port'] @server_version = 1.6 neo4j_bin_link = `which neo4j` neo4j_bin_link.chomp! neo4j_bin = Pathname.new(neo4j_bin_link).realpath java_command = `which java` java_command.chomp! libexec = Pathname.new(neo4j_bin_link).realpath.dirname.dirname libdir = "#{libexec}/lib" syslib = "#{libexec}/system/lib" plugin_dir = "#{libexec}/plugins" n4j_plugins = 'neo4j_plugins' neo4j_home = "tmp/n4j/#{@environment}" pid_file = "tmp/pids/n4j_#{@environment}_pid" FileUtils.mkdir_p neo4j_home FileUtils.mkdir_p "#{neo4j_home}/data/log" FileUtils.mkdir_p 'tmp/pids' class_path = [libdir, syslib, plugin_dir, n4j_plugins].collect {|dir| Dir.glob(dir + '/*.jar')}.flatten.join(':') java_opts = "-server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Xms3m -Xmx64m" # system "cp lib/n4j/templates/neo4j.properties tmp/n4j/#{@environment}/" # system "cp lib/n4j/templates/logging.properties tmp/n4j/#{@environment}/" FileUtils.cp 'templates/neo4j.properties', "tmp/n4j/#{@environment}/neo4j.properties" FileUtils.cp 'templates/logging.properties', "tmp/n4j/#{@environment}/logging.properties" FileUtils.touch "#{neo4j_home}/data/log/console.log" neo4j_server_properties = ERB.new(IO.read("templates/neo4j-server-#{@server_version}.properties")).result neo4j_server_properties_path = "#{neo4j_home}/neo4j-server.properties" File.open(neo4j_server_properties_path, 'w') {|f| f.write(neo4j_server_properties) } neo4j_wrapper_conf = ERB.new(IO.read('templates/neo4j-wrapper.conf')).result neo4j_wrapper_conf_path = "#{neo4j_home}/neo4j-wrapper.conf" File.open(neo4j_wrapper_conf_path, 'w') {|f| f.write(neo4j_wrapper_conf) } launch_neo4j_command = "#{java_command} -cp #{class_path} -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=#{neo4j_server_properties_path} -Djava.util.logging.config.file=#{neo4j_home}/logging.properties -Xms3m -Xmx64m -Dlog4j.configuration=file:#{neo4j_home}/log4j.properties -Dorg.neo4j.server.properties=#{neo4j_server_properties_path} -Djava.util.logging.config.file=#{neo4j_home}/logging.properties -Dneo4j.home=#{neo4j_home} -Dneo4j.instance=#{libexec} org.neo4j.server.Bootstrapper >> #{neo4j_home}/data/log/console.log 2>&1 & echo $! > \"#{pid_file}\" " system launch_neo4j_command.gsub(/\n/,' ').gsub(/\s+/, ' ') puts "#{environment.capitalize} Neo4j launched." end end desc "Stop server" task :stop do YAML.parse_file('config/n4j.yml').to_ruby.each_pair do |environment, conf| pid_filename = "tmp/pids/n4j_#{environment}_pid" if File.exist?(pid_filename) pid = IO.read pid_filename pid.chomp! result = `ps p#{pid} | grep #{pid} | wc -l` if result.to_i > 0 Process.kill("HUP", pid.to_i) puts "Killed #{environment} Neo4j." else puts "#{environment.capitalize} Neo4j wasn't running." end end end end end