Permalink
Browse files

use delayed job to queue feed refresh requests

  • Loading branch information...
1 parent d0b3a37 commit 6af130079a853a87324dd147860ad244a7b8c9d6 @cocoa committed Nov 4, 2012
Showing with 56 additions and 4 deletions.
  1. +1 −1 Gemfile
  2. +2 −0 Gemfile.lock
  3. +11 −0 app/controllers/feeds_controller.rb
  4. +39 −0 app/models/feed.rb
  5. +3 −3 app/views/feeds/index.html.erb
View
@@ -9,7 +9,7 @@ gem 'sqlite3'
gem 'feedzirra'
gem 'feedbag'
gem 'delayed_job_active_record'
-
+gem 'daemons'
# Gems used only for assets and not required
# in production environments by default.
View
@@ -38,6 +38,7 @@ GEM
execjs
coffee-script-source (1.4.0)
curb (0.8.1)
+ daemons (1.0.10)
delayed_job (3.0.2)
activesupport (~> 3.0)
delayed_job_active_record (0.3.2)
@@ -123,6 +124,7 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.2.1)
+ daemons
delayed_job_active_record
feedbag
feedzirra
@@ -16,6 +16,7 @@ def destroy
# - - - - - - - - - - - - -
+=begin
def refresh
if params[:id]
Feed.find(params[:id]).fetch
@@ -24,5 +25,15 @@ def refresh
end
redirect_to :action => :index
end
+=end
+
+ def refresh
+ if params[:id]
+ Feed.fetch(params[:id])
+ else
+ Feed.fetch_all
+ end
+ redirect_to :action => :index
+ end
end
View
@@ -7,8 +7,46 @@ class Feed < ActiveRecord::Base
after_create :fetch
+ def fetch
+ feed_url = Feedbag.find self.url
+ feed = Feedzirra::Feed.fetch_and_parse feed_url.first
+
+ transaction do
+ self.title = feed.title
+ self.touch
+ save!
+
+ feed.entries.each do |entry|
+ unless posts.find_by_url entry.url
+ posts.create! :title => entry.title, :url => entry.url
+ end
+ end
+ end
+ end
+ handle_asynchronously :fetch
+
+ def self.fetch(id)
+ find(id).fetch
+ end
+
+
+ def self.fetch_all
+ all.each do |feed|
+ feed.fetch
+ # Feed.delay.fetch(feed.id) # convierte (1 tarea de tiempo N) en (N tareas de tiempo 1)
+ end
+ end
+
+
+
+=begin
def fetch
+ Delayed::Job.enqueue self
+ end
+
+
+ def perform
feed_url = Feedbag.find self.url
feed = Feedzirra::Feed.fetch_and_parse feed_url.first
@@ -32,6 +70,7 @@ def self.fetch_all
feed.fetch
end
end
+=end
end
@@ -1,4 +1,4 @@
-<h1>Feed Reader - no queueing</h1>
+<h1>Queueing Feed Reader</h1>
<%= form_tag feeds_path, :method => :post do %>
<%= label_tag 'Add a feed' %>
@@ -23,7 +23,7 @@
<%= link_to 'destroy', feed_path(feed.id), :method => :delete %>
</h3>
- <% feed.posts.each do |post| %>
- <%= link_to post.title, post.url %> *
+ <% feed.posts.order('created_at DESC').first(6).each do |post| %>
+ <%= link_to post.title, post.url %>, created <%= time_ago_in_words post.created_at %> ago *
<%end%>
<%end%>

0 comments on commit 6af1300

Please sign in to comment.