Created
January 20, 2014 07:50
-
-
Save huemorgan/8516479 to your computer and use it in GitHub Desktop.
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 "Intercom general updates options" | |
task :sync, [:is_init] => :environment do |t, args| | |
require 'intercom' | |
require 'progressbar' | |
puts "Intercom sync started on #{Time.now} (#{Time.now.to_i})" | |
# Run over all the accounts | |
error_log = [] | |
orgids = Account.organizations.pluck(:id) | |
if args.is_init == "true" | |
puts "is init is true" | |
account_ids = orgids | |
else | |
account_ids = Post.where(account_id: orgids).where("DATE(created_at) >= ?", 1.day.ago.to_date).uniq.pluck(:account_id) | |
account_ids += User.where(account_id: orgids).where("DATE(created_at) >= ?", 1.day.ago.to_date).uniq.pluck(:account_id) | |
end | |
account_ids = account_ids.uniq | |
Account.organizations.where(id: account_ids).each do |account| | |
# Start the Progress Bar | |
puts "Updating Account: #{account.name} Users" | |
bar = ProgressBar.new( | |
"Updating Users in #{account.name}: ", | |
account.users.active.non_pending.count | |
) | |
account_posts = account.posts.updates.count | |
account_users = account.users.active.non_pending.count | |
# Run over all the users | |
account.users.active.non_pending.find_in_batches(:batch_size => 50) do |ub| | |
ub.each do |u| | |
# Find the user in intercom | |
attempts = 0 | |
begin | |
user = Intercom::User.find(:email => u.email) | |
# puts "User: #{user.name}, #{user.email}" | |
# puts "posts on the user account: #{account_posts}" | |
user.custom_data['account_posts'] = account_posts | |
# puts "users on the user account: #{account_users}" | |
user.custom_data['account_users'] = account_users | |
has_three_updates = (Post.updates.where(account_id: account.id).count >= 3) | |
has_three_users = (User.active.non_pending.where(account_id: account.id).count >= 3) | |
# puts("has_three_updates: #{has_three_updates} | has_three_users #{has_three_users}") | |
has_post_after_seven_days = nil | |
if has_three_updates && has_three_users | |
# puts("XXX") | |
last_step_user = account.users.active.non_pending.order("created_at asc").limit(3).last | |
last_step_post = account.posts.updates.order("created_at asc").limit(3).last | |
last_step_date = [last_step_user.created_at, last_step_post.created_at].max | |
last_post = account.posts.updates.order("created_at desc").limit(1).last | |
has_post_after_seven_days = (last_post.created_at - 7.days) >= last_step_date | |
user.custom_data['has_post_after_seven_days'] = has_post_after_seven_days | |
end | |
user.company = { | |
"id" => u.account.id.to_s, | |
"name" => u.account.name, | |
"created_at" => u.account.created_at.to_i, | |
"slug" => u.account.slug, | |
"posts_count" => account_posts, | |
"users_count" => account_users, | |
"has_post_after_seven_days" => has_post_after_seven_days, | |
"marketing_campaign" => u.account.marketing_campaign, | |
"marketing_source" => u.account.marketing_source, | |
"marketing_referrer" => u.account.marketing_referrer, | |
"marketing_medium" => u.account.marketing_medium, | |
"marketing_content" => u.account.marketing_content | |
} | |
user.save | |
rescue Intercom::ServiceUnavailableError | |
attempts += 1 | |
retry unless attempts > 2 | |
exit -1 | |
rescue Intercom::ResourceNotFound | |
error_log << "User #{u.name} not found on intercom.io. Moving on" | |
end | |
bar.inc | |
end | |
end | |
bar.finish | |
end | |
puts "Errors:" | |
puts error_log.join("\n") | |
puts "INTERCOM.IO DONE" | |
DashboardTimeoutMonitor.update_timeout('intercom:sync', 1.day.from_now) | |
end | |
desc "Update account days_to_expire" | |
task :accounts_expiration => :environment do |t, args| | |
require 'intercom' | |
require 'progressbar' | |
error_log = [] | |
accounts = Account.organizations.where('trial_start IS NOT NULL').where(plan_id: nil) | |
bar = ProgressBar.new( | |
"Updating accounts expiration days: ", | |
accounts.size | |
) | |
accounts.find_in_batches(batch_size: 50) do |ab| ab.each do |a| | |
next if a.days_to_expire.to_i < -1 | |
begin | |
IntercomService.update_plan_details(a) | |
rescue Intercom::ServiceUnavailableError | |
error_log << "Intercom::ServiceUnavailableError" | |
break | |
rescue IntercomService::IntercomServiceException => e | |
error_log << "#{e.inspect}. Moving on" | |
rescue => e | |
error_log << "Unknown error: #{e}" | |
end | |
bar.inc | |
end end # find_in_batches | |
puts "Errors:" | |
puts error_log.join("\n") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment