Commit 12440cec8a8406ab60c1ca8653e1763e7d7e6ab6

Authored by Rodrigo Souto
1 parent 593da77e

Logging Spaminator movements

plugins/spaminator/controllers/spaminator_plugin_admin_controller.rb
... ... @@ -34,7 +34,7 @@ class SpaminatorPluginAdminController < AdminController
34 34 settings.scanning = true
35 35 settings.save!
36 36 remove_scheduled_scan
37   - Delayed::Job.enqueue(SpaminatorPlugin::ScanJob.new(environment))
  37 + Delayed::Job.enqueue(SpaminatorPlugin::ScanJob.new(environment.id))
38 38 end
39 39 redirect_to :action => 'index'
40 40 end
... ...
plugins/spaminator/lib/spaminator_plugin/scan_job.rb
... ... @@ -6,7 +6,11 @@ class SpaminatorPlugin::ScanJob < Struct.new(:environment_id)
6 6 settings.scanning = true
7 7 settings.save!
8 8  
9   - SpaminatorPlugin::Spaminator.run(environment)
  9 + begin
  10 + SpaminatorPlugin::Spaminator.run(environment)
  11 + rescue Exception => exception
  12 + SpaminatorPlugin::Spaminator.log("Spaminator failed with the following error: \n ==> #{exception}\n#{exception.backtrace.join("\n")}")
  13 + end
10 14  
11 15 settings.scanning = false
12 16 SpaminatorPlugin.schedule_scan(environment) if settings.deployed
... ...
plugins/spaminator/lib/spaminator_plugin/spaminator.rb
... ... @@ -11,6 +11,14 @@ class SpaminatorPlugin::Spaminator
11 11 def benchmark(environment)
12 12 puts Benchmark.measure { run(environment) }
13 13 end
  14 +
  15 + def initialize_logger(logpath)
  16 + @logger = Logger.new(logpath)
  17 + end
  18 +
  19 + def log(message)
  20 + @logger << "[#{Time.now.strftime("%F %T %z")}] #{message}\n"
  21 + end
14 22 end
15 23  
16 24  
... ... @@ -20,9 +28,12 @@ class SpaminatorPlugin::Spaminator
20 28 @report = SpaminatorPlugin::Report.new(:environment => environment,
21 29 :total_people => Person.count,
22 30 :total_comments => Comment.count)
  31 + logpath = File.join(SpaminatorPlugin.root_path, 'log', "#{environment.name.to_slug}_#{ENV['RAILS_ENV']}_#{Time.now.strftime("%F_%T")}.log")
  32 + self.class.initialize_logger(logpath)
23 33 end
24 34  
25 35 def run
  36 + self.class.log("Starting Spaminator scan")
26 37 start_time = Time.now
27 38  
28 39 process_all_comments
... ... @@ -37,6 +48,7 @@ class SpaminatorPlugin::Spaminator
37 48 finish_report
38 49 @settings.last_run = start_time
39 50 @settings.save!
  51 + self.class.log("Finishing Spaminator scan successfully")
40 52 end
41 53  
42 54 # TODO considering run everything always
... ... @@ -53,6 +65,7 @@ class SpaminatorPlugin::Spaminator
53 65 end
54 66  
55 67 def process_all_comments
  68 + self.class.log("Starting to process all comments")
56 69 comments = comments_to_process
57 70 total = comments.count
58 71 pbar = ProgressBar.new("☢ Comments", total)
... ... @@ -66,9 +79,11 @@ class SpaminatorPlugin::Spaminator
66 79 end
67 80 @report.processed_comments = total
68 81 pbar.finish
  82 + self.class.log("All comments processed")
69 83 end
70 84  
71 85 def process_all_people
  86 + self.class.log("Starting to process all people")
72 87 people = people_to_process
73 88 total = people.count
74 89 pbar = ProgressBar.new("☢ People", total)
... ... @@ -79,9 +94,11 @@ class SpaminatorPlugin::Spaminator
79 94 end
80 95 @report.processed_people = total
81 96 pbar.finish
  97 + self.class.log("All people processed")
82 98 end
83 99  
84 100 def process_comment(comment)
  101 + self.class.log("Processing Comment[#{comment.id.to_s}]")
85 102 comment = Comment.find(comment.id)
86 103 comment.check_for_spam
87 104 @report.spams_by_content += 1 if comment.spam
... ... @@ -97,6 +114,7 @@ class SpaminatorPlugin::Spaminator
97 114 # person is author of more than 2 comments marked as spam
98 115 # → mark as spammer
99 116 #
  117 + self.class.log("Processing Person[#{person.id.to_s}] by comments")
100 118 begin
101 119 number_of_spam_comments = Comment.spam.where(:author_id => person.id).count
102 120 if number_of_spam_comments > 2
... ... @@ -115,6 +133,7 @@ class SpaminatorPlugin::Spaminator
115 133 # → disable the profile
116 134 # ? mark their comments as spam
117 135 #
  136 + self.class.log("Processing Person[#{person.id.to_s}] by network")
118 137 if person.created_at < (Time.now - 1.month) &&
119 138 person.friends.count == 0 &&
120 139 person.communities.count <= 1
... ... @@ -151,6 +170,7 @@ class SpaminatorPlugin::Spaminator
151 170 end
152 171  
153 172 def register_fail(kind, failed)
  173 + self.class.log("Failed #{kind.to_s.camelize}[#{failed.id.to_s}]")
154 174 @report[:failed][kind.to_sym] << failed.id
155 175 end
156 176 end
... ...