Hey Mike and the rest of the Gang,
I just finished packaging up a plugin for backgrounding many of the
Facebook API calls so that they don''t take up the request cycle.
Hopefully you will find it useful, once I started to package it up I
realized that there was a much simpler approach so I don''t have this
in production yet but it will be soon. It turned out to be simple
enough that maybe we could actually put it , or something like it into
the Facebooker Codebase? If not I will setup a rubyforge project for
it to live , currently it lives at:
script/plugin install
http://daveonrails.svnrepository.com/svn/projects/facebooker_mq
Let me know if you can''t get it, the guys told me that the set
anonymous access.
Here is the README as it stands now,
Copyright (c) 2008 David Clements, released under the MIT license
Updated: 1/18/2008
Purpose:
The of FacebookerMQ is to provide an async method for performing time consuming
Facebook API requests. The Facebook platform is very sensitive to long
running requests,
and as such it becomes important to offload some of these requests to
a background process.
As the name suggests this plugin relies on the facebooker api ( gem
install facebooker ).
Overview:
FacebookerMQ intercepts Facebooker calls into session.publish(method,
params) and saves all the relevant data
to the database. This is inspired by the ar_mailer project which does
the same for ActionMailer. In order
to process the queue you can setup a cron job to run script/runner
FacebookerMqRunner.work().
What you need:
1) script/plugin install
http://daveonrails.svnrepository.com/svn/projects/facebooker_mq
2) You will need a migration to create the table that holds the
messages. You can think if this table as the queue
class CreateFacebookMessages < ActiveRecord::Migration
def self.up
create_table :facebook_messages do |t|
t.column(:session_key, :string, :null => false)
t.column(:expires, :string, :null => false, :default =>
"0")
t.column(:uid, :integer, :null => false)
t.column(:method, :string, :null => false)
t.column(:params, :text, :null => false ) # Always a hash.
t.column(:state, :string, :null => false, :default =>
''new'' )
t.column(:send_after, :datetime, :null => false, :default =>
Time.at(0)) # Optional date time to wait until you send
t.column( :last_send_attempt, :datetime )
t.column( :send_attempts, :integer, :default => 0 , :null =>
false )
t.timestamps
end
end
def self.down
drop_table :facebook_messages
end
end
3) Cronjob or some other way to fire off FacebookerMqRunner.work()
Misc:
Look in facebooker_session_override.rb and you will see all the api
methods that are currently offloaded to the queue.
This can be added to or removed from by calling
Facebooker::Session.async_method or Facebooker::Session.sync_method
I am kinda hoping to get this pushed into the Facebooker source so
that it can be a config option. If not I will setup
a rubyforge project and change the URL
This is a work in progress and any comments or code is totally
welcome. It has been developed from a couple of projects
that I am currently working on.
TODO:
1) I think some of the options such as the max number of failure,
which methods to intercept and table name should be configurable.
2) I think that it would be good to work out some admin view of the queue.
3) Make sure that it works well with multiple runner instances.
4) Remove the need to run this within the RAILS_ENV
5) Create a gem like ar_mailer has.