Skip to content

Commit 02d448e

Browse files
authored
i1063-fix-tenants-rake (#1064)
Updates the tenants.rake to add graceful error handling in order to get the tenants data for client
1 parent 66f79ac commit 02d448e

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

lib/tasks/tenants.rake

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
# frozen_string_literal: true
22

33
namespace :tenants do
4-
# how much space, works, files, per each tenant?
4+
# How much space, works, files, per each tenant?
55
task calculate_usage: :environment do
66
@results = []
77
Account.where(search_only: false).find_each do |account|
8-
if account.cname.present?
9-
AccountElevator.switch!(account.cname)
10-
puts "---------------#{account.cname}-------------------------"
11-
models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
12-
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)
13-
if works&.any?
14-
puts "#{works.count} works found"
15-
@tenant_file_sizes = []
16-
works.each do |work|
8+
next if account.cname.blank?
9+
10+
AccountElevator.switch!(account.cname)
11+
puts "---------------#{account.cname}-------------------------"
12+
13+
models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
14+
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)
15+
16+
if works&.any?
17+
puts "#{works.count} works found"
18+
tenant_file_sizes = [] # Declare and initialize within the block
19+
20+
works.each do |work|
21+
begin
1722
document = SolrDocument.find(work.id)
18-
files = document._source["file_set_ids_ssim"]
19-
if files&.any?
20-
file_sizes = []
21-
files.each do |file|
22-
f = SolrDocument.find(file.to_s)
23-
if file
24-
file_sizes.push(f.to_h['file_size_lts']) unless f.to_h['file_size_lts'].nil?
25-
else
26-
files_sizes.push(0)
27-
end
28-
end
29-
if file_sizes.any?
30-
file_sizes_total_bytes = file_sizes.inject(0, :+)
31-
file_size_total = (file_sizes_total_bytes / 1.0.megabyte).round(2)
32-
else
33-
file_size_total = 0
23+
files = document._source["file_set_ids_ssim"] || []
24+
25+
if files.any?
26+
file_sizes = files.map do |file|
27+
begin
28+
f = SolrDocument.find(file.to_s)
29+
f.to_h['file_size_lts'] || 0
30+
rescue Blacklight::Exceptions::RecordNotFound => e
31+
puts "Warning: File #{file} not found. Skipping. Error: #{e.message}"
32+
0
33+
end
3434
end
35-
@tenant_file_sizes.push(file_size_total)
35+
36+
total_file_size_bytes = file_sizes.inject(0, :+)
37+
tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2)
3638
else
37-
@tenant_file_sizes.push(0)
39+
tenant_file_sizes << 0
3840
end
41+
rescue Blacklight::Exceptions::RecordNotFound => e
42+
puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}"
43+
tenant_file_sizes << 0
3944
end
40-
if @tenant_file_sizes
41-
tenant_file_sizes_total_megabytes = @tenant_file_sizes.inject(0, :+)
42-
@results.push("#{account.cname}: #{tenant_file_sizes_total_megabytes} Total MB / #{works.count} Works")
43-
else
44-
@results.push("#{account.cname}: 0 Total MB / #{works.count} Works")
45-
end
46-
else
47-
@results.push("#{account.cname}: 0 Total MB / 0 Works")
48-
end
49-
puts "=================================================================="
50-
@results.each do |result|
51-
puts result
5245
end
46+
47+
total_mb = tenant_file_sizes.inject(0, :+)
48+
@results << "#{account.cname}: #{total_mb} Total MB / #{works.count} Works"
49+
else
50+
@results << "#{account.cname}: 0 Total MB / 0 Works"
5351
end
52+
53+
puts "=================================================================="
54+
end
55+
56+
# Output results
57+
@results.each do |result|
58+
puts result
5459
end
5560
end
5661
end

0 commit comments

Comments
 (0)