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:
7
app/jobs/populate_saved_search_job.rb
Normal file
7
app/jobs/populate_saved_search_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class PopulateSavedSearchJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(query)
|
||||
SavedSearch.populate(query)
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user