Skip to content

Instantly share code, notes, and snippets.

@DenisKem
Created June 1, 2019 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisKem/bd71eb6caaf666c58397f0c25dabf364 to your computer and use it in GitHub Desktop.
Save DenisKem/bd71eb6caaf666c58397f0c25dabf364 to your computer and use it in GitHub Desktop.
Demo ruby script. Catching signals and simulate unexpected failure
# 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