Skip to content

Commit

Permalink
Better handling of gist timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
rwdaigle committed Nov 24, 2012
1 parent dc92b94 commit 6f53dd5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 36 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/site.sass
Expand Up @@ -3,6 +3,11 @@
width: 95% width: 95%
text-align: right text-align: right


.last-edited
margin-right: 15px
color: #ccc
font-size: 12px

#footer #footer
position: absolute position: absolute
bottom: 0px bottom: 0px
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/gists_controller.rb
Expand Up @@ -9,7 +9,7 @@ class GistsController < ApplicationController
# end # end


def search def search
if(!cache? || stale?(etag: search_etag, last_modified: current_user.last_gh_fetch)) if(!cache? || stale?(etag: search_etag, last_modified: current_user.updated_at))
@results = Gist.search(current_user, normalized_query) @results = Gist.search(current_user, normalized_query)
if(feeling_lucky_directive? && lucky_result = @results.first) if(feeling_lucky_directive? && lucky_result = @results.first)
redirect_to lucky_result.url redirect_to lucky_result.url
Expand Down
6 changes: 4 additions & 2 deletions app/models/gist.rb
Expand Up @@ -5,10 +5,12 @@ class Gist < ActiveRecord::Base
attr_accessible :gh_id, :user_id, :owner_gh_id, :owner_gh_username, :owner_gh_avatar_url, :description, :url, :git_pull_url, :git_push_url, :public, attr_accessible :gh_id, :user_id, :owner_gh_id, :owner_gh_username, :owner_gh_avatar_url, :description, :url, :git_pull_url, :git_push_url, :public,
:comment_count, :gh_created_at, :gh_updated_at, :starred, :owned :comment_count, :gh_created_at, :gh_updated_at, :starred, :owned


belongs_to :user belongs_to :user, :touch => true
has_many :files, :class_name => 'GistFile', :dependent => :delete_all has_many :files, :class_name => 'GistFile', :dependent => :delete_all


scope :with_ids, lambda { |ids| where(ids.any? ? ["id in (?)", ids] : "1 = 0") } scope :with_ids, lambda { |ids| where(ids.any? ? ["id in (?)", ids] : "1 = 0") }
scope :starred, where(starred: true)
scope :not_starred, where(["starred = ? OR starred IS NULL", false])


index_name ELASTICSEARCH_INDEX_NAME index_name ELASTICSEARCH_INDEX_NAME


Expand Down Expand Up @@ -94,7 +96,7 @@ def reindex(gists = scoped)
end end


def search_cache_key(user, q) def search_cache_key(user, q)
"#{CACHE_ACTIVE ? CACHE_VERSION : "no-cache"}-user_id:#{user.id}-updated_at:#{user.last_gh_fetch ? user.last_gh_fetch.to_i : "never"}-#{q}" "#{CACHE_ACTIVE ? CACHE_VERSION : "no-cache"}-user_id:#{user.id}-updated_at:#{user.updated_at}-#{q}"
end end


def with_cache(key) def with_cache(key)
Expand Down
26 changes: 10 additions & 16 deletions app/models/gist_fetcher.rb
Expand Up @@ -3,10 +3,8 @@ class GistFetcher
class << self class << self


def fetch def fetch
period = ENV['FETCH_INTERVAL_MINS'] ? ENV['FETCH_INTERVAL_MINS'].to_i : 1440 log({ns: self, fn: __method__}) do
since = period.minutes.ago User.active_auth.pluck(:id).each do |user_id|
log({ns: self, fn: __method__}, since: since) do
User.last_fetched_before(since).active_auth.pluck(:id).each do |user_id|
QC.enqueue("GistFetcher.fetch_gists", user_id) QC.enqueue("GistFetcher.fetch_gists", user_id)
QC.enqueue("GistFetcher.fetch_starred_gists", user_id) QC.enqueue("GistFetcher.fetch_starred_gists", user_id)
end end
Expand All @@ -17,35 +15,31 @@ def fetch
def fetch_gists(user_id) def fetch_gists(user_id)


user = User.find(user_id) user = User.find(user_id)
since = user.last_gist_updated_at(user.gists.not_starred)


gh_client(user) do |gh| gh_client(user) do |gh|
log({ns: self, fn: __method__, measure: true}, user) do log({ns: self, fn: __method__, measure: true, since: since}, user) do
gh.gists(nil, since: (user.last_gh_fetch ? user.last_gh_fetch.iso8601.to_s : nil)).each do |gh_gist| gh.gists(nil, since: since).each do |gh_gist|
Gist.import(user_id, gh_gist) Gist.import(user_id, gh_gist)
QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist.id) GistFetcher.fetch_gist_files(user_id, gh_gist.id)
end end
end end
QC.enqueue("User.refresh_index", user_id)
end end

user.fetched! # If gist imports fail, this could cause gaps in updated gists...
end end


def fetch_starred_gists(user_id) def fetch_starred_gists(user_id)


user = User.find(user_id) user = User.find(user_id)
since = user.last_gist_updated_at(user.gists.starred)


gh_client(user) do |gh| gh_client(user) do |gh|
log({ns: self, fn: __method__, measure: true}, user) do log({ns: self, fn: __method__, measure: true, since: since}, user) do
gh.starred_gists(since: (user.last_gh_fetch ? user.last_gh_fetch.iso8601.to_s : nil)).each do |gh_gist| gh.starred_gists(since: since).each do |gh_gist|
Gist.import(user_id, gh_gist, starred: true) Gist.import(user_id, gh_gist, starred: true)
QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist.id) GistFetcher.fetch_gist_files(user_id, gh_gist.id)
end end
end end
QC.enqueue("User.refresh_index", user_id)
end end

user.fetched! # If gist imports fail, this could cause gaps in updated gists...
end end


def fetch_gist_files(user_id, gh_gist_id) def fetch_gist_files(user_id, gh_gist_id)
Expand Down
23 changes: 10 additions & 13 deletions app/models/user.rb
Expand Up @@ -5,7 +5,6 @@ class User < ActiveRecord::Base
has_many :gists, :dependent => :destroy has_many :gists, :dependent => :destroy
has_many :files, :through => :gists has_many :files, :through => :gists


scope :last_fetched_before, lambda { |since| where(["last_gh_fetch < ? OR last_gh_fetch IS NULL", since])}
scope :active_auth, where(gh_auth_active: true) scope :active_auth, where(gh_auth_active: true)


class << self class << self
Expand All @@ -28,27 +27,29 @@ def authenticate(auth)
end end
end end


def refresh_indexes
User.active_auth.pluck(:id).each do |user_id|
refresh_index(user_id)
end
end

def refresh_index(user_id) def refresh_index(user_id)
user = User.find(user_id) user = User.find(user_id)
log({ns: self, fn: __method__}, user) do log({ns: self, fn: __method__}, user) do
Gist.reindex(user.gists) Gist.reindex(user.gists)
end end
end end
end


def fetched!(user_id) def last_gist_updated_at(gist_scope = gists)
user = User.find(user_id) gist = gist_scope.order("gh_updated_at DESC").first
user.fetched! gist ? gist.gh_updated_at : nil
end
end end


def invalidate_auth! def invalidate_auth!
update_attribute(:gh_auth_active, false) update_attribute(:gh_auth_active, false)
end end


def fetched!
update_attribute(:last_gh_fetch, Time.now)
end

def gists_count def gists_count
@gists_count ||= gists.count @gists_count ||= gists.count
end end
Expand All @@ -57,10 +58,6 @@ def files_count
@files_count ||= files.count @files_count ||= files.count
end end


def fetched?
!last_gh_fetch.nil?
end

def to_log def to_log
{ user: gh_username, user_id: id, user_email: gh_email } { user: gh_username, user_id: id, user_email: gh_email }
end end
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -25,7 +25,9 @@
%body.launch %body.launch
#profile #profile
= link_to("logout", logout_path) if user_logged_in? - if user_logged_in?
%span.last-edited= "Most recent gist edited on #{current_user.last_gist_updated_at.to_formatted_s(:short)}"
%span.logout= link_to("logout", logout_path)
#header #header
.container .container
= yield = yield
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20121124154836_remove_last_gh_fetch.rb
@@ -0,0 +1,12 @@
class RemoveLastGhFetch < ActiveRecord::Migration

def up
remove_column :users, :last_gh_fetch
end

def down
change_table :users do |t|
t.timestamp :last_gh_fetch
end
end
end
3 changes: 2 additions & 1 deletion lib/tasks/fetch.rake
@@ -1,7 +1,8 @@
namespace :fetch do namespace :fetch do


task :periodic => :environment do task :periodic => :environment do
QC.enqueue("GistFetcher.fetch") GistFetcher.fetch
QC.enqueue("User.refresh_indexes")
end end


end end
9 changes: 9 additions & 0 deletions lib/tasks/gisted.rake
Expand Up @@ -3,4 +3,13 @@ namespace :gisted do
task :work => :environment do task :work => :environment do
GistedWorker.new.start GistedWorker.new.start
end end

desc "Delete all gist data, and do a fresh fetch and reindex of all user data"
task :rebuild => :environment do
Gist.destroy_all
Gist.tire.index.delete
Gist.tire.index.create
GistFetcher.fetch
QC.enqueue("User.refresh_indexes")
end
end end
4 changes: 2 additions & 2 deletions lib/tasks/search.rake
Expand Up @@ -2,14 +2,14 @@ namespace :search do


# Reindex all gists # Reindex all gists
task :reindex => :environment do task :reindex => :environment do
Gist.reindex User.refresh_indexes
end end


# Delete existing gist index, create new one and reindex all gists # Delete existing gist index, create new one and reindex all gists
task :rebuild => :environment do task :rebuild => :environment do
Gist.tire.index.delete Gist.tire.index.delete
Gist.tire.index.create Gist.tire.index.create
Gist.reindex User.refresh_indexes
end end


end end

0 comments on commit 6f53dd5

Please sign in to comment.