Permalink
Browse files

Add periodic fetching of gists

  • Loading branch information...
1 parent b701bce commit c896e2a91881b8d9369f58fb663465202c3fa918 @rwdaigle committed Oct 5, 2012
@@ -13,7 +13,7 @@ def search
end
def refresh
- QC.enqueue("GistFetcher.fetch", current_user.id)
+ QC.enqueue("GistFetcher.fetch_user", current_user.id)
redirect_to search_gists_path
end
end
View
@@ -2,14 +2,25 @@ class GistFetcher
class << self
- def fetch(user_id)
+ def fetch
+ period = ENV['FETCH_INTERVAL_MINS'] ? ENV['FETCH_INTERVAL_MINS'].to_i : 1440
+ since = period.minutes.ago
+ log({ns: self, fn: __method__}, since: since) do
+ User.last_fetched_before(since).pluck(:id) do |user_id|
+ fetch_user(user_id)
+ end
+ end
+ end
+
+ def fetch_user(user_id)
user = User.find(user_id)
log({ns: self, fn: __method__}, user) do
gh = gh_client(user)
fetch_gists(gh, user)
fetch_files(gh, user)
update_search_indices(user)
end
+ user.update_attribute(:last_gh_fetch, Time.now)
end
protected
View
@@ -4,6 +4,8 @@ class User < ActiveRecord::Base
has_many :gists, :dependent => :destroy
+ scope :last_fetched_before, lambda { |since| where(["last_gh_fetch < ?", since])}
+
class << self
def authenticate(auth)
@@ -0,0 +1,9 @@
+class AddLastFetchTimestamp < ActiveRecord::Migration
+ def up
+ add_column :users, :last_gh_fetch, :timestamp
+ end
+
+ def down
+ remove_column :users, :last_gh_fetch
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121002204505) do
+ActiveRecord::Schema.define(:version => 20121005200216) do
create_table "gist_files", :force => true do |t|
t.integer "gist_id"
@@ -65,6 +65,7 @@
t.string "gh_url"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.datetime "last_gh_fetch"
end
add_index "users", ["gh_id"], :name => "index_users_on_gh_id"
View
@@ -0,0 +1,7 @@
+namespace :fetch do
+
+ task :periodic => :environment do
+ GistFetcher.fetch
+ end
+
+end

0 comments on commit c896e2a

Please sign in to comment.