From 2bd86e85ebeb67df929e0624eefb218cd6298df1 Mon Sep 17 00:00:00 2001 From: Helen Pickavance Date: Thu, 21 Nov 2024 11:57:37 +0000 Subject: [PATCH] Add logging to file and exception handling - Logging allows us to gather a list of documents that cannot be found - Failure to find is typically because the path/slug has changed. --- lib/tasks/access_and_permissions.rake | 60 +++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/tasks/access_and_permissions.rake b/lib/tasks/access_and_permissions.rake index c88b3a956..bf92760a7 100644 --- a/lib/tasks/access_and_permissions.rake +++ b/lib/tasks/access_and_permissions.rake @@ -1,52 +1,52 @@ require "csv" namespace :permissions do - desc "Add an organisation to an document's access permissions list" + desc "Add an organisation to a document's access permissions list" task :add_organisation_access, %i[document_content_id org_content_id log_file] => :environment do |_, args| document = Artefact.find_by(id: args[:document_content_id]) + if document.nil? - if args[:log_file] - args[:log_file].puts "Document ID #{args[:org_content_id]} not found, no permissions changed." - else - puts "Document with ID #{args[:org_content_id]} not found, no permissions changed." - end - elsif document.latest_edition.owning_org_content_ids.include? args[:org_content_id] - if args[:log_file] - args[:log_file].puts "Organisation already has permission to access the document with ID - #{document.id}" - else - puts "Organisation already has permission to access the document with ID - #{document.id}" - end + message = "Document ID #{args[:document_content_id]} not found, no permissions changed." + elsif document.latest_edition.owning_org_content_ids.include?(args[:org_content_id]) + message = "Organisation already has permission to access the document with ID - #{document.id}" else Edition.where(panopticon_id: document.id).each do |edition| edition.owning_org_content_ids << args[:org_content_id] edition.save!(validate: false) end document.save_as_task!("PermissionsAddition") - if args[:log_file] - args[:log_file].puts("Access permission successfully assigned to document with ID - #{document.id}") - else - puts "Access permission successfully assigned to document with ID - #{document.id}" - end + message = "Access permission successfully assigned to document with ID - #{document.id}" end + args[:log_file] ? args[:log_file].puts(message) : puts(message) + rescue StandardError => e + error_message = "An error occurred while processing document ID #{args[:document_content_id]}: #{e.message}" + args[:log_file] ? args[:log_file].puts(error_message) : puts(error_message) end desc "Bulk process access permissions from CSV of URLs - See doc" task :bulk_process_access_flags, %i[csv_filename organisation_id] => :environment do |_, args| - log_file = File.new("permissions_rake_log.txt", "w") - CSV.foreach(args[:csv_filename], headers: true) do |row| - path = row[1] - path&.slice! "https://www.gov.uk/" - document = Artefact.find_by(slug: path) + log_file = File.open("permissions_rake_log.txt", "w") + + begin + CSV.foreach(args[:csv_filename], headers: true) do |row| + path = row[1] + path&.slice!("https://www.gov.uk/") + document = Artefact.find_by(slug: path) - next if document.nil? + if document.nil? + log_file.puts "Document with slug '#{path}' not found. Skipping..." + next + end - Rake::Task["permissions:add_organisation_access"].reenable # Can be done after if cleaner - Rake::Task["permissions:add_organisation_access"].invoke(document.id, args[:organisation_id], log_file) - rescue Mongoid::Errors::DocumentNotFound => e - log_file.puts "--- Document not found error ---" - log_file.puts e.detailed_message - log_file.puts "------" + Rake::Task["permissions:add_organisation_access"].reenable + Rake::Task["permissions:add_organisation_access"].invoke(document.id, args[:organisation_id], log_file) + rescue Mongoid::Errors::DocumentNotFound => e + log_file.puts "--- Document not found error ---" + log_file.puts e.detailed_message + log_file.puts "------" + end + ensure + log_file.close end - log_file.close end end