Created
March 14, 2012 17:36
-
-
Save marcinbunsch/2038115 to your computer and use it in GitHub Desktop.
Gracefult quit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "singleton" | |
class GracefulQuit | |
include Singleton | |
attr_accessor :breaker | |
def initialize | |
self.breaker = false | |
end | |
def self.enable | |
trap('INT') { | |
# force-quit on double ctrl-c | |
if self.instance.breaker | |
puts "Exiting" | |
exit | |
end | |
yield if block_given? | |
self.instance.breaker = true | |
} | |
end | |
def self.check(message = "Quitting") | |
if self.instance.breaker | |
yield if block_given? | |
puts message | |
exit | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment