Created
November 27, 2012 08:38
-
-
Save 3dd13/4153156 to your computer and use it in GitHub Desktop.
Runing single ruby file on heroku
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
source 'https://rubygems.org' |
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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'net/smtp' | |
MAILING_LIST = ['[email protected]', '[email protected]'] | |
def ping_api_1 | |
post_check('http://example.com/resources') | |
end | |
def ping_api_2 | |
get_check('http://example.com/resources') | |
end | |
def get_check(url) | |
begin | |
res = Net::HTTP.get_response(URI(url)) | |
if res.code == "200" | |
puts "alive #{url}" | |
else | |
send_message("Something wrong with #{url}") | |
end | |
rescue => e | |
send_message("Something wrong with #{url} #{e.inspect}") | |
end | |
end | |
def post_check(url) | |
# post something ... | |
end | |
def full_message(msg) | |
<<MESSAGE_END | |
From: Bot Monitor <[email protected]> | |
To: Developers | |
MIME-Version: 1.0 | |
Content-type: text/html | |
Subject: #{msg} | |
<h2>Error</h2> | |
<br/> | |
#{msg}<br/> | |
<br/> | |
MESSAGE_END | |
end | |
def send_message(msg) | |
smtp = Net::SMTP.new 'smtp.gmail.com', 587 | |
smtp.enable_starttls | |
smtp.start("gmail.com", "[email protected]", 'your_password', :login) do |smtp| | |
smtp.send_message(full_message(msg), "[email protected]", MAILING_LIST) | |
end | |
end | |
puts "Start scanning at #{Time.now}" | |
threads = [] | |
threads << Thread.new { ping_api_1 } | |
threads << Thread.new { ping_api_2 } | |
threads.each do |t| | |
t.join | |
end | |
puts "Complete scanning at #{Time.now}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment