Skip to content

Instantly share code, notes, and snippets.

@ksk5280
Created January 6, 2016 07:11
Show Gist options
  • Save ksk5280/9c56da9a392f09421896 to your computer and use it in GitHub Desktop.
Save ksk5280/9c56da9a392f09421896 to your computer and use it in GitHub Desktop.
class ServerManager
attr_reader :path, :thread, :pid
def initialize(path)
@path = path
end
def run
return if running?
@pid = Process.spawn "ruby", path
end
def kill
return unless running?
Process.kill :INT, pid
ensure
@pid = nil
end
def running?
pid && Process.getpgid(pid)
rescue Errno::ESRCH
puts "\e[36mNOT RUNNING\e[0m"
return false
end
end
def title(title)
puts "", "", "\e[34m===== #{title} =====\e[39m"
end
path = File.expand_path("server.rb", __dir__)
manager = ServerManager.new(path)
title "STARTING"
manager.run
sleep 2
title "REQUEST SHOULD WORK"
system "curl localhost:9292/hello"
title "KILLING"
manager.kill
title "REQUEST SHOULD NOT WORK"
system "curl localhost:9292/hello"
title "STARTING"
manager.run
sleep 2
title "SHUTTING IT DOWN"
system "curl localhost:9292/shutdown"
title "REQUEST SHOULD NOT WORK"
system "curl localhost:9292/hello"
title "STARTING"
manager.run
sleep 2
title "REQUEST SHOULD WORK"
system "curl localhost:9292/hello"
manager.kill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment