From 73a87a6d45abeb8e83b6122b9e29aa184570c15d Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 11 Nov 2024 18:23:14 -0600 Subject: [PATCH] WIP --- ruby/lib/jam_ruby/app/mailers/user_mailer.rb | 6 +- .../jam_ruby/lib/email_new_musician_match.rb | 103 +++++++++--------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index 2c4108565..22c4b062a 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -401,18 +401,18 @@ module JamRuby end end - def new_musicians_match(user, musicians_data) + def new_musicians_match(user, musicians_data, receipents) @user, @musicians_data = user, musicians_data @instrument_proficiencies = { '1': 'Beginner', '2': 'Intermediate', '3': 'Expert' } - sendgrid_recipients([user.email]) + sendgrid_recipients(receipents) sendgrid_substitute('@USERID', [user.id]) sendgrid_unique_args :type => "new_musicians_match" - mail(:to => user.email, :subject => EmailNewMusicianMatch.subject) do |format| + mail(:to => receipents, :subject => EmailNewMusicianMatch.subject) do |format| format.text format.html { render layout: "user_mailer_beta" } end diff --git a/ruby/lib/jam_ruby/lib/email_new_musician_match.rb b/ruby/lib/jam_ruby/lib/email_new_musician_match.rb index bc3ab6534..0e0b6817d 100644 --- a/ruby/lib/jam_ruby/lib/email_new_musician_match.rb +++ b/ruby/lib/jam_ruby/lib/email_new_musician_match.rb @@ -1,8 +1,8 @@ module JamRuby class EmailNewMusicianMatch - PER_PAGE = 150 - LIMIT = 20 + PER_PAGE = 10 + LIMIT = 300 JOINED_WITHIN_DAYS = 7 ACTIVE_WITHIN_DAYS = 30 @@ -27,72 +27,77 @@ module JamRuby limit: PER_PAGE } - email_sending = UserMatchEmailSending.most_recent - if email_sending.nil? || email_sending.completed? - email_sending = UserMatchEmailSending.create - end - - begin - recipients = User.where("users.subscribe_email = ? AND - users.subscribe_email_for_user_match = ? - AND NOT COALESCE(users.user_match_email_sent_at, ?) > ?", - true, true, 7.days.ago, 6.days.ago).where.not(id: email_sending.sent_user_ids).order(" - CASE WHEN users.email IN ('#{PRIORITY_RECIPIENTS.map {|str| "\"#{str}\""}.join(',')}') - THEN 0 ELSE 1 END, last_active_at DESC").select("users.*, - GREATEST(updated_at, last_jam_updated_at) AS last_active_at").limit(LIMIT) + #email_sending = UserMatchEmailSending.most_recent + #if email_sending.nil? || email_sending.completed? + # email_sending = UserMatchEmailSending.create + #end - # If the flag is set to send user match email only to jamkazam team - if Rails.application.config.send_user_match_mail_only_to_jamkazam_team - recipients = recipients.where(email: PRIORITY_RECIPIENTS) + # support easy overrides for iterative testing + limit = ENV["FORCED_BATCH_LIMIT"] || LIMIT + limit = limit.to_i + + send_to_jk_employees = ENV["SEND_TO_JK_EMPLOYEES"] == '1' || APP_CONFIG.send_user_match_mail_only_to_jamkazam_team + + monitoring_email_specified = !APP_CONFIG.user_match_monitoring_email.blank? + + puts "EmailNewMusicianMatch: limit: #{limit}, send_to_jk_employees: #{send_to_jk_employees}" + begin + recipients = User.where("users.subscribe_email = ? AND + users.subscribe_email_for_user_match = ? + AND NOT COALESCE(users.user_match_email_sent_at, ?) > ?", + true, true, 7.days.ago, 6.days.ago) + .where.not(id: email_sending.sent_user_ids) + .order("last_active_at DESC") + .select("users.*, GREATEST(updated_at, last_jam_updated_at) AS last_active_at") + .limit(LIMIT) + + if monitoring_email_specified + AdminMailer.ugly({to: APP_CONFIG.user_match_monitoring_email, + subject:"Weekly user match email sending job started.", + body: "#{email_sending.sent_user_ids.any?? "This job is resuming. It was originally started at #{email_sending.created_at} and has been sent to #{email_sending.sent_user_ids.size} user(s) so far." : "This job was started at #{email_sending.created_at}" }. It will send to total of #{ recipients.size } users."}).deliver_now end - - AdminMailer.ugly({to: APP_CONFIG.user_match_monitoring_email, - subject:"Weekly user match email sending job started.", - body: "#{email_sending.sent_user_ids.any?? "This job is resuming. It was originally started at #{email_sending.created_at} and has been sent to #{email_sending.sent_user_ids.size} user(s) so far." : "This job was started at #{email_sending.created_at}" }. It will send to total of #{ recipients.size } users."}).deliver_now recipients.find_each do |user| ip_address = user.last_jam_addr.blank?? '127.0.0.1' : IPAddr.new(user.last_jam_addr, Socket::AF_INET).to_s matched_musician_data = [] - nextOffset = 0 + params.merge!({ offset: nextOffset }) + results, latency_data, nextOffset = JamRuby::MusicianFilter.filter(user, ip_address, params) + matched_musician_data << [{ musicians: results, latencies: latency_data }] if results && results.size > 0 - while !nextOffset.nil? && nextOffset >= 0 do - - params.merge!({ offset: nextOffset }) - results, latency_data, nextOffset = JamRuby::MusicianFilter.filter(user, ip_address, params) - - matched_musician_data << [{ musicians: results, latencies: latency_data }] if results && results.size > 0 - end + send_to_receipents = send_to_jk_employees ? PRIORITY_RECIPIENTS : [user.email] + user.update_column(:user_match_email_sent_at, Time.now, :user_match_num_matched, matched_musician_data.size) if matched_musician_data.size > 0 - - UserMailer.new_musicians_match(user, matched_musician_data).deliver_now - - user.update_column(:user_match_email_sent_at, Time.now) - email_sending.sent_user_ids.push(user.id) - email_sending.save! + UserMailer.new_musicians_match(user, matched_musician_data, send_to_receipents).deliver_now + #email_sending.sent_user_ids.push(user.id) + #email_sending.save! end end - email_sending.total_recipients = email_sending.sent_user_ids.size - email_sending.completed_at = Time.now - email_sending.save! + #email_sending.total_recipients = email_sending.sent_user_ids.size + #email_sending.completed_at = Time.now + #email_sending.save! - AdminMailer.ugly({ - to: APP_CONFIG.user_match_monitoring_email, - subject:"Weekly user match email sending completed.", - body: "Weekly email sending job was completed at #{Time.now}. It was sent to #{email_sending.sent_user_ids.size} user(s)." - }).deliver_now + if monitoring_email_specified + AdminMailer.ugly({ + to: APP_CONFIG.user_match_monitoring_email, + subject:"Weekly user match email sending completed.", + body: "Weekly email sending job was completed at #{Time.now}. It was sent to #{email_sending.sent_user_ids.size} user(s)." + }).deliver_now + end rescue => exception begin - fail_count = email_sending.fail_count - email_sending.update_attributes(fail_count: fail_count + 1, exception_detail: exception.message) + #fail_count = email_sending.fail_count + #email_sending.update_attributes(fail_count: fail_count + 1, exception_detail: exception.message) - AdminMailer.ugly({to: APP_CONFIG.user_match_monitoring_email, - subject:"Error occured when sending weekly user match email.", - body: "An error was encountered at #{Time.now} while sending weekly user match email - #{exception.message}."}).deliver_now + if monitoring_email_specified + AdminMailer.ugly({to: APP_CONFIG.user_match_monitoring_email, + subject:"Error occured when sending weekly user match email.", + body: "An error was encountered at #{Time.now} while sending weekly user match email - #{exception.message}."}).deliver_now + end rescue Bugsnag.notify(exception) end