Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from frommww/master
Added clarity to MongoDB and IndexTank examples
- Loading branch information
Showing
13 changed files
with
443 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sw_access_key: ABC | ||
sw_secret_key: XYZ | ||
|
||
klout_api_key: ABC | ||
twitter_usernames: ["simpleworker", "chadarimura", "klout", "techcrunch", "monkchips", "cote", "sgblank", "venturehacks", "nulltweet12345"] | ||
|
||
aws_access_key: ABC | ||
aws_secret_key: XYZ | ||
|
||
mongo_host: somehostname.mongohq.com | ||
mongo_port: somenumber | ||
mongo_username: ABC | ||
mongo_password: XYZ | ||
mongo_db_name: yourdbname | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#-- | ||
# SimpleWorker.com | ||
# Developer: Roman Kononov / Ken Fromm | ||
# | ||
# Klout_HelloWorker is a simple example of how to connect a worker to the Klout API. | ||
# | ||
# There is a klout.rb gem that has some nice methods and error checking but this | ||
# makes the API call directly so you can see the formats. | ||
# | ||
# A set of usernames can be obtained by changing the API param to :users=>twitter_usernames.join(",") | ||
# Of course, you'd want to take out or modify the twitter_usernames do loop. | ||
# | ||
#++ | ||
|
||
require 'simple_worker' | ||
require 'json' | ||
require 'open-uri' | ||
require 'rest-client' | ||
|
||
class KloutHelloWorker < SimpleWorker::Base | ||
|
||
attr_accessor :klout_api_key, :twitter_usernames | ||
|
||
def run | ||
log "Running Klout HelloWorker" | ||
|
||
twitter_usernames.each do |username| | ||
begin | ||
log "Processing username #{username}" | ||
|
||
# Call the Klout API | ||
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}} | ||
parsed = JSON.parse(response) | ||
|
||
daily_score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0] | ||
|
||
log "Processing: #{username} Score: #{daily_score}" | ||
|
||
rescue =>ex | ||
puts "EXCEPTION #{ex.inspect}" | ||
end | ||
end | ||
|
||
log "Done with Klout HelloWorker!" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#-- | ||
# Klout_Hello_Worker_Runner | ||
# Developer: Roman Kononov / Ken Fromm | ||
# | ||
# TO USE: | ||
# 1) Get accounts/credentials for Klout and SimpleWorker | ||
# | ||
# 2) Create a file in this directory called config.yml with the following parameters: | ||
# (Note: Your keys will be different than below. Get the right values from each service.) | ||
# | ||
# sw_access_key: ABC | ||
# sw_secret_key: XYZ | ||
# klout_api_key: ABC | ||
# twitter_usernames: ["simpleworker", "klout", "techcrunch"] | ||
# | ||
|
||
require 'simple_worker' | ||
require_relative 'klout_hello_worker' | ||
|
||
@config = YAML.load_file('config.yml') | ||
p @config | ||
|
||
SimpleWorker.configure do |config| | ||
config.access_key = @config["sw_access_key"] | ||
config.secret_key = @config["sw_secret_key"] | ||
end | ||
|
||
# Create the worker and set some attributes | ||
khw = KloutHelloWorker.new | ||
khw.klout_api_key = @config["klout_api_key"] | ||
khw.twitter_usernames = @config["twitter_usernames"] | ||
|
||
|
||
# Run the job (with several alternatives included) | ||
# | ||
#khw.run_local | ||
#khw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2) | ||
#khw.queue(:priority => 2) | ||
|
||
khw.queue | ||
|
||
|
||
# This gets stats on the job after completion. You can see the same in the | ||
# SimpleWorker dashboard. | ||
# | ||
# Note that wait_until_complete has issues with run_local and schedule. | ||
# Included for use with .queue so that we can display the log file. | ||
# | ||
status = khw.wait_until_complete | ||
p status | ||
puts khw.get_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#-- | ||
# KloutMongoWorker is a simple example of how the Klout API can be called from a Worker and the results | ||
# stored into a MongoHQ DB (using Mongoid). An example of this would be to go through the user base | ||
# on a nightly basis, get the Klout score, and store it back into your own database. | ||
# | ||
#++ | ||
|
||
require 'simple_worker' | ||
require 'json' | ||
require 'open-uri' | ||
require 'rest-client' | ||
|
||
class KloutMongoWorker < SimpleWorker::Base | ||
|
||
merge File.join(File.dirname(__FILE__), "user_klout_mongo_stat.rb") | ||
attr_accessor :klout_api_key, :twitter_usernames, | ||
:mongo_db_name, :mongo_host, :mongo_port, :mongo_username, :mongo_password | ||
|
||
def run | ||
log "Running Klout MongoWorker" | ||
|
||
init_mongohq | ||
|
||
# We only want to store the Klout for each user once per day - so this allows us to check | ||
today = Time.now.utc.at_beginning_of_day | ||
|
||
# Iterate through the usernames passed into the worker from the runner (or your app) | ||
# One optimization is to send the set of usernames (change param to :users=>twitter_usernames.join(",")) | ||
# and adjust the loop to work on the returned set. | ||
|
||
twitter_usernames.each do |username| | ||
begin | ||
# Check if there's a current score already set for today | ||
if daily_score = UserKloutMongoStat.first(conditions: {username: username, for_date: today}) | ||
log "Existing daily score of #{daily_score.username}: #{daily_score.score}" | ||
else | ||
# If a daily score record doesn't exist, call the Klout API and create one | ||
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}} | ||
parsed = JSON.parse(response) | ||
|
||
daily_score = UserKloutMongoStat.new(:username => username, :for_date => today, :score => 0) | ||
daily_score.score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0] | ||
daily_score.save | ||
|
||
log "New daily score of #{daily_score.username}: #{daily_score.score}" | ||
end | ||
|
||
rescue =>ex | ||
# If no username, .RestClient.get will return: 404 Resource Not Found. | ||
log "EXCEPTION #{ex.inspect}" | ||
end | ||
end | ||
|
||
log "Finishing Klout MongoWorker!" | ||
end | ||
|
||
# Configures settings for MongoDB. Values for mongo_host and mongo_port passed in to | ||
# make the example easy to understand. Could be placed directly inline to streamline. | ||
def init_mongohq | ||
Mongoid.configure do |config| | ||
config.database = Mongo::Connection.new(mongo_host, mongo_port).db(mongo_db_name) | ||
config.database.authenticate(mongo_username, mongo_password) | ||
# config.slaves = [ | ||
# Mongo::Connection.new(host, 27018, :slave_ok => true).db(name) | ||
# ] | ||
config.persist_in_safe_mode = false | ||
end | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# | ||
# This is a mashup of the Klout HelloWorker example and the MongoWorker example. | ||
# | ||
# TO USE: | ||
# 1) Get accounts/credentials for Klout and SimpleWorker | ||
# | ||
# 2) Create a file in this directory called config.yml with the following parameters: | ||
# (Note: Your keys and values will be different than below. Get the right values from each service.) | ||
# | ||
# sw_access_key: ABC | ||
# sw_secret_key: XYZ | ||
# klout_api_key: ABC | ||
# twitter_usernames: ["simpleworker", "klout", "techcrunch"] | ||
# mongo_host: somehostname.mongohq.com | ||
# mongo_port: somenumber | ||
# mongo_username: ABC | ||
# mongo_password: XYZ | ||
# mongo_db_name: yourdbname | ||
# | ||
|
||
|
||
require 'simple_worker' | ||
|
||
# Require_relative on the class name will also work | ||
load "klout_mongo_worker.rb" | ||
|
||
@config = YAML.load_file('config.yml') | ||
p @config | ||
|
||
SimpleWorker.configure do |config| | ||
config.access_key = @config["sw_access_key"] | ||
config.secret_key = @config["sw_secret_key"] | ||
end | ||
|
||
|
||
|
||
|
||
# Create the worker and set some attributes | ||
kmw = KloutMongoWorker.new | ||
kmw.klout_api_key = @config["klout_api_key"] | ||
kmw.twitter_usernames = @config["twitter_usernames"] | ||
|
||
kmw.mongo_db_name = @config["mongo_db_name"] | ||
kmw.mongo_host = @config["mongo_host"] | ||
kmw.mongo_port = @config["mongo_port"] | ||
kmw.mongo_username = @config['mongo_username'] | ||
kmw.mongo_password = @config['mongo_password'] | ||
|
||
|
||
# Run the job (with several alternatives included) | ||
# | ||
#kmw.run_local | ||
#kmw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2) | ||
#kmw.queue(:priority=>2) | ||
|
||
kmw.queue | ||
|
||
|
||
# This gets stats on the job after completion. You can see the same in the | ||
# SimpleWorker dashboard. | ||
# | ||
# Note that wait_until_complete has issues with run_local and schedule. | ||
# Included for use with .queue so that we can display the log file. | ||
# | ||
status = kmw.wait_until_complete | ||
p status | ||
puts kmw.get_log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#-- | ||
# Copyright (c) 2011 SimpleWorker.com | ||
# Developer: Roman Kononov | ||
# | ||
# KloutSimpleDBWorker is a simple example of how the Klout API can be called from a Worker and the results | ||
# stored into a SimpleDB domain (using SimpleRecord). An example of this would be to | ||
# go through your entire user base on a nightly basis, get the Klout score, and store it | ||
# back into your own database. | ||
# | ||
# This example can be modified and optimized as you see fit. | ||
# | ||
#++ | ||
|
||
require 'simple_worker' | ||
require 'simple_record' | ||
require 'json' | ||
require 'open-uri' | ||
require 'rest-client' | ||
|
||
class KloutSimpleDBWorker < SimpleWorker::Base | ||
|
||
merge File.join(File.dirname(__FILE__), "user_klout_stat.rb") | ||
attr_accessor :klout_api_key, :twitter_usernames, | ||
:aws_access_key, :aws_secret_key, :sdb_prefix | ||
|
||
def run | ||
log "Running Klout SimpleDBWorker" | ||
|
||
init_simpledb | ||
|
||
# We only want to store the Klout for each user once per day - so this allows us to check | ||
today = Time.now.utc.at_beginning_of_day | ||
|
||
# Iterate through the usernames passed into the worker from the runner (or your app) | ||
twitter_usernames.each do |username| | ||
begin | ||
# Call the Klout API | ||
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}} | ||
parsed = JSON.parse(response) | ||
|
||
# Check if there's a current score already set for today | ||
if daily_score = UserKloutStat.find(:first, :conditions=>["username=? and for_date=?", username, today]) | ||
log "Existing daily score of #{daily_score.username}: #{daily_score.score}" | ||
else | ||
# Create if no score today | ||
daily_score = UserKloutStat.new(:username => username, :for_date => today, :score => 0) unless daily_score | ||
daily_score.score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0] | ||
daily_score.save | ||
|
||
log "New daily score of #{daily_score.username}: #{daily_score.score}" | ||
end | ||
rescue =>ex | ||
log "EXCEPTION #{ex.inspect}" | ||
end | ||
end | ||
|
||
UserKloutStat.close_connection | ||
|
||
log "Finishing Klout SimpleDBWorker!" | ||
end | ||
|
||
# Establish connection to SimpleDB | ||
def init_simpledb | ||
SimpleRecord.establish_connection(aws_access_key, aws_secret_key, :connection_mode=>:per_thread) | ||
SimpleRecord::Base.set_domain_prefix("sample_worker_dev_") | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
# | ||
# TO USE: | ||
# 1) Get accounts/credentials for Klout, SimpleWorker, and AWS/SimpleDB | ||
# | ||
# 2) Create a file in this directory called config.yml with the following parameters: | ||
# (Note: Your keys will be different than below. Get the right values from each service.) | ||
# | ||
# sw_access_key: ABC | ||
# sw_secret_key: XYZ | ||
# klout_api_key: ABC | ||
# twitter_usernames: ["simpleworker", "klout", "techcrunch"] | ||
# aws_access_key: ABC | ||
# aws_secret_key: XYZ | ||
# | ||
|
||
require 'simple_worker' | ||
require 'json' | ||
require 'open-uri' | ||
require 'rest-client' | ||
|
||
# Require_relative on the class name will also work | ||
load "klout_simpledb_worker.rb" | ||
|
||
@config = YAML.load_file('config.yml') | ||
p @config | ||
|
||
SimpleWorker.configure do |config| | ||
config.access_key = @config["sw_access_key"] | ||
config.secret_key = @config["sw_secret_key"] | ||
end | ||
|
||
# Create the worker and set some attributes | ||
ksw = KloutSimpleDBWorker.new | ||
ksw.klout_api_key = @config["klout_api_key"] | ||
ksw.twitter_usernames = @config["twitter_usernames"] | ||
|
||
ksw.aws_access_key = @config["aws_access_key"] | ||
ksw.aws_secret_key = @config["aws_secret_key"] | ||
|
||
|
||
# Run the job (with several alternatives included) | ||
# | ||
#ksw.run_local | ||
#ksw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2) | ||
#ksw.queue(:priority=>2) | ||
|
||
ksw.queue | ||
|
||
|
||
|
||
# This gets stats on the job after completion. You can see the same in the | ||
# SimpleWorker dashboard. | ||
# | ||
# Note that wait_until_complete has issues with run_local and schedule. | ||
# Included for use with .queue so that we can display the log file. | ||
# | ||
status = ksw.wait_until_complete | ||
p status | ||
puts ksw.get_log |
Oops, something went wrong.