-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started Guide
bnorton edited this page Mar 26, 2013
·
4 revisions
###To use all of the Default system components.
# Gemfile
gem 'micro_q'
# from the terminal
$ bundle###Then from within the application
# Asynchronously invoke the update method on the User instance (via the ActiveRecord API).
user.async.update
# Asynchronously send welcome email to a new user (via the ActionMailer API).
UserMailer.async.welcome(user.id)
# Asynchronously fetch new content (via the dedicated worker class API).
SocialDataUpdater.perform_async(user.id)
# ^ is the same (shorthand for):
SocialDataUpdater.new.async.perform(user.id)###What an expected implementation might be:
The ActiveRecord User update
class User < ActiveRecord::Base
has_many :facebook_posts
after_save -> { async.update }, :if => -> { updated_at + fb_ttl < Time.now }
# request the user from Facebook and update the `name`, `link` and `email`
def update
data = Facebook::API.new(token).get_object(graph_id)
update_attributes(data.slice(*%w(name link email)))
end
endThe ActionMailer email
class UserMailer < ActionMailer::Base
def welcome(user_id)
@user = User.find(user_id)
mail(:to => @user.name_and_email, :subject => 'Welcome to the App!')
end
endThe Worker class updater
# app/workers/social_data_updater.rb
class SocialDataUpdater
worker :timeout => 60 # the hook that includes the MicroQ Worker API
def perform(user_id)
User.find(user_id).tap do |user|
messages = Facebook::API.new(user.token).get_timeline(user.graph_id)
user.facebook_posts.create_many(messages)
end
end
end