Created
December 9, 2020 22:00
-
-
Save kumo/24780b3c27021f85de23837915678bc6 to your computer and use it in GitHub Desktop.
Ruby version of a simple monitoring web app
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 'sinatra' | |
get '/hello' do | |
# Update the 'last modified' time of a specific file to the current time | |
FileUtils.touch('status') | |
"Hello to you too!" | |
end | |
get '/check/:minutes' do |minutes| | |
# Get the last modified time of a specific file in seconds | |
last_modified = File.mtime('status') | |
# Get the current time in seconds | |
now = Time.now | |
# Calculate how much time has passed | |
difference = now - last_modified | |
# Convert the minutes into seconds | |
seconds = minutes.to_i * 60 | |
# Firstly we check if there has been a hello in the last X minutes. | |
# If there hasn't we check if the last hello was in the last 2*X minutes, | |
# if so, we can send a Telegram message saying that something has gone wrong. | |
# If the last hello was more than 2*x minutes ago, then it is still offline. | |
if difference < seconds | |
"It has said hello recently" | |
elsif difference < seconds * 2 | |
"Offline and I should send a Telegram message" | |
else | |
"Still offline. Should I send a message?" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment