docbrown/app/workers/articles/update_page_views_worker.rb
Mac Siri 092ee4a425
Implement PageViewRollup (#20670)
* Refactor

* Create PageViewRollup WIP

* Create PageViewRollupWorker and specs

* Add spec WIP

* Write spec

* Refactor

* Add spec

* Prevent retry

* Update baseline date

* Improve specs

* Update ATTRIBUTES_

* Update spec again
2024-03-18 20:33:12 +00:00

35 lines
1.3 KiB
Ruby

module Articles
class UpdatePageViewsWorker
include Sidekiq::Job
sidekiq_options queue: :medium_priority,
lock: :until_executing,
on_conflict: :replace,
retry: false
# @see Articles::PageViewUpdater
def perform(create_params)
article = Article.find_by(id: create_params["article_id"])
return unless article&.published?
return if create_params[:user_id] && article.user_id == create_params[:user_id]
PageView.create!(create_params)
updated_count = article.page_views.sum(:counts_for_number_of_views)
if updated_count > article.page_views_count
article.update_column(:page_views_count, updated_count)
end
# PageViewsController#create called the method update_organic_page_views
# at the end. The relationship between the two was 12.5% chance (rand(8))
# and 1% chance (rand(100)), or roughly 12x more likely for page view
# updates vs. organic page view updates. We kept a similar relationship
# between the two workers, this one here is schedule after 2 minutes,
# organic page view updates after 25 minutes.
Articles::UpdateOrganicPageViewsWorker.perform_at(
25.minutes.from_now,
article.id,
)
end
end
end