Created
June 1, 2019 14:20
-
-
Save DenisKem/bd71eb6caaf666c58397f0c25dabf364 to your computer and use it in GitHub Desktop.
Demo ruby script. Catching signals and simulate unexpected failure
This file contains hidden or 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
# frozen_string_literal: true | |
seconds = 0.0 | |
STEP = 0.5 | |
OUTPUTFILE = "/home/denis/code/mad/signals/o.log" | |
BAD_FILE = "/home/denis/code/mad/signals/bad.txt" | |
def say(message) | |
File.open(OUTPUTFILE, 'a') { |file| file.puts message } | |
end | |
def detect_bad_file | |
if File.exist? BAD_FILE | |
say("Detected bad file") | |
File.delete(BAD_FILE) | |
raise | |
end | |
end | |
say("") | |
say("Welcome!") | |
say("") | |
at_exit do | |
say("Closing with #{$!} ...") | |
sleep STEP | |
end | |
Signal.trap("INT") { exit } | |
loop do | |
detect_bad_file | |
say "Seconds: #{seconds += STEP}" | |
sleep STEP | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment