Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Backgrounding of an initial update task when a new feed is added.

  • Loading branch information...
commit 026cf2d33adec5b5d28fc8c3a18f9acd6f736414 1 parent c2133d3
@leenookx authored
View
13 NOTES
@@ -0,0 +1,13 @@
+Using BackgrounDRb is a pain since the documentation and examples are so poor. Here's some notes on how I'm using it.
+
+Injecting a worker
+
+ # script/console
+ # >> MiddleMan.new_worker(:worker => :feeds_worker, :worker_key => 2345, :do_work -> 1)
+
+Then:
+
+ # bots/background_monitor
+
+Should show you all of the workers currently running, including the one you just kicked off.
+
View
5 app/controllers/feeds_controller.rb
@@ -45,6 +45,11 @@ def create
respond_to do |format|
if @feed.save
flash[:notice] = 'Feed was successfully created.'
+
+ # Create a task for updating the feed.
+ key = MiddleMan.new_worker(:worker => :feeds_worker, :worker_key => @feed.id)
+ MiddleMan.worker(:feeds_worker, key).async_do_work(:feed_id => @feed.id)
+
format.html { redirect_to(@feed) }
format.xml { render :xml => @feed, :status => :created, :location => @feed }
else
View
61 bots/feed_grabber.rb
@@ -6,7 +6,7 @@
require 'open-uri'
require 'active_record'
-feed_id = 1
+feed_id = ARGV.pop
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = true
@@ -18,23 +18,48 @@
require 'app/models/feed.rb'
-record = Feed.find( feed_id )
-url = record.url
-
-content = ""
-open(url) do |s| content = s.read end
-cache_file = 'local_cache/'+`uuidgen`+'.feed'
-# Try and parse the feed, using validation
-begin
- rss = RSS::Parser.parse(content, true)
-rescue
- # Something went wrong with the validation, so let's go without it...
- rss = RSS::Parser.parse(content, false)
-end
+class FeedGrabber < Object
+ def initialize(feed_id)
+ @fid = feed_id
+ end
+
+ def go
+ find_record
+ grab_rss
+ update_feed_records
+ dump_stories
+ end
+
+ private
+ def find_record()
+ record = Feed.find( @fid )
+ @url = record.url
+ end
-Feed.update(feed_id, {:description => rss.channel.description, :cache_file => cache_file, :published => Time.now})
+ def grab_rss
+ @content = ""
+ open(@url) do |s| @content = s.read end
+ @cache_file = 'local_cache/'+`uuidgen`+'.feed'
+ # Try and parse the feed, using validation
+ begin
+ @rss = RSS::Parser.parse(@content, true)
+ rescue
+ # Something went wrong with the validation, so let's go without it...
+ @rss = RSS::Parser.parse(@content, false)
+ end
+ end
+
+ def update_feed_records
+ Feed.update(@fid, {:description => @rss.channel.description, :cache_file => @cache_file, :published => Time.now})
+ end
+
+ def dump_stories
+ # Write the feed out to disk
+ file = File.new(@cache_file, "w+")
+ file << @content
+ end
+end
-# Write the feed out to disk
-file = File.new(cache_file, "w+")
-file << content
+f = FeedGrabber.new( feed_id )
+f.go
View
81 bots/story_grabber.rb
@@ -1,4 +1,4 @@
-#!/usr/bin/ruby
+#!/usr/bin/env ruby
require 'rubygems'
require 'rss'
@@ -9,7 +9,7 @@
ENV["RAILS_ENV"]='development'
require RAILS_ROOT + '/config/environment'
-feed_id = 1
+feed_id = ARGV.pop
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = true
@@ -24,41 +24,60 @@
require 'app/models/feed.rb'
require 'app/models/story.rb'
-feed_record = Feed.find( feed_id )
-cache_file = feed_record.cache_file
-
-contents = String.new
+class StoryGrabber < Object
+ def initialize(feed_id)
+ @fid = feed_id
+ end
-begin
- File.open(cache_file) do |f|
- contents = f.read
+ def go
+ get_cache_file
+ grab_feed_contents
end
- # Try and parse the feed, using validation
- begin
- feed = RSS::Parser.parse(contents, true)
- rescue
- # Something went wrong with the validation, so let's go without it...
- feed = RSS::Parser.parse(contents, false)
+ private
+ def get_cache_file
+ @feed_record = Feed.find( @fid )
+ @cache_file = @feed_record.cache_file
end
- if feed.nil?
- puts "Sorry, that URL is not RSS 0.9x/1.0/2.0 or Atom 1.0 and can't be processed right now."
- else
- puts feed.channel.description
+ def grab_feed_contents
+ contents = String.new
- feed.items.each do |story|
- puts "#{story.title}"
- feed_record.stories.create(:feed_id => feed_id, :url => story.link, :details => story.description)
- end
+ begin
+ File.open(@cache_file) do |f|
+ contents = f.read
+ end
+
+ # Try and parse the feed, using validation
+ begin
+ feed = RSS::Parser.parse(contents, true)
+ rescue
+ # Something went wrong with the validation, so let's go without it...
+ feed = RSS::Parser.parse(contents, false)
+ end
+
+ if feed.nil?
+ puts "Sorry, that URL is not RSS 0.9x/1.0/2.0 or Atom 1.0 and can't be processed right now."
+ else
+ puts feed.channel.description
- # There seems to be a bug in ferret that means that this
- # is not done automatically...
- Story.rebuild_index
+ feed.items.each do |story|
+ puts "#{story.title}"
+ @feed_record.stories.create(:feed_id => @fid, :url => story.link, :details => story.description)
+ end
+
+ # There seems to be a bug in ferret that means that this
+ # is not done automatically...
+ Story.rebuild_index
+ end
+
+ rescue => err
+ puts "Exception: #{err}"
+ err
+ end
end
+end
+
+grabber = StoryGrabber.new( feed_id )
+grabber.go
-rescue => err
- puts "Exception: #{err}"
- err
-end
-
View
BIN  db/development.sqlite3
Binary file not shown
View
13 lib/workers/feeds_worker.rb
@@ -1,3 +1,5 @@
+require 'bots/feed_grabber'
+
class FeedsWorker < BackgrounDRb::MetaWorker
set_worker_name :feeds_worker
@@ -6,9 +8,16 @@ def create(args = nil)
# Gets called when the worker is created.
end
- def do_work(args)
+ def do_work(feed_id)
# This method is called in it's own new thread when you
# call new worker. args is set to :args
- end
+
+ helper = FeedGrabber.new( feed_id )
+ helper.go
+ story_helper = StoryGrabber.new( feed_id )
+ story_helper.go
+
+ persistent_job.finish
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.