Created
February 24, 2011 11:33
-
-
Save IskanderHaziev/842073 to your computer and use it in GitHub Desktop.
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
def force_flush_cache | |
CacheTumblrPost.update_cached_posts(true) | |
end | |
def runner_check_cache(force = false) | |
cronlog_timestamp('start rebuild caches') | |
CacheTumblrPost.update_cached_posts(force) | |
cronlog_timestamp('finish rebuild caches') | |
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
namespace :remote_cache do | |
desc "Rebuild the tumblr cache according to caching rules" | |
task :check_tumblr => :environment do | |
ApplicationController.new.runner_check_cache(false) | |
end | |
desc "Force Rebuild the tumblr cache" | |
task :force_tumblr => :environment do | |
ApplicationController.new.runner_check_cache(true) | |
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
class CacheTumblrPost < ActiveRecord::Base | |
MAX_CACHE_LENGTH_IN_MINUTES = 300 | |
DEFAULT_AGE_IN_SECONDS = 10000000 | |
named_scope :completed, :conditions => 'incomplete != 1' | |
class << self | |
def login_and_get_tumblr_user(blog = nil) | |
@tumblr_user ||= Tumblr::User.new('micahalbert@empoweringlives.org', 'sudan79') | |
Tumblr.blog = blog || 'empoweringlives' | |
@tumblr_user | |
end | |
def update_cached_posts(force = false) | |
login_and_get_tumblr_user | |
cronlog("cache age: #{CacheTumblrPost.age_in_minutes.round} minutes old") | |
return false unless force || self.need_to_update? | |
cronlog("build tumblr cache #{' - force_flush' if force}") | |
posts = self.fetch_posts | |
found_posts = 0 | |
good_posts = 0 | |
posts.each do |post| | |
found_posts += 1 | |
next unless allow_to_add_post?(post) | |
create!( | |
:tumblr_post_id => post["id"], | |
:title => post["regular_title"], | |
:desc => post["regular_body"], | |
:post_date => post["date"], | |
:url => post["url_with_slug"], | |
:reblog_key => post["reblog_key"], | |
:post_type => post["type"], | |
:incomplete => 1 | |
) | |
good_posts += 1 | |
end | |
if found_posts == total_count | |
replace_by_new_cache! | |
cronlog("Total Tumblr posts: #{found_posts} | Usable posts: #{good_posts}") | |
elsif found_posts < total_count && found_posts > 1 | |
cronlog( | |
"Error: Tumblr has #{total_count} posts but only #{found_posts} were retrieved" | |
) | |
else | |
cronlog('Tumblr api/read Down --') | |
end | |
rescue | |
cronlog('Tumblr posts fetching failed') | |
end | |
def allow_to_add_post?(post) | |
post["type"] == "regular" && post['regular_title'] && | |
!self.exists?(:tumblr_post_id => post['id'], :incomplete => 1) | |
end | |
def total_count | |
@total_count ||= Tumblr::Request.read["tumblr"]["posts"]["total"].to_i | |
end | |
def fetch_posts | |
request_count = (total_count / 50) + 1 | |
posts = [] | |
request_count.times do |i| | |
posts += Tumblr::Post.all(:query => { :start => 50 * i, :num => 50 }).uniq | |
end | |
posts | |
rescue | |
{} | |
end | |
def replace_by_new_cache! | |
self.delete_all('incomplete != 1') | |
self.update_all(:incomplete => 0) | |
end | |
def sample | |
@sample ||= self.first(:conditions => "incomplete != 1") | |
end | |
def age_in_seconds | |
if sample.present? | |
Time.now - sample.created_at.in_time_zone("Pacific Time (US & Canada)") | |
else | |
DEFAULT_AGE_IN_SECONDS | |
end | |
end | |
def age_in_minutes | |
age_in_seconds / 60 | |
end | |
def need_to_update? | |
sample.blank? || (age_in_minutes > MAX_CACHE_LENGTH_IN_MINUTES) | |
end | |
def cronlog(desc) | |
puts DateTime.now.strftime("%y-%m-%d %H:%M:%S") + " - #{desc}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment