Skip to content

Instantly share code, notes, and snippets.

@mwawrusch
Created June 30, 2011 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwawrusch/1055966 to your computer and use it in GitHub Desktop.
Save mwawrusch/1055966 to your computer and use it in GitHub Desktop.
Worker
require 'simple_worker'
require 'hoptoad_notifier'
require 'devise'
require 'twitter'
require 'json'
require 'mongoid'
require 'devise/orm/mongoid'
require 'cancan'
require 'omniauth'
require 'carrierwave'
require 'mini_fb'
#require 'app_image_asset'
#require 'image_asset'
#require 'freshfugu_worker'
class UpdateAuthWorker < SimpleWorker::Base # < FreshfuguWorker
merge_gem "mongoid", "2.0.2"
merge_gem 'carrierwave'
merge_gem 'omniauth'
merge_gem 'voteable_mongoid'
merge_gem 'kaminari'
require 'kaminari'
merge "./freshfugu_worker.rb"
# puts "TEST START"
# puts "WORKING #{Dir::getwd}"
# Dir["../../lib/model_ext/*.rb"].each {|x| puts x}
# puts "TEST END"
merge_folder "lib/model_ext/"
merge_folder "app/uploaders/"
merge_folder "app/mailers/"
merge_folder "app/models/**/"
# merge_folder File.join(File.dirname(__FILE__), "../../lib/model_ext/")
# merge_folder File.join(File.dirname(__FILE__), "../uploaders/")
# merge_folder File.join(File.dirname(__FILE__), "../models/**/")
attr_accessor :user_name,:pre_heat,:provider
attr_accessor :devise_sender,:devise_pepper,:mongo_host,:mongo_port,:mongo_db,:mongo_user,:mongo_password
attr_accessor :hoptoad_key,:twitter_key,:twitter_secret,:facebook_key,:facebook_secret
attr_accessor :apigee_twitter_api_endpoint,:apigee_twitter_search_api_endpoint,:apigee_facebook_api_endpoint,:apigee_facebook_graph_api_endpoint
# Update the friends list. If provider is specified, then only that
# provider, if it exists, is processed. Otherwise all available authentications
# will be processed.
def self.enqueue (user_name,provider = nil,force_production = false)
worker = UpdateAuthWorker.new
raise ArgumentMissing, "A user_name is required and it must not be nil." if user_name == nil || user_name.empty?
worker.provider = provider
worker.user_name = user_name
worker.run_local if Rails.env == 'development' && !force_production
worker.queue(:priority => 0) if Rails.env == 'production' || force_production
rescue Exception => not_good
Rails.logger.debug not_good
end
# Invoke once to pre heat the worker.
def self.pre_heat
worker = UpdateAuthWorker.new
worker.pre_heat = true
worker.queue(:priority => 0) if Rails.env == 'production'
end
def run
return if self.pre_heat
log "Initializing new user #{self.user_name} in #{Rails.env}"
HoptoadNotifier.configure do |config|
config.api_key = self.hoptoad_key
end
begin
init
user = User.find_by_slug! self.user_name
authentication = user.authentications.where(:provider => 'twitter').first
if authentication && (!provider || provider == "twitter")
Twitter.configure do |config|
# config.gateway = "http://#{self.apigee_twitter_api_endpoint}"
config.consumer_key = self.twitter_key
config.consumer_secret= self.twitter_secret
config.oauth_token = authentication.token
config.oauth_token_secret = authentication.secret
end
user.set_is_working! true, 1,"Retrieving followers and friends!",false
#log "Key: #{self.twitter_key} against http://#{self.apigee_twitter_api_endpoint}"
log "Retrieving friends for user #{self.user_name}"
client = Twitter::Client.new
max_tries = 50 # e.g. limit = 5000
cursor = -1
total_user_count= 0
begin
max_tries = max_tries - 1
# client
hashie = client.friends(:cursor => cursor)
cursor = hashie.next_cursor
log "Request results in #{hashie.users.count} users"
hashie.users.each do |x|
begin
p = SocialPerson.create_or_update_social_person!('twitter',
x['screen_name'],
x['name'],
x['screen_name'],
x['description'],
x['profile_image_url'],
x['location'])
c = SocialConnection.get_or_create!(user,p)
rescue Exception => e
log "Oops #{e.message}"
# TODO: LOG THIS AS AN IMPORT ERROR FOR GECKOBOARD
end
# create relationship here
end
total_user_count =total_user_count +hashie.users.count
user.set_is_working! true, 50,"Fetching social_persons. #{total_user_count} social_persons so far.",false
log "Fetched #{hashie.users.count} social_persons."
end until max_tries < 0 || cursor == nil || cursor == 0
#user.user_status.is_new_user = true
user.set_is_working false, 100,"Setup completed successfully!",false
user.user_status.is_setup_complete = true
user.save!
end
# FACEBOOK NOW
authentication = user.authentications.where(:provider => 'facebook').first
if authentication && (!provider || provider == "facebook")
friends = MiniFB.get(authentication.token,"me", :type => "friends" ,:fields => %w[id,name,username])
friends.data.each do |x|
begin
p = SocialPerson.create_or_update_social_person!('facebook',
x['username'] || x['id'],
x['name'],
x['username'] || x['id'],
'',
"https://graph.facebook.com/#{x['id']}/picture",
'')
c = SocialConnection.get_or_create!(user,p)
rescue Exception => e
log "Oops #{e.message}"
# TODO: LOG THIS AS AN IMPORT ERROR FOR GECKOBOARD
end
# create relationship here
end
total_user_count = friends.data.count
user.set_is_working false, 100,"Setup completed successfully! Fetched #{total_user_count} social_persons",false
user.user_status.is_setup_complete = true
user.save!
end
=begin
<#Hashie::Rash
contributors_enabled=false
created_at="Thu Apr 16 04:48:38 +0000 2009"
default_profile=false
default_profile_image=false
description="Dad, entreprenuer, CEO (ccLoop.com), executive, guitar hero expert, triathete, ultrarunner, cyclocrosser, technologist, SF dweller, wine drinker, Quora addict."
favourites_count=0
follow_request_sent=false
followers_count=317
following=true
friends_count=148
geo_enabled=true
id=31648475
id_str="31648475"
is_translator=false
lang="en"
listed_count=10
location="San Francisco"
notifications=false
profile_background_color="C6E2EE"
profile_background_image_url="http://a1.twimg.com/a/1304019356/images/themes/theme2/bg.gif"
name="Michael Wolfe"
profile_background_tile=false
profile_image_url="http://a2.twimg.com/profile_images/623812992/firetrail50_normal.jpg"
screen_name="michaelrwolfe"
profile_link_color="1F98C7"
profile_sidebar_border_color="C6E2EE"
profile_sidebar_fill_color="DAECF4"
profile_text_color="663B12"
profile_use_background_image=true
protected=false
show_all_inline_media=true
status=<#Hashie::Rash
contributors=nil
coordinates=nil
created_at="Sat Apr 30 13:44:05 +0000 2011"
favorited=false
geo=nil
id=64324144820523008
id_str="64324144820523008"
in_reply_to_screen_name=nil
in_reply_to_status_id=nil
in_reply_to_status_id_str=nil
in_reply_to_user_id=nil
in_reply_to_user_id_str=nil
place=nil
retweet_count=0
retweeted=false
source="<a href=\"http://fast140.com\"
rel=\"nofollow\">Fast140</a>"
text="Playing the Twitter speed-typing game at http://fast140.com - How fast can you type?"
truncated=false>
statuses_count=59
time_zone="Pacific Time (US & Canada)"
url="http://michaelrwolfe.posterous.com"
utc_offset=-28800
verified=false>
=end
rescue => ex
log "Oops #{ex.message}"
HoptoadNotifier.notify(ex)
end
end
def init
is_production = Mongoid.master == nil rescue true
log "Configuring... Is Production: #{is_production}"
if is_production
Mongoid.configure do |config|
config.database = Mongo::Connection.new(self.mongo_host,self.mongo_port).db(self.mongo_db)
config.database.authenticate(self.mongo_user,self.mongo_password)
config.persist_in_safe_mode = true
config.skip_version_check = true
config.max_retries_on_connection_failure = 5
log "Configuring database complete - Running against #{Mongoid.master.name}"
end
end
Devise.setup do |config|
config.mailer_sender = self.devise_sender
config.mailer = "Devise::Mailer"
require 'devise/orm/mongoid'
config.authentication_keys = [ :login ]
config.http_authentication_realm = "freshfugu"
config.stretches = 10
config.pepper = self.devise_pepper
config.remember_for = 6.months
config.case_insensitive_keys = [:email]
end
# end
end
end
@rxbynerd
Copy link

Hi, I'm Luke, an engineer assigned to this ticket

Just glossing over, line #62 uses Rails.logger.debug, but we do not hook into your logger or hijack Rails.logger
Instead of Rails.logger, use log, which accepts a string.
This means the exception data will be present in the workers runtime log, which can be found in the web interface, as well as via the API

Thanks,
Luke

@mwawrusch
Copy link
Author

mwawrusch commented Jun 30, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment