Browse Source

Replaced puts for rails logger and enabled info logging on rake tasks + new tasks for deleting all action logs

master
rodley82 4 years ago
parent
commit
8ffaa0fd2c
  1. 6
      app/interactors/monitor_inbox.rb
  2. 1
      db/migrate/20220307195441_add_action_logs_table.rb
  3. 16
      lib/tasks/monitor.rake

6
app/interactors/monitor_inbox.rb

@ -43,7 +43,7 @@ class MonitorInbox
# Analiza uno a uno los elmentos del array de emails # Analiza uno a uno los elmentos del array de emails
def analyze_emails(emails_arr) def analyze_emails(emails_arr)
emails_arr.each do |email| emails_arr.each do |email|
puts "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}" Rails.logger.info "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}"
if is_action_email?(email) if is_action_email?(email)
execute_action(email) execute_action(email)
end end
@ -65,7 +65,7 @@ class MonitorInbox
) )
if al.valid? if al.valid?
puts "Executing action..." Rails.logger.info "Executing action..."
command = ENV["COMMAND"] command = ENV["COMMAND"]
system(command) system(command)
context.actions_executed_count+=1 context.actions_executed_count+=1
@ -73,7 +73,7 @@ class MonitorInbox
else else
# We're using the validate uniqueness of ActionLog to detect duplicate actions # We're using the validate uniqueness of ActionLog to detect duplicate actions
# we have already processed if we get this exception we should not execute it # we have already processed if we get this exception we should not execute it
puts "We have already executed this action" Rails.logger.info "We have already executed this action"
end end
end end
end end

1
db/migrate/20220307195441_add_action_logs_table.rb

@ -8,4 +8,3 @@ class AddActionLogsTable < ActiveRecord::Migration[7.0]
end end
end end
end end

16
lib/tasks/monitor.rake

@ -1,9 +1,19 @@
namespace :monitor do namespace :monitor do
desc "executes the monitoring logic" desc "executes the monitoring logic"
task :run => :environment do task :run => :environment do
puts "About to check #{ENV["EMAIL"]} inbox..." Rails.logger = Logger.new(STDOUT)
Rails.logger.level = Logger::INFO
Rails.logger.info "About to check #{ENV["EMAIL"]} inbox..."
res = MonitorInbox.call! res = MonitorInbox.call!
puts "A total of #{res.analyzed_emails_count} new emails were analyzed" Rails.logger.info "A total of #{res.analyzed_emails_count} new emails were analyzed"
puts "A total of #{res.actions_executed_count} actions were triggered" Rails.logger.info "A total of #{res.actions_executed_count} actions were triggered"
end
desc "deletes all log entries"
task :clear_log => :environment do
Rails.logger = Logger.new(STDOUT)
Rails.logger.level = Logger::INFO
Rails.logger.info "About to clear all log entries"
ActionLog.delete_all
end end
end end

Loading…
Cancel
Save