lib/oria/server.rb in oria-0.0.1 vs lib/oria/server.rb in oria-0.0.2

- old
+ new

@@ -11,11 +11,17 @@ require 'logger' @@logger ||= Logger.new(log_file, 0, 100 * 1024 * 1024) end def start(server, port, app_key = nil, debug = false) - @@pid = Process.pid + at_exit do + Oria::Server.stop + end + pid = Process.pid + File.open(pid_file, 'w') do |file| + file.puts pid + end app_key ||= 'default' @@servers ||= if File.exists?(yaml_store) YAML.load_file(yaml_store) else {} @@ -26,11 +32,16 @@ EventMachine.start_server server, port, Oria::Server end end def stop - Process.kill('HUP', @@pid) if defined?(@@pid) + if File.exists?(pid_file) + if pid + Process.kill(9, pid) + end + File.unlink(pid_file) + end end def write_hash @@write_hash ||= proc do File.open(yaml_store, 'w') do |store| @@ -40,9 +51,17 @@ end protected def log_file @@log_file ||= File.join(Dir.tmpdir, 'oria.log') + end + + def pid + @@pid ||= File.read(pid_file).to_i if File.exists?(pid_file) + end + + def pid_file + @@pid_file ||= File.join(Dir.tmpdir, 'oria.pid') end def yaml_store @@yaml_store ||= File.join(Dir.tmpdir, 'oria.yml') end \ No newline at end of file