public
Last active

Tokyo Tyrant rake tasks

  • Download Gist
tyrant.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#
# Tokyo Tyrant rake tasks
#
# rake tyrant:restart # Restart Tyrant server
# rake tyrant:start # Start Tyrant server
# rake tyrant:stop # Stop Tyrant server
#
namespace :tyrant do
 
desc "Start Tyrant server"
task :start do
raise RuntimeError, "Tyrant is already running." if tyrant_running?
system "ttserver -pid #{tyrant_pid_file} -log #{tyrant_log_file} #{tyrant_db_file}&"
sleep(2)
if tyrant_running?
puts "Tyrant started successfully (pid #{tyrant_pid})."
else
puts "Failed to start tyrant push server. Check logs."
end
end
 
desc "Stop Tyrant server"
task :stop do
raise RuntimeError, "Tyrant isn't running." unless tyrant_running?
system "kill #{tyrant_pid}"
sleep(2)
if tyrant_running?
puts "Tyrant didn't stopped. Check the logs."
else
puts "Tyrant stopped."
end
end
 
desc "Restart Tyrant server"
task :restart => [:stop, :start]
 
end
 
def tyrant_db_file
File.join(RAILS_ROOT, "db", "tyrant.tch")
end
 
def tyrant_pid_file
File.join(RAILS_ROOT, "tmp", "pids", "tyrant.pid")
end
 
def tyrant_log_file
File.join(RAILS_ROOT, "log", "tyrant.log")
end
 
def tyrant_pid
`cat #{tyrant_pid_file}`.to_i
end
 
def tyrant_running?
if File.exist?(tyrant_pid_file)
process_check = `ps -p #{tyrant_pid} | wc -l`
tyrant_pid && process_check.to_i > 1
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.