diff --git a/app.json b/app.json index 22c302fb..eab8c55f 100644 --- a/app.json +++ b/app.json @@ -24,6 +24,10 @@ "command": "rake notify_operators", "schedule": "30 15 * * *" }, + { + "command": "rake tag_opdb_changelog", + "schedule": "30 13 * * *" + }, { "command": "rake delete_opted_in_empty_locations", "schedule": "30 22 * * 0" diff --git a/app/models/machine.rb b/app/models/machine.rb index 19887452..d2005c7e 100644 --- a/app/models/machine.rb +++ b/app/models/machine.rb @@ -45,7 +45,21 @@ def self.tag_with_opdb_type_json(opdb_json) end end - def self.tag_with_opdb_json(opdb_json) + def self.tag_with_opdb_changelog_json(opdb_json) + JSON.parse(opdb_json).each do |r| + m = Machine.find_by_opdb_id(r["opdb_id_deleted"]) + next unless m + + if r["action"] == "move" + m.opdb_id = r["opdb_id_replacement"] + elsif r["action"] == "delete" + m.opdb_id = "" + end + m.save + end + end + + def self.tag_with_opdb_image_json(opdb_json) JSON.parse(opdb_json).each do |r| m = Machine.find_by_opdb_id(r["opdb_id"]) next unless m diff --git a/lib/tasks/tag_opdb.rake b/lib/tasks/tag_opdb.rake deleted file mode 100644 index f788a621..00000000 --- a/lib/tasks/tag_opdb.rake +++ /dev/null @@ -1,9 +0,0 @@ -desc "Tags machines with opdb data" -task tag_opdb: :environment do - response = Net::HTTP.get_response(URI("https://opdb.org/api/export?api_token=#{ENV['OPDB_KEY']}")) - Machine.tag_with_opdb_json(response.body) -rescue StandardError => e - error_subject = "Tag OPDB rake task error" - error = e.to_s - ErrorMailer.with(error: error, error_subject: error_subject).rake_task_error.deliver_later -end