From 75e2c7e460d1e2a4474336c4aeabc42589fbeb07 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Thu, 10 Mar 2011 10:52:37 +0100 Subject: [PATCH] adrian tried to create a delayed job for the download statistics: actually doing it now --- Rakefile | 2 ++ app/helpers/search_helper.rb | 19 ++++--------------- config/deploy.rb | 3 +++ config/environment.rb | 1 + db/schema.rb | 2 ++ lib/tasks/delayed_job.rake | 7 +++---- lib/workers/search_helper_job.rb | 13 +++++++++++-- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Rakefile b/Rakefile index 3bb0e85..476a314 100644 --- a/Rakefile +++ b/Rakefile @@ -8,3 +8,5 @@ require 'rake/testtask' require 'rake/rdoctask' require 'tasks/rails' +require 'delayed/tasks' + diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 238b994..62d5ea3 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -1,3 +1,5 @@ +require 'workers/search_helper_job' + module SearchHelper def shorten_description(text, chars) text.sub /^(.{0,#{chars}}\b).*/m, '\1' @@ -53,23 +55,10 @@ module SearchHelper def top_downloads r = Rails.cache.read('top_downloads') - r = top_downloads_update unless r + # it's possible we will have to enqueue one on cold caches + Delayed::Job.enqueue SearchHelperJob.new unless r return r end - def top_downloads_update - time_limit = DateTime.parse 3.months.ago.to_s - result = ActiveRecord::Base.connection.execute("select query, count(*) as c from download_histories where query is NOT NULL AND created_at > '#{time_limit}' group by query order by c desc limit 15") - - top = Array.new - result.each do |entry| - top << { :query => entry[0].strip.downcase, :count => entry[1].to_i} - end - - Rails.cache.write('top_downloads', top) - - return top - end - end diff --git a/config/deploy.rb b/config/deploy.rb index 5f096b1..d5ddc4c 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -75,14 +75,17 @@ end namespace :deploy do task :start do run "sv start /service/#{runit_name}-*" + run "sv start /service/delayed_job_software" end task :restart do run "for i in /service/#{runit_name}-*; do sv restart $i; sleep 3; done" + run "sv restart /service/delayed_job_software" end task :stop do run "sv stop /service/#{runit_name}-*" + run "sv stop /service/delayed_job_software" end diff --git a/config/environment.rb b/config/environment.rb index 9d9e338..ccd94ec 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -44,6 +44,7 @@ Rails::Initializer.run do |config| config.gem 'libxml-ruby' config.gem 'gettext_rails' + config.gem 'daemons' config.gem 'delayed_job' config.logger = NiceLogger.new(config.log_path) diff --git a/db/schema.rb b/db/schema.rb index 04860a8..ca7c9f1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -32,6 +32,8 @@ ActiveRecord::Schema.define(:version => 20110227150000) do t.datetime "created_at" end + add_index "download_histories", ["query"], :name => "index_download_histories_on_query" + create_table "missing_codecs", :force => true do |t| t.integer "visitor_id" t.string "framework" diff --git a/lib/tasks/delayed_job.rake b/lib/tasks/delayed_job.rake index 4bef2e2..d6292bd 100644 --- a/lib/tasks/delayed_job.rake +++ b/lib/tasks/delayed_job.rake @@ -1,8 +1,7 @@ require 'workers/search_helper_job.rb' -task(:clean_stats => :environment) do - desc "Inject a job to clean up the search cache" - task(:searchhelper => :environment) { Delayed::Job.enqueue SearchHelperJob.new } - +namespace :jobs do + desc "Inject a job to update the search cache" + task(:searchhelper => :environment) { Delayed::Job.enqueue SearchHelperJob.new } end diff --git a/lib/workers/search_helper_job.rb b/lib/workers/search_helper_job.rb index 2bb1599..3b6e992 100644 --- a/lib/workers/search_helper_job.rb +++ b/lib/workers/search_helper_job.rb @@ -5,8 +5,17 @@ class SearchHelperJob end def perform - c = SearchHelper.new - c.top_downloads_update + time_limit = DateTime.parse 3.months.ago.to_s + result = ActiveRecord::Base.connection.execute("select query, count(*) as c from download_histories where query is NOT NULL AND created_at > '#{time_limit}' group by query order by c desc limit 15") + + top = Array.new + result.each do |entry| + top << { :query => entry[0].strip.downcase, :count => entry[1].to_i} + end + + Rails.cache.write('top_downloads', top) + + return top end end -- 2.1.4