Last active
May 23, 2016 15:38
-
-
Save deathweaselx86/8a88a7164e4666df343662831043a587 to your computer and use it in GitHub Desktop.
reprocess links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "open a given log file, and extract/save links from all Business Verification records logged" | |
task :reprocess_links, [:filename] => :environment do |t, args| | |
lines_read = lines_processed = lines_skipped = lines_failed = 0 | |
filename = args[:filename] | |
puts "Reading lines from #{filename}..." | |
in_log = File.open(filename) | |
in_log.each do |line| | |
lines_read += 1 | |
json = JSON.parse(line) | |
if json['scope'] == ['BusinessVerifier'] | |
uuid = json['business']['uuid'] | |
links = json['links'] | |
if process(uuid, links) | |
lines_processed+=1 | |
else | |
lines_failed +=1 | |
end | |
else | |
lines_skipped += 1 | |
end | |
#puts "Read: #{lines_read} (skipped: #{lines_skipped}, processed: #{lines_processed}, failed: #{lines_failed})" if lines_read % 100 == 0 | |
end | |
end | |
def process(uuid, links) | |
business = Business.with_deleted.where(uuid: uuid).first | |
return false if business.nil? | |
# fix links | |
links.each do |attrs| | |
link = Link.with_deleted.where(business_id: business.id, | |
url: attrs["url"]).first_or_create | |
link.update_attributes(text: attrs["text"], | |
deleted_at: nil) | |
end | |
puts "Updated #{uuid}" | |
return true | |
rescue StandardError => e | |
puts "ERROR processing #{uuid} - #{e.class}: #{e.message}" | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment