Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding in delayed job for title tracking

  • Loading branch information...
commit d95cfb5039dbadb028d98155ab37e2bfdf405c52 1 parent d2821e8
@thejefflarson thejefflarson authored
View
7 Rakefile
@@ -8,3 +8,10 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
+
+begin
+ gem 'delayed_job', '~>2.0.4'
+ require 'delayed/tasks'
+rescue LoadError
+ STDERR.puts "Run `rake gems:install` to install delayed_job"
+end
View
29 app/models/stat.rb
@@ -1,15 +1,15 @@
$KCODE = 'UTF-8'
-class Stat < ActiveRecord::Base
+class Stat < ActiveRecord::Base
validates_presence_of :url, :hits
validate_on_create :log_hits?
- validate :url_format
+ validate :url_format
named_scope :aggregate, :order => "hits DESC", :select => ["sum(hits) as hits, url, title"], :group => :url
-
-
+
+
def title=(t)
- write_attribute :title, HTML::FullSanitizer.new.sanitize(t)
+ write_attribute :title, HTML::FullSanitizer.new.sanitize(t)
end
-
+
def title
t = read_attribute :title
if t.nil?
@@ -18,7 +18,7 @@ def title
req.perform
self.title = Nokogiri::HTML.parse(req.body_str).css('title').text
# catch bad urls
- rescue Curl::Err::HostResolutionError
+ rescue Curl::Err::HostResolutionError
self.title = "Inaccessible URL"
ensure
save
@@ -27,23 +27,23 @@ def title
self.title = "No Title" if read_attribute(:title).blank?
read_attribute :title
end
-
+
private
-
+
def url_format
u = URI.parse url
- case
+ case
when u.scheme.nil? then errors.add(:url, "no scheme")
when u.host.nil? then errors.add(:url, "no host")
end
rescue URI::BadURIError, URI::InvalidURIError
errors.add(:url, "bad url")
end
-
+
def record_todays_date
self.date_for = Date.today
end
-
+
def log_hits?
record_todays_date
if Stat.exists? :url => url, :date_for => date_for
@@ -57,9 +57,9 @@ def log_hits?
end
true
end
-
+
public
-
+
def self.track(json, secret)
raw_stats = JSON.parse(json)
if secret == PIXEL_SECRET
@@ -74,6 +74,7 @@ def self.track(json, secret)
end
stat = Stat.new(:title => title, :url => url, :hits => hits)
stat.save
+ stat.delay.title if title.nil?
end
return true
end
View
1  config/environment.rb
@@ -8,6 +8,7 @@
config.gem "curb", :lib => "curl"
config.gem "jammit"
config.gem "nokogiri"
+ config.gem "delayed_job", :version => '~>2.0.5'
config.time_zone = 'Eastern Time (US & Canada)'
end
View
22 db/migrate/20110104232433_create_delayed_jobs.rb
@@ -0,0 +1,22 @@
+class CreateDelayedJobs < ActiveRecord::Migration
+ def self.up
+ create_table :delayed_jobs, :force => true do |table|
+ table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
+ table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
+ table.text :handler # YAML-encoded string of the object that will do work
+ table.text :last_error # reason for last failure (See Note below)
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
+ table.datetime :locked_at # Set when a client is working on this object
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
+ table.string :locked_by # Who is working on this object (if locked)
+ table.timestamps
+ end
+
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
+ add_index :delayed_jobs, :locked_by, :name => 'delayed_jobs_locked_by'
+ end
+
+ def self.down
+ drop_table :delayed_jobs
+ end
+end
View
5 script/delayed_job
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
+require 'delayed/command'
+Delayed::Command.new(ARGV).daemonize

0 comments on commit d95cfb5

Please sign in to comment.
Something went wrong with that request. Please try again.