From 76166567fbd154b3457542aef12233cb60ef2eb8 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 12 Jun 2014 11:29:29 +1000 Subject: [PATCH] Use the cheap Defer queue as opposed to sidekiq for view tracking. --- app/controllers/topics_controller.rb | 17 +++++++++++------ app/jobs/regular/view_tracker.rb | 17 ----------------- spec/controllers/topics_controller_spec.rb | 3 +++ 3 files changed, 14 insertions(+), 23 deletions(-) delete mode 100644 app/jobs/regular/view_tracker.rb diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 71c6a46be..7c617514e 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -365,12 +365,17 @@ class TopicsController < ApplicationController end def track_visit_to_topic - Jobs.enqueue(:view_tracker, - topic_id: @topic_view.topic.id, - ip: request.remote_ip, - user_id: (current_user.id if current_user), - track_visit: should_track_visit_to_topic? - ) + topic_id = @topic_view.topic.id + ip = request.remote_ip + user_id = (current_user.id if current_user) + track_visit = should_track_visit_to_topic? + + Scheduler::Defer.later do + View.create_for_parent(Topic, topic_id, ip, user_id) + if track_visit + TopicUser.track_visit! topic_id, user_id + end + end end diff --git a/app/jobs/regular/view_tracker.rb b/app/jobs/regular/view_tracker.rb deleted file mode 100644 index 7b0d11a5b..000000000 --- a/app/jobs/regular/view_tracker.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Jobs - - class ViewTracker < Jobs::Base - def execute(args) - topic_id = args[:topic_id] - user_id = args[:user_id] - ip = args[:ip] - track_visit = args[:track_visit] - - View.create_for_parent(Topic, topic_id, ip, user_id) - if track_visit - TopicUser.track_visit! topic_id, user_id - end - end - end - -end diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 432f83376..7be236f08 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -660,6 +660,9 @@ describe TopicsController do it 'shows the topic if valid api key is provided' do get :show, topic_id: topic.id, slug: topic.slug, api_key: api_key.key expect(response).to be_successful + topic.reload + # free test, only costs a reload + topic.views.should == 1 end it 'returns 403 for an invalid key' do