jobs: migrate saved searches to ActiveJob.

* Fix tests to run the searches for real instead of mocking everything out.

* Fix SavedSearch.populate to only use the read only database in
  production because in breaks things in tests. Specifically:
  the posts get created in one db connection but searched for in
  another, but the second transaction doesn't see the uncommitted posts
  in the first transaction, so the search doesn't work.
This commit is contained in:
evazion
2019-08-16 20:49:35 -05:00
parent a68db501c2
commit 2bbdc5d143
3 changed files with 55 additions and 40 deletions

View File

@@ -0,0 +1,7 @@
class PopulateSavedSearchJob < ApplicationJob
queue_as :default
def perform(query)
SavedSearch.populate(query)
end
end

View File

@@ -32,7 +32,7 @@ class SavedSearch < ApplicationRecord
post_ids.merge(sub_ids)
update_count += 1
else
SavedSearch.delay(queue: "default").populate(query)
PopulateSavedSearchJob.perform_later(query)
end
end
post_ids.to_a.sort.last(QUERY_LIMIT)
@@ -97,7 +97,7 @@ class SavedSearch < ApplicationRecord
CurrentUser.as_system do
redis_key = "search:#{query}"
return if redis.exists(redis_key)
post_ids = Post.tag_match(query, read_only: true).limit(QUERY_LIMIT).pluck(:id)
post_ids = Post.tag_match(query, read_only: Rails.env.production?).limit(QUERY_LIMIT).pluck(:id)
redis.sadd(redis_key, post_ids)
redis.expire(redis_key, REDIS_EXPIRY)
end