Last active
December 19, 2015 21:08
-
-
Save saicologic/6017638 to your computer and use it in GitHub Desktop.
Ruby Timers Sample Program
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 | |
# -*- coding: utf-8 -*- | |
require 'timers' | |
timers = Timers.new | |
logs = [] | |
logs << [] | |
logs << [] | |
def write_line(num) | |
puts '-' * num | |
end | |
def alert(histories, index, &block) | |
line_number = 50 | |
write_line(line_number) | |
puts "User#{index+1} Log Report" | |
block.call(histories[index]) | |
puts 'log histories:' | |
puts histories[index] | |
write_line(line_number) | |
end | |
log_generator_timer = timers.every(1) { | |
# Create random trouble errors | |
take_number = rand(0..2) | |
puts "Take number: #{take_number}" | |
user1_alert_message = 'SUCCESS' | |
user2_alert_message = 'SUCCESS' | |
if take_number == 1 | |
user1_alert_message = 'ERROR' | |
end | |
if take_number == 2 | |
user2_alert_message = 'ERROR' | |
end | |
logs[0] << "#{Time.now} user1 #{user1_alert_message}" | |
logs[1] << "#{Time.now} user2 #{user2_alert_message}" | |
} | |
evaludate_function = Proc.new {|histories| | |
error_count = 0 | |
histories.each do |message| | |
if /ERROR/ =~ message | |
error_count +=1 | |
end | |
end | |
puts "#{histories.length - error_count} SUCCESS" | |
puts "#{error_count} ERRORS" | |
} | |
user1_timer = timers.every(5) { | |
user_id = 0 | |
alert(logs, user_id) {|histories| | |
evaludate_function.call(histories) | |
} | |
logs[user_id] = [] | |
} | |
user2_timer = timers.every(10) { | |
user_id = 1 | |
alert(logs, user_id) {|histories| | |
evaludate_function.call(histories) | |
} | |
logs[user_id] = [] | |
} | |
loop { timers.wait } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment