Permalink
Browse files

Use Iron.io worker to build AMA cache

  • Loading branch information...
1 parent 45c5612 commit 1d37d7f9bf6b697a11e38b01e81dcb6fb367a52a @andey committed Oct 19, 2013
Showing with 25 additions and 56 deletions.
  1. +2 −1 Gemfile
  2. +8 −2 Gemfile.lock
  3. +8 −1 app/models/ama.rb
  4. +0 −18 lib/cache_builder.rb
  5. +0 −19 lib/tasks/cache.rake
  6. +0 −1 workers/.gitignore
  7. +7 −14 workers/build_cache.rb
View
@@ -5,7 +5,7 @@ ruby '2.0.0'
gem "rails", "~> 4.0.0"
gem 'pg'
gem 'bson', '~> 2.0.0.rc2'
-gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'
+gem 'mongoid', github: 'mongoid/mongoid'
gem 'unicorn'
gem 'jquery-rails'
gem 'sass-rails'
@@ -27,6 +27,7 @@ gem 'paper_trail', '>= 3.0.0.beta1'
gem 'airbrake'
gem 'httparty'
gem 'activeadmin', github: 'gregbell/active_admin'
+gem 'iron_worker_ng'
group :heroku do
gem 'rails_12factor'
View
@@ -17,7 +17,7 @@ GIT
sass-rails
GIT
- remote: https://github.com/mongoid/mongoid.git
+ remote: git://github.com/mongoid/mongoid.git
revision: a9b962f17de359d3173cf0ece813c9020803d3af
specs:
mongoid (4.0.0)
@@ -150,6 +150,10 @@ GEM
iron_cache (>= 1)
iron_core (1.0.1)
rest (>= 2.2.0)
+ iron_worker_ng (1.0.2)
+ bundler (>= 1.2.0)
+ iron_core (>= 1.0.0)
+ rubyzip (= 0.9.9)
jbuilder (1.5.2)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
@@ -177,7 +181,7 @@ GEM
net-scp (1.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.7.0)
- newrelic_rpm (3.6.8.164)
+ newrelic_rpm (3.6.8.168)
nokogiri (1.5.10)
optionable (0.1.1)
origin (1.1.0)
@@ -239,6 +243,7 @@ GEM
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
ruby-hmac (0.4.0)
+ rubyzip (0.9.9)
sass (3.2.12)
sass-rails (4.0.1)
railties (>= 4.0.0, < 5.0)
@@ -316,6 +321,7 @@ DEPENDENCIES
htmlentities
httparty
iron_cache_rails
+ iron_worker_ng
jbuilder
jquery-rails
mongoid!
View
@@ -17,6 +17,8 @@
#
require 'api/reddit.com'
+require 'iron_worker_ng'
+
class Ama < ActiveRecord::Base
# Basic validations
@@ -51,7 +53,7 @@ def users_attributes=(users)
# after_save :update_ops
# Update tagging karma score
- after_save :update_taggings
+ after_save :update_taggings, :build_cache
# Moderation Queue
scope :queue, -> { where 'amas.id NOT IN ( SELECT taggable_id from taggings ) AND amas.user_id NOT IN ( SELECT user_id from ops_users )' }
@@ -142,6 +144,11 @@ def update_taggings
end
end
+ def build_cache
+ client = IronWorkerNG::Client.new(token: ENV['IRON_CACHE_TOKEN'], project_id: ENV['IRON_CACHE_PROJECT_ID'])
+ client.tasks.create( 'build_cache', :ama => self.key )
+ end
+
# Updates an AMA from Reddit AMA JSON
def update_by_json(json)
View
@@ -1,18 +0,0 @@
-module CacheBuilder
-
- class << self
-
- require 'net/http'
- require 'open-uri'
- include AmasHelper
- include Rails.application.routes.url_helpers
-
- # Visit bestofama.com to build the cache
- def build_ama(a)
- http = Net::HTTP.new('bestofama.herokuapp.com')
- request = Net::HTTP::Get.new('http://bestofama.herokuapp.com' + ama_path(a))
- return http.request(request)
- end
-
- end
-end
View
@@ -1,19 +0,0 @@
-namespace :cache do
- # Build the cache of all AMAs
- task :build => :environment do
- require 'cache_builder'
- offset = ENV['offset'] ? ENV['offset'].to_i : 0
-
- Ama.order(:date).offset(offset).reverse_order.each do |a|
- puts '--------------'
- begin
- puts a.title
- response = CacheBuilder.build_ama(a)
- puts "Response: #{response.code}"
- puts "Count: #{offset}"
- ensure
- offset += 1
- end
- end
- end
-end
View
@@ -1,2 +1 @@
.iron.json
-.pg.yaml
View
@@ -2,17 +2,10 @@
require 'open-uri'
start = Time.now
-begin
- path = params[:path]
- puts "Rebuild Cache"
- puts "Path: #{path}"
-
- http = Net::HTTP.new("bestofama.herokuapp.com")
- request = Net::HTTP::Get.new("http://bestofama.herokuapp.com#{path}")
- response = http.request(request)
- puts "HTTP Code: #{response.code}"
-rescue
- puts "FAILED"
-ensure
- puts "Time Elapsed: #{Time.now - start}"
-end
+ama = params[:ama]
+puts "AMA: #{ama}"
+http = Net::HTTP.new("bestofama.herokuapp.com")
+request = Net::HTTP::Get.new("http://bestofama.herokuapp.com/amas/#{ama}")
+response = http.request(request)
+puts "HTTP Code: #{response.code}"
+puts "Time Elapsed: #{Time.now - start}"

0 comments on commit 1d37d7f

Please sign in to comment.