class AddDurationToBroadcastRecordings < ActiveRecord::DataMigration
  def up
    recordings = BroadcastRecording.where(duration: nil)
    client = MuxRuby::AssetsApi.new

    recordings.each do |recording|
      begin
        response = client.get_asset(recording.asset_uid)
        duration = response.data.duration
        recording.update(duration: duration)
      rescue MuxRuby::ApiError => e
        Rails.logger.error("Failed to update duration for broadcast recording with id #{recording.id}\n" + e.message)
      end

      sleep(1)
    end
  end
end