From 9aabce9c9fd0b6f8b6df366b710600e272b30bb6 Mon Sep 17 00:00:00 2001 From: lizzie cullen davison Date: Wed, 26 Apr 2023 16:15:18 +0100 Subject: [PATCH 1/2] Add task for updating index without wiping --- lib/tasks/update_public_index.rake | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/tasks/update_public_index.rake diff --git a/lib/tasks/update_public_index.rake b/lib/tasks/update_public_index.rake new file mode 100644 index 00000000..a45f0a98 --- /dev/null +++ b/lib/tasks/update_public_index.rake @@ -0,0 +1,65 @@ +desc 'Update public index in the Mongo API' +task :update_public_index => :environment do + # Turn off logging for this rake task, otherwise it just fills up our logs + dev_null = Logger.new('/dev/null') + Rails.logger = dev_null + ActiveRecord::Base.logger = dev_null + + Mongo::Logger.logger.level = Logger::FATAL + + puts "⏰ Connecting to mongo database..." + client = Mongo::Client.new(ENV["DB_URI"] || 'mongodb://root:password@localhost:27017/outpost_development?authSource=admin', { + retry_writes: false + }) + collection = client.database[:indexed_services] + + approved_count, unapproved_count, deleted_count, deleted_skipped_count = 0, 0, 0, 0 + + Service.find_each do |service| + + case service.status + when 'active', 'temporarily closed' + collection.find_one_and_update({ id: service.id }, + IndexedServicesSerializer.new(service).as_json, + { upsert: true }) + puts "✅ #{service.name} indexed" + approved_count += 1 + + when 'archived', 'marked for deletion', 'invisible', 'scheduled', 'expired' + + deleted = collection.find_one_and_delete({ id: service.id }) + if deleted + puts "✅ 🚮 #{service.name} deleted" + deleted_count += 1 + else + puts "✅ 🚮 #{service.name} not found in index, skipping" + deleted_skipped_count += 1 + end + + when 'pending' + approved_alternative = service.last_approved_snapshot + unless approved_alternative + puts "🚨 No alternative approved snapshot of #{service.name} exists. Skipping." + next + end + + unless approved_alternative.object['visible'] == true && approved_alternative.object['discarded_at'].blank? + puts "🚨 Approved snapshot of #{service.name} is not publicly visible. Skipping." + next + end + + snapshot = Service.from_hash(approved_alternative.object) + collection.find_one_and_update({ id: service.id }, + IndexedServicesSerializer.new(snapshot).as_json, + { upsert: true }) + puts "🤔 Alternative approved snapshot of #{service.name} indexed" + unapproved_count += 1 + end + end + + puts "\n\n 🏁🏁 SUMMARY 🏁🏁" + puts "👉 #{approved_count} approved services added to index." + puts "👉 #{unapproved_count} alternative snapshots of unapproved services added to index." + puts "👉 #{deleted_count} deleted or expired services removed from index." + puts "👉 #{deleted_skipped_count} deleted or expired services not found in index." +end From 81b9a8d6af70e46e04f84d35453b435caf266a50 Mon Sep 17 00:00:00 2001 From: lizzie cullen davison Date: Wed, 26 Apr 2023 16:16:34 +0100 Subject: [PATCH 2/2] Change description for build_public_index task --- lib/tasks/build_public_index.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/build_public_index.rake b/lib/tasks/build_public_index.rake index 2e27d59a..e3a624ef 100644 --- a/lib/tasks/build_public_index.rake +++ b/lib/tasks/build_public_index.rake @@ -1,4 +1,4 @@ -desc 'Build public index in the Mongo API' +desc 'Wipe and rebuild public index in the Mongo API' task :build_public_index => :environment do # Turn off logging for this rake task, otherwise it just fills up our logs dev_null = Logger.new('/dev/null')