Skip to content

Instantly share code, notes, and snippets.

@spickermann
Last active December 13, 2015 23:48
Show Gist options
  • Save spickermann/4993825 to your computer and use it in GitHub Desktop.
Save spickermann/4993825 to your computer and use it in GitHub Desktop.
Identifiy and disable users that are in the list of users with invalid emails and do not have any purchases and are not active.
namespace :disable_users do
desc 'Generates a csv file containing candidates for deletion'
task :determine_candidates => :environment do
logger = Logger.new('tmp/candidates.csv')
users = 0
candidates = 0
{ :invalid => :invalid_emails, :dns_err => :unresolvable_emails }.each_pair do |reason, input_file|
File.read("tmp/#{input_file}.csv").each do |line|
users += 1
_, uuid, date, email = line.split(',')
if user = User.find_by_uuid(uuid)
date = DateTime.parse(date) if date
last_active = [date, user.last_request_at].compact.max
if user.purchases.blank? && (!last_active || last_active < 3.month.ago)
logger.warn("#{reason},#{uuid},#{last_active},#{email}")
candidates += 1
end
end
end
end
puts "#{candidates} candidates / #{users} users"
end
desc 'Disable all users in the candidates.csv'
task :delete_candidates => :environment do
logger = Logger.new('log/deleted_candidates.csv')
candidates = 0
deleted = 0
File.read("tmp/candidates.csv").each do |line|
candidates += 1
reason, uuid, date, email = line.split(',')
if user = User.find_by_uuid(uuid)
user.delete_account(backup_user = true, notify_user = false)
deleted += 1
else
logger.warn("User #{uuid} not found")
end
puts "processed candidate ##{candidates}"
end
logger.warn "#{deleted} deleted / #{candidates} candidates"
puts "#{deleted} deleted / #{candidates} candidates"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment