Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Yoomee/juggernaut
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Atkins committed Mar 15, 2011
2 parents 10ce2c5 + 46de416 commit 38bf90b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/juggernaut/redis.js
@@ -1,7 +1,7 @@
var url = require("url");
var redis = require("redis-client");

redis.DEFAULT_PORT = 6380;
// redis.DEFAULT_PORT = 6380;

module.exports.createClient = function(){
if (process.env.REDISTOGO_URL) {
Expand Down
47 changes: 42 additions & 5 deletions lib/start.rb
@@ -1,7 +1,44 @@
if File.exists?("node.pid")
puts "Already runnning with pid #{File.read("node.pid")}"
def get_pid(command)
%x{ps -ef | grep \"#{command}\"}.split("\n").each do |line|
if !line.match(/grep/) && res = line.strip.match(/^\S*\s+(\d+)\s/)
return res[1]
end
end
nil
end

def get_child_process(ppid)
%x{ps -ef | grep #{ppid}}.split("\n").each do |line|
if !line.match(/grep/) && res = line.strip.match(/^.*\s+(\d+)\s+#{ppid}\s/)
return res[1]
end
end
nil
end

def pid_file_path
"/var/run/juggernaut.pid"
end

def root_path
File.dirname(__FILE__) + "/../"
end

def delete_pid_file
%x{rm #{pid_file_path}} if File.exists?(pid_file_path)
end

if pid = get_pid("node server.js")
File.open(pid_file_path, 'w') {|f| f.write(pid)}
puts "Already runnning with pid #{pid}."
else
pid = IO.popen("node server.js &> node.log").pid
File.open("node.pid", 'w') {|f| f.write(pid)}
puts "Started node server with pid #{pid}."
delete_pid_file
%x{mkdir -p /var/run}
ppid = IO.popen("cd #{root_path} && node server.js &> /var/log/node.log").pid
if (pid = get_child_process(ppid))
File.open(pid_file_path, 'w') {|f| f.write(pid)}
puts "Started node server with pid #{pid}."
else
puts "Failed to start node server."
end
end
33 changes: 25 additions & 8 deletions lib/stop.rb
@@ -1,10 +1,27 @@
pid = File.read("node.pid")
%x{ps -ef | grep #{pid}}.split("\n").each do |line|
if !line.match(/grep/) && res = line.match(/.*\s(\d+)\s#{pid}\s/)
cpid = res[1]
%x{kill #{cpid}}
def get_pids(command)
pids = []
%x{ps -ef | grep \"#{command}\"}.split("\n").each do |line|
if !line.match(/grep/) && res = line.strip.match(/^\S*\s+(\d+)\s/)
pids << res[1]
end
end
pids
end
%x{kill #{pid}}
%x{rm node.pid}
puts "Stopped node server."

def pid_file_path
"/var/run/juggernaut.pid"
end

def delete_pid_file
%x{rm #{pid_file_path}} if File.exists?(pid_file_path)
end

(pids = get_pids("node server.js")).each do |pid|
%x{kill #{pid}}
end
if pids.empty?
puts "node server isn't running."
else
puts "Stopped node server."
end
delete_pid_file

0 comments on commit 38bf90b

Please sign in to comment.