lib/oria.rb in oria-0.0.3 vs lib/oria.rb in oria-0.1.0

- old
+ new

@@ -1,10 +1,11 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) require 'rubygems' require 'eventmachine' require 'json' require 'oria/errors' +require 'tmpdir' module Oria autoload(:Client, 'oria/client') autoload(:Server, 'oria/server') @@ -23,38 +24,29 @@ def self.app_key=(key) @@app_key = key.to_s end - def self.auto_start - @@pid = fork do - Oria::Server.start(Oria.server, Oria.port, Oria.app_key) - end - Process.detach(@@pid) - at_exit { - Process.kill("HUP", @@pid) - } - sleep 1 - end - def self.clear store(:delete) end - def self.connect(server, port = nil, options = {}) - @@server = server + def self.connect(server = Oria.server, port = Oria.port, debug = false) + @@server = server.to_s @@port = port.to_i unless port.to_i == 0 + unless Oria::Server.running? + # That's right. Boot Oria on the command line so we don't bother forking or any + # of that other troublesome nonsense. It's smart enough to maintain its own + # status and start/stop/restart itself. + system("./bin/oria start -d -h #{@@server} -p #{@@port}") + end end def self.delete(key) store(:delete, :key => key) end - def self.disconnect - Oria::Server.stop - end - def self.has_key?(key) key?(key) end def self.key?(key) @@ -88,9 +80,15 @@ def self.store(method, args = {}) args.merge!(:app_key => Oria.app_key) args = JSON.generate(args) do_store(method, args) rescue Oria::ConnectionError - auto_start + # Thanks to ActiveSupport for the "silence_stream" functionality - we're quieting STDOUT + # at autoconnect time. + old_stream = STDOUT.dup + STDOUT.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') + STDOUT.sync = true + connect + STDOUT.reopen(old_stream) do_store(method, args) end end \ No newline at end of file