From 8ffaa0fd2c8568735f61ab8a2f42d9d733d11aa6 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Tue, 8 Mar 2022 00:53:31 -0300 Subject: [PATCH] Replaced puts for rails logger and enabled info logging on rake tasks + new tasks for deleting all action logs --- app/interactors/monitor_inbox.rb | 6 +++--- .../20220307195441_add_action_logs_table.rb | 1 - lib/tasks/monitor.rake | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/interactors/monitor_inbox.rb b/app/interactors/monitor_inbox.rb index 7c370e9..d6df8e4 100644 --- a/app/interactors/monitor_inbox.rb +++ b/app/interactors/monitor_inbox.rb @@ -43,7 +43,7 @@ class MonitorInbox # Analiza uno a uno los elmentos del array de emails def analyze_emails(emails_arr) 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) execute_action(email) end @@ -65,7 +65,7 @@ class MonitorInbox ) if al.valid? - puts "Executing action..." + Rails.logger.info "Executing action..." command = ENV["COMMAND"] system(command) context.actions_executed_count+=1 @@ -73,7 +73,7 @@ class MonitorInbox else # 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 - puts "We have already executed this action" + Rails.logger.info "We have already executed this action" end end end diff --git a/db/migrate/20220307195441_add_action_logs_table.rb b/db/migrate/20220307195441_add_action_logs_table.rb index 91edd78..107aea2 100644 --- a/db/migrate/20220307195441_add_action_logs_table.rb +++ b/db/migrate/20220307195441_add_action_logs_table.rb @@ -8,4 +8,3 @@ class AddActionLogsTable < ActiveRecord::Migration[7.0] end end end - diff --git a/lib/tasks/monitor.rake b/lib/tasks/monitor.rake index 18ba898..42c1354 100644 --- a/lib/tasks/monitor.rake +++ b/lib/tasks/monitor.rake @@ -1,9 +1,19 @@ namespace :monitor do desc "executes the monitoring logic" 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! - puts "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.analyzed_emails_count} new emails were analyzed" + 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