Improve logging

This commit is contained in:
Karl Erik Õunapuu 2020-09-03 16:42:04 +03:00
parent 11fc484270
commit bbbb3d5367
No known key found for this signature in database
GPG key ID: C9DD647298A34764
5 changed files with 8 additions and 17 deletions

View file

@ -12,7 +12,7 @@ module Concerns
def archivable?(post: false) def archivable?(post: false)
inactive = inactive? inactive = inactive?
log("Found archivable contact id(#{id}), code (#{code})") if inactive && !post puts "Found archivable contact id(#{id}), code (#{code})" if inactive && !post
inactive inactive
end end
@ -38,11 +38,6 @@ module Concerns
def inactivity_period def inactivity_period
Setting.orphans_contacts_in_months.months Setting.orphans_contacts_in_months.months
end end
def log(msg)
@logger ||= Logger.new(STDOUT)
@logger.info(msg)
end
end end
end end
end end

View file

@ -7,15 +7,9 @@ class InactiveContacts
def archive(verified: false) def archive(verified: false)
contacts.each do |contact| contacts.each do |contact|
log("Archiving contact: id(#{contact.id}), code(#{contact.code})") puts "Archiving contact: id(#{contact.id}), code(#{contact.code})"
contact.archive(verified: verified) contact.archive(verified: verified)
yield contact if block_given? yield contact if block_given?
end end
end end
def log(msg)
@logger ||= Logger.new(STDOUT)
@logger.info(msg)
end
end end

View file

@ -2,7 +2,6 @@ namespace :contacts do
desc 'Archives inactive contacts' desc 'Archives inactive contacts'
task archive: :environment do task archive: :environment do
puts 'Starting to gather archivable contacts'
inactive_contacts = InactiveContacts.new inactive_contacts = InactiveContacts.new
archived_contacts = inactive_contacts.archive(verified: true) archived_contacts = inactive_contacts.archive(verified: true)

View file

@ -3,11 +3,13 @@ require 'test_helper'
class InactiveContactsTest < ActiveSupport::TestCase class InactiveContactsTest < ActiveSupport::TestCase
def test_archives_inactive_contacts def test_archives_inactive_contacts
contact_mock = Minitest::Mock.new contact_mock = Minitest::Mock.new
contact_mock.expect(:archive, nil) contact_mock.expect(:archive, nil, [{verified: false}])
contact_mock.expect(:id, 'id')
contact_mock.expect(:code, 'code')
inactive_contacts = InactiveContacts.new([contact_mock]) inactive_contacts = InactiveContacts.new([contact_mock])
inactive_contacts.archive inactive_contacts.archive
assert_mock contact_mock assert_mock contact_mock
end end
end end

View file

@ -13,7 +13,8 @@ class ArchiveContactsTaskTest < ActiveSupport::TestCase
contact = archivable_contact contact = archivable_contact
eliminate_effect_of_all_contacts_except(contact) eliminate_effect_of_all_contacts_except(contact)
expected_output = "Contact ##{contact.id} (code: #{contact.code}) is archived\n" \ expected_output = "Found archivable contact id(#{contact.id}), code (#{contact.code})\n" \
"Archiving contact: id(#{contact.id}), code(#{contact.code})\n" \
"Archived total: 1\n" "Archived total: 1\n"
assert_output(expected_output) { run_task } assert_output(expected_output) { run_task }
end end