Files
growstuff/lib/tasks/photos.rake
T
9ff8a5491b Run photos:prune_404s rake task monthly (#5107)
* Run photos:prune_404s task monthly on the 1st of the month

Assuming Heroku Scheduler invokes the task daily, check if today is
the first day of the month before pruning 404 photos. Also add spec to test this behavior.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Delete spec/tasks/photos_prune_404s_spec.rb

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-10-26 23:26:38 +10:30

26 lines
804 B
Ruby

# frozen_string_literal: true
namespace :photos do
desc 'Removes references to photos that dont exist anymore (404)'
task prune_404s: :environment do
# Heroku scheduler only lets us run things daily, so this checks
# Run on the 1st of each month
next unless Time.zone.today.day == 1
puts 'Checking for broken photo links...'
Photo.find_each do |photo|
puts "Checking #{photo.fullsize_url}..."
uri = URI.parse(photo.fullsize_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
request = Net::HTTP::Head.new(uri.request_uri)
response = http.request(request)
if response.code == '404'
puts "Photo #{photo.id} is broken, deleting."
photo.destroy
end
end
puts 'Done.'
end
end