Commit d155fa25b7dc8c13336289d8427655b8c4c174e2

Authored by Dhruv Kapadia
1 parent 230094ef

Added helpful output on cron job success

app/models/cron_mailer.rb
1 class CronMailer < ActionMailer::Base 1 class CronMailer < ActionMailer::Base
2 2
3 - def error_message(subject, message, sent_at= Time.now) 3 + def info_message(recipients, subject, message, sent_at= Time.now)
  4 + @recipients = recipients
4 @from = 'cronjob@allourideas.org' 5 @from = 'cronjob@allourideas.org'
5 - @recipients = "dhruv@allourideas.org"  
6 @subject ="[All Our Ideas] " + subject 6 @subject ="[All Our Ideas] " + subject
7 @sent_on = sent_at 7 @sent_on = sent_at
8 @body[:message] = message 8 @body[:message] = message
app/views/cron_mailer/error_message.erb
1 Hi, 1 Hi,
2 2
3 -A cron job has sent an error message. Find the hopefully relevant message below: 3 +A cron job has sent a message. Find the hopefully relevant message below:
4 4
5 <%= @message %> 5 <%= @message %>
6 6
app/views/cron_mailer/error_message.haml
@@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
1 -Hi,  
2 -  
3 -= "A cron job has sent an error message. Find the hopefully relevant message below:\n"  
4 -  
5 -= @message`  
6 -  
7 -Thanks,  
8 -All Our Ideas  
9 -145 Wallace Hall  
10 -Princeton, NJ 08544  
11 -info@allourideas.org  
app/views/cron_mailer/info_message.erb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +Hi,
  2 +
  3 +A cron job has sent an error message. Find the hopefully relevant message below:
  4 +
  5 +<%= @message %>
  6 +
  7 +Thanks,
  8 +All Our Ideas
  9 +145 Wallace Hall
  10 +Princeton, NJ 08544
  11 +info@allourideas.org
config/initializers/mail.rb
@@ -14,4 +14,5 @@ ActionMailer::Base.smtp_settings = { @@ -14,4 +14,5 @@ ActionMailer::Base.smtp_settings = {
14 } 14 }
15 15
16 DO_NOT_REPLY = "donotreply@example.com" 16 DO_NOT_REPLY = "donotreply@example.com"
17 - 17 +CRON_EMAIL = "cron@allourideas.org" #Change to ENV
  18 +ERRORS_EMAIL = "errors@allourideas.org" #Change to ENV
lib/tasks/test_api.rake
@@ -24,32 +24,31 @@ namespace :test_api do @@ -24,32 +24,31 @@ namespace :test_api do
24 end 24 end
25 end 25 end
26 26
27 -  
28 if (2*total_wins != total_votes) 27 if (2*total_wins != total_votes)
29 - error_msg += "Error 1:" 28 + error_msg += "Error 1: 2 x Total Wins != Total votes"
30 error_bool= true 29 error_bool= true
31 end 30 end
32 31
33 if(total_votes % 2 != 0) 32 if(total_votes % 2 != 0)
34 - error_msg += "Error 2:" 33 + error_msg += "Error 2: Total votes is not Even!"
35 error_bool= true 34 error_bool= true
36 end 35 end
37 36
38 - if(total_votes != question.votes_count)  
39 - error_msg += "Error 3:" 37 + if(total_votes != 2* question.votes_count)
  38 + error_msg += "Error 3: Total votes != 2 x # vote objects"
40 error_bool = true 39 error_bool = true
41 end 40 end
42 41
43 if error_bool 42 if error_bool
44 error_msg += "Question #{question.id}: 2*wins = #{2*total_wins}, total votes = #{total_votes}, vote_count = #{question.votes_count}\n" 43 error_msg += "Question #{question.id}: 2*wins = #{2*total_wins}, total votes = #{total_votes}, vote_count = #{question.votes_count}\n"
45 end 44 end
46 -  
47 error_bool = false 45 error_bool = false
48 -  
49 end 46 end
50 47
51 - if error_msg  
52 - CronMailer.deliver_error_message("Data in api check error!", error_msg) 48 + if error_msg.blank?
  49 + CronMailer.deliver_info_message(CRON_EMAIL, "Test of API Vote Consistency passed", "Examined #{questions.count} questions and found no irregulaties")
  50 + else
  51 + CronMailer.deliver_info_message("#{CRON_EMAIL},#{ERRORS_EMAIL}", "Error! Failure of API Vote Consistency " , error_msg)
53 end 52 end
54 53
55 end 54 end