Skip to content
Permalink
Browse files

DEV: s/\$redis/Discourse\.redis (#8431)

This commit also adds a rubocop rule to prevent global variables.
  • Loading branch information
jjaffeux committed Dec 3, 2019
1 parent 9eccfb7 commit 0d3d2c43a0dd420e7702f329d4ffb7d63fe4c326
Showing with 378 additions and 362 deletions.
  1. +13 −4 .rubocop.yml
  2. +2 −2 app/controllers/application_controller.rb
  3. +1 −1 app/controllers/forums_controller.rb
  4. +3 −3 app/controllers/session_controller.rb
  5. +1 −1 app/controllers/user_api_keys_controller.rb
  6. +2 −2 app/controllers/users/associate_accounts_controller.rb
  7. +1 −1 app/controllers/users/omniauth_callbacks_controller.rb
  8. +2 −2 app/controllers/webhooks_controller.rb
  9. +1 −1 app/helpers/application_helper.rb
  10. +1 −1 app/jobs/onceoff/clean_up_sidekiq_statistic.rb
  11. +2 −2 app/jobs/onceoff/onceoff.rb
  12. +2 −2 app/jobs/regular/run_heartbeat.rb
  13. +3 −3 app/jobs/scheduled/clean_up_uploads.rb
  14. +2 −2 app/jobs/scheduled/pending_queued_posts_reminder.rb
  15. +3 −3 app/jobs/scheduled/pending_reviewables_reminder.rb
  16. +2 −2 app/jobs/scheduled/pending_users_reminder.rb
  17. +6 −6 app/jobs/scheduled/poll_mailbox.rb
  18. +2 −2 app/mailers/user_notifications.rb
  19. +8 −8 app/models/admin_dashboard_data.rb
  20. +1 −1 app/models/application_request.rb
  21. +3 −3 app/models/category_featured_topic.rb
  22. +5 −5 app/models/concerns/cached_counting.rb
  23. +2 −2 app/models/concerns/stats_cacheable.rb
  24. +4 −4 app/models/global_setting.rb
  25. +4 −4 app/models/post.rb
  26. +4 −4 app/models/search_log.rb
  27. +5 −5 app/models/site.rb
  28. +2 −2 app/models/topic_link_click.rb
  29. +2 −2 app/models/topic_view_item.rb
  30. +6 −6 app/models/trust_level3_requirements.rb
  31. +2 −2 app/models/user.rb
  32. +2 −2 app/models/user_option.rb
  33. +2 −2 app/models/user_profile_view.rb
  34. +2 −2 app/models/user_stat.rb
  35. +6 −6 app/models/web_crawler_request.rb
  36. +3 −3 app/services/badge_granter.rb
  37. +2 −2 app/services/group_message.rb
  38. +9 −9 app/services/random_topic_selector.rb
  39. +1 −1 app/services/topic_timestamp_changer.rb
  40. +1 −1 config/application.rb
  41. +1 −1 config/initializers/001-redis.rb
  42. +1 −1 config/initializers/100-sidekiq.rb
  43. +1 −1 config/initializers/100-watch_for_restart.rb
  44. +2 −2 config/unicorn.conf.rb
  45. +4 −4 db/migrate/20150213174159_create_digest_unsubscribe_keys.rb
  46. +1 −1 db/migrate/20160303183607_clear_common_passwords_cache.rb
  47. +6 −6 lib/admin_confirmation.rb
  48. +1 −1 lib/auth/default_current_user_provider.rb
  49. +1 −1 lib/auth/open_id_authenticator.rb
  50. +9 −9 lib/backup_restore.rb
  51. +2 −2 lib/cache.rb
  52. +2 −2 lib/common_passwords/common_passwords.rb
  53. +22 −15 lib/discourse.rb
  54. +2 −2 lib/discourse_hub.rb
  55. +1 −1 lib/discourse_redis.rb
  56. +14 −14 lib/discourse_updates.rb
  57. +2 −2 lib/distributed_memoizer.rb
  58. +1 −1 lib/distributed_mutex.rb
  59. +2 −2 lib/email/processor.rb
  60. +3 −3 lib/email_backup_token.rb
  61. +3 −3 lib/final_destination.rb
  62. +7 −7 lib/middleware/anonymous_cache.rb
  63. +3 −3 lib/oneboxer.rb
  64. +4 −4 lib/post_revisor.rb
  65. +5 −5 lib/rate_limiter.rb
  66. +5 −5 lib/secure_session.rb
  67. +4 −4 lib/sidekiq/pausable.rb
  68. +2 −2 lib/stylesheet/watcher.rb
  69. +1 −1 lib/tasks/redis.rake
  70. +4 −4 lib/tasks/typepad.thor
  71. +2 −2 lib/topic_retriever.rb
  72. +3 −3 plugins/discourse-narrative-bot/lib/discourse_narrative_bot/actions.rb
  73. +6 −6 plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb
  74. +7 −7 plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
  75. +6 −6 plugins/discourse-presence/plugin.rb
  76. +4 −4 plugins/discourse-presence/spec/presence_manager_spec.rb
  77. +4 −4 plugins/discourse-presence/spec/requests/presence_controller_spec.rb
  78. +4 −4 script/benchmarks/cache/bench.rb
  79. +1 −1 script/redis_memory.rb
  80. +1 −1 spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb
  81. +2 −2 spec/components/auth/default_current_user_provider_spec.rb
  82. +4 −4 spec/components/cache_spec.rb
  83. +3 −3 spec/components/discourse_redis_spec.rb
  84. +8 −8 spec/components/discourse_spec.rb
  85. +3 −3 spec/components/distributed_memoizer_spec.rb
  86. +9 −9 spec/components/distributed_mutex_spec.rb
  87. +4 −4 spec/components/email/processor_spec.rb
  88. +1 −1 spec/components/middleware/anonymous_cache_spec.rb
  89. +1 −1 spec/components/middleware/request_tracker_spec.rb
  90. +1 −1 spec/components/post_revisor_spec.rb
  91. +2 −2 spec/components/pretty_text_spec.rb
  92. +2 −2 spec/components/rate_limiter_spec.rb
  93. +1 −1 spec/components/topic_query_spec.rb
  94. +1 −1 spec/components/validators/post_validator_spec.rb
  95. +3 −3 spec/jobs/about_stats_spec.rb
  96. +1 −1 spec/jobs/poll_mailbox_spec.rb
  97. +1 −1 spec/mailers/user_notifications_spec.rb
  98. +5 −5 spec/models/application_request_spec.rb
  99. +3 −3 spec/models/global_setting_spec.rb
  100. +1 −1 spec/models/search_log_spec.rb
  101. +1 −1 spec/models/topic_spec.rb
  102. +1 −1 spec/models/user_spec.rb
  103. +2 −2 spec/models/user_stat_spec.rb
  104. +4 −4 spec/models/web_crawler_request_spec.rb
  105. +1 −1 spec/rails_helper.rb
  106. +1 −1 spec/requests/admin/backups_controller_spec.rb
  107. +1 −1 spec/requests/admin/users_controller_spec.rb
  108. +2 −2 spec/requests/application_controller_spec.rb
  109. +2 −2 spec/requests/search_controller_spec.rb
  110. +6 −6 spec/requests/session_controller_spec.rb
  111. +2 −2 spec/requests/user_api_keys_controller_spec.rb
  112. +1 −1 spec/requests/webhooks_controller_spec.rb
  113. +6 −6 spec/services/group_message_spec.rb
  114. +4 −4 spec/services/random_topic_selector_spec.rb
  115. +4 −4 spec/services/topic_timestamp_changer_spec.rb
  116. +1 −1 spec/services/word_watcher_spec.rb
  117. +3 −3 spec/support/shared_examples_for_stats_cacheable.rb
  118. +1 −1 spec/tasks/redis_spec.rb
@@ -132,8 +132,17 @@ Style/Semicolon:
Style/RedundantReturn:
Enabled: true

DiscourseCops/NoChdir:
Enabled: true
Exclude:
- 'spec/**/*' # Specs are run sequentially, so chdir can be used
DiscourseCops/NoChdir:
Enabled: true
Exclude:
- 'spec/**/*' # Specs are run sequentially, so chdir can be used
- 'plugins/*/spec/**/*'

Style/GlobalVars:
Enabled: true
Severity: warning
Exclude:
- 'lib/tasks/**/*'
- 'script/**/*'
- 'spec/**/*.rb'
- 'plugins/*/spec/**/*'
@@ -768,14 +768,14 @@ def build_not_found_page(opts = {})

if !SiteSetting.login_required? || (current_user rescue false)
key = "page_not_found_topics"
if @topics_partial = $redis.get(key)
if @topics_partial = Discourse.redis.get(key)
@topics_partial = @topics_partial.html_safe
else
category_topic_ids = Category.pluck(:topic_id).compact
@top_viewed = TopicQuery.new(nil, except_topic_ids: category_topic_ids).list_top_for("monthly").topics.first(10)
@recent = Topic.includes(:category).where.not(id: category_topic_ids).recent(10)
@topics_partial = render_to_string partial: '/exceptions/not_found_topics', formats: [:html]
$redis.setex(key, 10.minutes, @topics_partial)
Discourse.redis.setex(key, 10.minutes, @topics_partial)
end
end

@@ -9,7 +9,7 @@ class ForumsController < ActionController::Base
after_action :add_readonly_header

def status
if $shutdown
if $shutdown # rubocop:disable Style/GlobalVars
render plain: "shutting down", status: 500
else
render plain: "ok"
@@ -409,15 +409,15 @@ def email_login
end

def one_time_password
@otp_username = otp_username = $redis.get "otp_#{params[:token]}"
@otp_username = otp_username = Discourse.redis.get "otp_#{params[:token]}"

if otp_username && user = User.find_by_username(otp_username)
if current_user&.username == otp_username
$redis.del "otp_#{params[:token]}"
Discourse.redis.del "otp_#{params[:token]}"
return redirect_to path("/")
elsif request.post?
log_on_user(user)
$redis.del "otp_#{params[:token]}"
Discourse.redis.del "otp_#{params[:token]}"
return redirect_to path("/")
else
# Display the form
@@ -205,7 +205,7 @@ def one_time_password(public_key, username)
raise Discourse::InvalidAccess unless UserApiKey.allowed_scopes.superset?(Set.new(["one_time_password"]))

otp = SecureRandom.hex
$redis.setex "otp_#{otp}", 10.minutes, username
Discourse.redis.setex "otp_#{otp}", 10.minutes, username

Base64.encode64(public_key.public_encrypt(otp))
end
@@ -21,7 +21,7 @@ def connect_info
# Presents a confirmation screen to the user. Accessed via GET, with no CSRF checks
def connect
auth = get_auth_hash
$redis.del "#{REDIS_PREFIX}_#{current_user&.id}_#{params[:token]}"
Discourse.redis.del "#{REDIS_PREFIX}_#{current_user&.id}_#{params[:token]}"

provider_name = auth.provider
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
@@ -37,7 +37,7 @@ def connect

def get_auth_hash
token = params[:token]
json = $redis.get "#{REDIS_PREFIX}_#{current_user&.id}_#{token}"
json = Discourse.redis.get "#{REDIS_PREFIX}_#{current_user&.id}_#{token}"
raise Discourse::NotFound if json.nil?

OmniAuth::AuthHash.new(JSON.parse(json))
@@ -31,7 +31,7 @@ def complete
if session.delete(:auth_reconnect) && authenticator.can_connect_existing_user? && current_user
# Save to redis, with a secret token, then redirect to confirmation screen
token = SecureRandom.hex
$redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
Discourse.redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
return redirect_to Discourse.base_uri("/associate/#{token}")
else
@auth_result = authenticator.after_authenticate(auth)
@@ -119,8 +119,8 @@ def valid_mailgun_signature?(token, timestamp, signature)

# prevent replay attacks
key = "mailgun_token_#{token}"
return false unless $redis.setnx(key, 1)
$redis.expire(key, 10.minutes)
return false unless Discourse.redis.setnx(key, 1)
Discourse.redis.expire(key, 10.minutes)

# ensure timestamp isn't too far from current time
return false if (Time.at(timestamp.to_i) - Time.now).abs > 12.hours.to_i
@@ -41,7 +41,7 @@ def shared_session_key
return request.env[sk] if request.env[sk]

request.env[sk] = key = (session[sk] ||= SecureRandom.hex)
$redis.setex "#{sk}_#{key}", 7.days, current_user.id.to_s
Discourse.redis.setex "#{sk}_#{key}", 7.days, current_user.id.to_s
key
end
end
@@ -3,7 +3,7 @@
module Jobs
class CleanUpSidekiqStatistic < ::Jobs::Onceoff
def execute_onceoff(args)
$redis.without_namespace.del('sidekiq:sidekiq:statistic')
Discourse.redis.without_namespace.del('sidekiq:sidekiq:statistic')
end
end
end
@@ -16,7 +16,7 @@ def running_key_name
# Pass `force: true` to force it happen again
def execute(args)
job_name = self.class.name_for(self.class)
has_lock = $redis.setnx(running_key_name, Time.now.to_i)
has_lock = Discourse.redis.setnx(running_key_name, Time.now.to_i)

# If we can't get a lock, just noop
if args[:force] || has_lock
@@ -25,7 +25,7 @@ def execute(args)
execute_onceoff(args)
OnceoffLog.create!(job_name: job_name)
ensure
$redis.del(running_key_name) if has_lock
Discourse.redis.del(running_key_name) if has_lock
end
end

@@ -10,11 +10,11 @@ def self.heartbeat_key
end

def execute(args)
$redis.set(self.class.heartbeat_key, Time.new.to_i.to_s)
Discourse.redis.set(self.class.heartbeat_key, Time.new.to_i.to_s)
end

def self.last_heartbeat
$redis.get(heartbeat_key).to_i
Discourse.redis.get(heartbeat_key).to_i
end
end
end
@@ -96,16 +96,16 @@ def execute(args)
end

def last_cleanup=(v)
$redis.setex(last_cleanup_key, 7.days.to_i, v.to_s)
Discourse.redis.setex(last_cleanup_key, 7.days.to_i, v.to_s)
end

def last_cleanup
v = $redis.get(last_cleanup_key)
v = Discourse.redis.get(last_cleanup_key)
v ? v.to_i : v
end

def reset_last_cleanup!
$redis.del(last_cleanup_key)
Discourse.redis.del(last_cleanup_key)
end

protected
@@ -33,11 +33,11 @@ def should_notify_ids
end

def last_notified_id
(i = $redis.get(self.class.last_notified_key)) && i.to_i
(i = Discourse.redis.get(self.class.last_notified_key)) && i.to_i
end

def last_notified_id=(arg)
$redis.set(self.class.last_notified_key, arg)
Discourse.redis.set(self.class.last_notified_key, arg)
end

def self.last_notified_key
@@ -37,19 +37,19 @@ def execute(args)
end

def self.last_notified_id
$redis.get(last_notified_key).to_i
Discourse.redis.get(last_notified_key).to_i
end

def self.last_notified_id=(arg)
$redis.set(last_notified_key, arg)
Discourse.redis.set(last_notified_key, arg)
end

def self.last_notified_key
"last_notified_reviewable_id".freeze
end

def self.clear_key
$redis.del(last_notified_key)
Discourse.redis.del(last_notified_key)
end

def active_moderator_usernames
@@ -50,11 +50,11 @@ def execute(args)
end

def previous_newest_username
$redis.get previous_newest_username_cache_key
Discourse.redis.get previous_newest_username_cache_key
end

def previous_newest_username=(username)
$redis.setex previous_newest_username_cache_key, 7.days, username
Discourse.redis.setex previous_newest_username_cache_key, 7.days, username
end

def previous_newest_username_cache_key
@@ -43,15 +43,15 @@ def poll_pop3
end
end
rescue Net::OpenTimeout => e
count = $redis.incr(POLL_MAILBOX_TIMEOUT_ERROR_KEY).to_i
count = Discourse.redis.incr(POLL_MAILBOX_TIMEOUT_ERROR_KEY).to_i

$redis.expire(
Discourse.redis.expire(
POLL_MAILBOX_TIMEOUT_ERROR_KEY,
SiteSetting.pop3_polling_period_mins.minutes * 3
) if count == 1

if count > 3
$redis.del(POLL_MAILBOX_TIMEOUT_ERROR_KEY)
Discourse.redis.del(POLL_MAILBOX_TIMEOUT_ERROR_KEY)
mark_as_errored!
add_admin_dashboard_problem_message('dashboard.poll_pop3_timeout')
Discourse.handle_job_exception(e, error_context(@args, "Connecting to '#{SiteSetting.pop3_polling_host}' for polling emails."))
@@ -65,13 +65,13 @@ def poll_pop3
POLL_MAILBOX_ERRORS_KEY ||= "poll_mailbox_errors".freeze

def self.errors_in_past_24_hours
$redis.zremrangebyscore(POLL_MAILBOX_ERRORS_KEY, 0, 24.hours.ago.to_i)
$redis.zcard(POLL_MAILBOX_ERRORS_KEY).to_i
Discourse.redis.zremrangebyscore(POLL_MAILBOX_ERRORS_KEY, 0, 24.hours.ago.to_i)
Discourse.redis.zcard(POLL_MAILBOX_ERRORS_KEY).to_i
end

def mark_as_errored!
now = Time.now.to_i
$redis.zadd(POLL_MAILBOX_ERRORS_KEY, now, now.to_s)
Discourse.redis.zadd(POLL_MAILBOX_ERRORS_KEY, now, now.to_s)
end

def add_admin_dashboard_problem_message(i18n_key)
@@ -697,9 +697,9 @@ def self.summary_new_users_count_key(min_date_str)
def summary_new_users_count(min_date)
min_date_str = min_date.is_a?(String) ? min_date : min_date.strftime('%Y-%m-%d')
key = self.class.summary_new_users_count_key(min_date_str)
((count = $redis.get(key)) && count.to_i) || begin
((count = Discourse.redis.get(key)) && count.to_i) || begin
count = User.real.where(active: true, staged: false).not_suspended.where("created_at > ?", min_date_str).count
$redis.setex(key, 1.day, count)
Discourse.redis.setex(key, 1.day, count)
count
end
end
@@ -63,16 +63,16 @@ def self.problems_started_key
end

def self.set_problems_started
existing_time = $redis.get(problems_started_key)
$redis.setex(problems_started_key, 14.days.to_i, existing_time || Time.zone.now.to_s)
existing_time = Discourse.redis.get(problems_started_key)
Discourse.redis.setex(problems_started_key, 14.days.to_i, existing_time || Time.zone.now.to_s)
end

def self.clear_problems_started
$redis.del problems_started_key
Discourse.redis.del problems_started_key
end

def self.problems_started_at
s = $redis.get(problems_started_key)
s = Discourse.redis.get(problems_started_key)
s ? Time.zone.parse(s) : nil
end

@@ -109,19 +109,19 @@ def self.fetch_problems(opts = {})
end

def self.problem_message_check(i18n_key)
$redis.get(problem_message_key(i18n_key)) ? I18n.t(i18n_key, base_path: Discourse.base_path) : nil
Discourse.redis.get(problem_message_key(i18n_key)) ? I18n.t(i18n_key, base_path: Discourse.base_path) : nil
end

def self.add_problem_message(i18n_key, expire_seconds = nil)
if expire_seconds.to_i > 0
$redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
Discourse.redis.setex problem_message_key(i18n_key), expire_seconds.to_i, 1
else
$redis.set problem_message_key(i18n_key), 1
Discourse.redis.set problem_message_key(i18n_key), 1
end
end

def self.clear_problem_message(i18n_key)
$redis.del problem_message_key(i18n_key)
Discourse.redis.del problem_message_key(i18n_key)
end

def self.problem_message_key(i18n_key)
@@ -52,7 +52,7 @@ def self.clear_cache!(date = nil)

req_types.each do |req_type, _|
key = redis_key(req_type, date)
$redis.del key
Discourse.redis.del key
end
end

@@ -16,7 +16,7 @@ def self.feature_topics(batched: false, batch_size: nil)

batch_size ||= DEFAULT_BATCH_SIZE

next_category_id = batched ? $redis.get(NEXT_CATEGORY_ID_KEY).to_i : 0
next_category_id = batched ? Discourse.redis.get(NEXT_CATEGORY_ID_KEY).to_i : 0

categories = Category.select(:id, :topic_id, :num_featured_topics)
.where('id >= ?', next_category_id)
@@ -27,7 +27,7 @@ def self.feature_topics(batched: false, batch_size: nil)
if batched
if categories.length == batch_size
next_id = Category.where('id > ?', categories.last.id).order('id asc').limit(1).pluck(:id)[0]
next_id ? $redis.setex(NEXT_CATEGORY_ID_KEY, 1.day, next_id) : clear_batch!
next_id ? Discourse.redis.setex(NEXT_CATEGORY_ID_KEY, 1.day, next_id) : clear_batch!
else
clear_batch!
end
@@ -39,7 +39,7 @@ def self.feature_topics(batched: false, batch_size: nil)
end

def self.clear_batch!
$redis.del(NEXT_CATEGORY_ID_KEY)
Discourse.redis.del(NEXT_CATEGORY_ID_KEY)
end

def self.feature_topics_for(c, existing = nil)
@@ -19,13 +19,13 @@ class << self

class_methods do
def perform_increment!(key, opts = nil)
val = $redis.incr(key).to_i
val = Discourse.redis.incr(key).to_i

# readonly mode it is going to be 0, skip
return if val == 0

# 3.days, see: https://github.com/rails/rails/issues/21296
$redis.expire(key, 259200)
Discourse.redis.expire(key, 259200)

autoflush = (opts && opts[:autoflush]) || self.autoflush
if autoflush > 0 && val >= autoflush
@@ -51,9 +51,9 @@ def write_cache!(date = nil)
# this may seem a bit fancy but in so it allows
# for concurrent calls without double counting
def get_and_reset(key)
namespaced_key = $redis.namespace_key(key)
val = $redis.without_namespace.eval(GET_AND_RESET, keys: [namespaced_key]).to_i
$redis.expire(key, 259200) # SET removes expiry, so set it again
namespaced_key = Discourse.redis.namespace_key(key)
val = Discourse.redis.without_namespace.eval(GET_AND_RESET, keys: [namespaced_key]).to_i
Discourse.redis.expire(key, 259200) # SET removes expiry, so set it again
val
end

0 comments on commit 0d3d2c4

Please sign in to comment.
You can’t perform that action at this time.