Skip to content

Commit

Permalink
it was a nice addition, but the error handling has it under control s…
Browse files Browse the repository at this point in the history
…o no need to disable sentry while running now
  • Loading branch information
aprilrieger committed Dec 11, 2024
1 parent baed2f4 commit 19c98b7
Showing 1 changed file with 44 additions and 56 deletions.
100 changes: 44 additions & 56 deletions lib/tasks/tenants.rake
Original file line number Diff line number Diff line change
@@ -1,73 +1,61 @@
# frozen_string_literal: true

# Temporarily disable Sentry for this task
Raven.configure do |config|
config.silence_ready = true
end

namespace :tenants do
# How much space, works, files, per each tenant?
task calculate_usage: :environment do
begin
@results = []
Account.where(search_only: false).find_each do |account|
next unless account.cname.present? # Skip accounts without a cname

AccountElevator.switch!(account.cname)
puts "---------------#{account.cname}-------------------------"

models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)

if works&.any?
puts "#{works.count} works found"
tenant_file_sizes = [] # Declare and initialize within the block

works.each do |work|
begin
document = SolrDocument.find(work.id)
files = document._source["file_set_ids_ssim"] || []

if files.any?
file_sizes = files.map do |file|
begin
f = SolrDocument.find(file.to_s)
f.to_h['file_size_lts'] || 0
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: File #{file} not found. Skipping. Error: #{e.message}"
0
end
@results = []
Account.where(search_only: false).find_each do |account|
next unless account.cname.present? # Skip accounts without a cname

AccountElevator.switch!(account.cname)
puts "---------------#{account.cname}-------------------------"

models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)

if works&.any?
puts "#{works.count} works found"
tenant_file_sizes = [] # Declare and initialize within the block

works.each do |work|
begin
document = SolrDocument.find(work.id)
files = document._source["file_set_ids_ssim"] || []

if files.any?
file_sizes = files.map do |file|
begin
f = SolrDocument.find(file.to_s)
f.to_h['file_size_lts'] || 0
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: File #{file} not found. Skipping. Error: #{e.message}"
0
end

total_file_size_bytes = file_sizes.inject(0, :+)
tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2)
else
tenant_file_sizes << 0
end
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}"

total_file_size_bytes = file_sizes.inject(0, :+)
tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2)
else
tenant_file_sizes << 0
end
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}"
tenant_file_sizes << 0
end

total_mb = tenant_file_sizes.inject(0, :+)
@results << "#{account.cname}: #{total_mb} Total MB / #{works.count} Works"
else
@results << "#{account.cname}: 0 Total MB / 0 Works"
end

puts "=================================================================="
total_mb = tenant_file_sizes.inject(0, :+)
@results << "#{account.cname}: #{total_mb} Total MB / #{works.count} Works"
else
@results << "#{account.cname}: 0 Total MB / 0 Works"
end

# Output results
@results.each do |result|
puts result
end
ensure
# Re-enable Sentry after the task
Raven.configure do |config|
config.silence_ready = false
end
puts "=================================================================="
end

# Output results
@results.each do |result|
puts result
end
end
end

0 comments on commit 19c98b7

Please sign in to comment.