Skip to content

Commit 5c1e396

Browse files
committed
add grading heartbeat
1 parent c515a98 commit 5c1e396

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

lib/tasks/service.rake

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
namespace :service do
22
desc "Recalculate ratings for all services (in batches)"
33
task perform_rating: :environment do
4-
batch_size = 100
4+
batch_size = (ENV["BATCH_SIZE"] || 100).to_i
5+
debug_mode = ActiveModel::Type::Boolean.new.cast(ENV["DEBUG_RATING"])
6+
processed_count = 0
7+
updated_count = 0
8+
9+
puts "[service:perform_rating] Starting (batch_size=#{batch_size}, debug=#{debug_mode})"
10+
511
Service.find_in_batches(batch_size: batch_size) do |services|
612
services.each do |service|
7-
recalculate_service_rating(service)
13+
updated = recalculate_service_rating(service, debug_mode)
14+
processed_count += 1
15+
updated_count += 1 if updated
16+
end
17+
end
18+
19+
puts "[service:perform_rating] Done (processed=#{processed_count}, updated=#{updated_count})"
20+
21+
heartbeat_url = ENV["GRADING_SUCCESS_HEARTBEAT"]
22+
if heartbeat_url.present?
23+
if system("curl", "-fsS", "-m", "10", heartbeat_url)
24+
puts "[service:perform_rating] Sent success heartbeat"
25+
else
26+
warn "[service:perform_rating] Failed to send success heartbeat"
827
end
928
end
1029
end
1130

12-
def recalculate_service_rating(service)
31+
def recalculate_service_rating(service, debug_mode = false)
1332
initial_rating = service.rating
1433
new_rating = service.calculate_service_rating
1534

16-
return if new_rating == initial_rating
35+
if new_rating == initial_rating
36+
puts "[service:perform_rating] Service ##{service.id} unchanged (rating=#{initial_rating})" if debug_mode
37+
return false
38+
end
1739

1840
service.update_columns(rating: new_rating, updated_at: Time.current)
41+
puts "[service:perform_rating] Service ##{service.id} updated #{initial_rating} -> #{new_rating}" if debug_mode
1942

2043
Version.create!(
2144
item_type: "Service",
@@ -24,6 +47,8 @@ namespace :service do
2447
whodunnit: "21311",
2548
object_changes: "This has been an automatic update by an official ToS;DR bot. The rating for this service changed from #{initial_rating} to #{new_rating}."
2649
)
50+
51+
true
2752
end
2853

2954
desc "Mark as reviewed if approved points are over 20"

0 commit comments

Comments
 (0)