follow_recommendations_scheduler.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. class Scheduler::FollowRecommendationsScheduler
  3. include Sidekiq::Worker
  4. include Redisable
  5. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i
  6. # The maximum number of accounts that can be requested in one page from the
  7. # API is 80, and the suggestions API does not allow pagination. This number
  8. # leaves some room for accounts being filtered during live access
  9. SET_SIZE = 100
  10. def perform
  11. # Maintaining a materialized view speeds-up subsequent queries significantly
  12. AccountSummary.refresh
  13. FollowRecommendation.refresh
  14. fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE)
  15. Trends.available_locales.each do |locale|
  16. recommendations = if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist
  17. FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.rank, recommendation.account_id] }
  18. else
  19. []
  20. end
  21. # Use language-agnostic results if there are not enough language-specific ones
  22. missing = SET_SIZE - recommendations.size
  23. if missing.positive? && fallback_recommendations.size.positive?
  24. max_fallback_rank = fallback_recommendations.first.rank || 0
  25. # Language-specific results should be above language-agnostic ones,
  26. # otherwise language-agnostic ones will always overshadow them
  27. recommendations.map! { |(rank, account_id)| [rank + max_fallback_rank, account_id] }
  28. added = 0
  29. fallback_recommendations.each do |recommendation|
  30. next if recommendations.any? { |(_, account_id)| account_id == recommendation.account_id }
  31. recommendations << [recommendation.rank, recommendation.account_id]
  32. added += 1
  33. break if added >= missing
  34. end
  35. end
  36. redis.multi do |multi|
  37. multi.del(key(locale))
  38. multi.zadd(key(locale), recommendations)
  39. end
  40. end
  41. end
  42. private
  43. def key(locale)
  44. "follow_recommendations:#{locale}"
  45. end
  46. end