rebake based on version, not date

This commit is contained in:
Sam 2014-05-30 14:45:39 +10:00
parent 91b6459f2b
commit 95221ee9c1
3 changed files with 17 additions and 7 deletions

View File

@ -15,9 +15,8 @@ class Post < ActiveRecord::Base
include Trashable include Trashable
include HasCustomFields include HasCustomFields
# rebake all posts baked before this date # increase this number to force a system wide post rebake
# in our periodical job BAKED_VERSION = 1
REBAKE_BEFORE = Time.new(2014,5,27)
rate_limit rate_limit
rate_limit :limit_posts_per_day rate_limit :limit_posts_per_day
@ -315,7 +314,7 @@ class Post < ActiveRecord::Base
end end
def self.rebake_old(limit) def self.rebake_old(limit)
Post.where('baked_at IS NULL OR baked_at < ?', REBAKE_BEFORE) Post.where('baked_version IS NULL OR baked_version < ?', BAKED_VERSION)
.limit(limit).each do |p| .limit(limit).each do |p|
begin begin
p.rebake! p.rebake!
@ -333,7 +332,7 @@ class Post < ActiveRecord::Base
) )
old_cooked = cooked old_cooked = cooked
update_columns(cooked: new_cooked, baked_at: Time.new) update_columns(cooked: new_cooked, baked_at: Time.new, baked_version: BAKED_VERSION)
# Extracts urls from the body # Extracts urls from the body
TopicLink.extract_from self TopicLink.extract_from self
@ -391,6 +390,7 @@ class Post < ActiveRecord::Base
self.last_editor_id ||= user_id self.last_editor_id ||= user_id
self.cooked = cook(raw, topic_id: topic_id) unless new_record? self.cooked = cook(raw, topic_id: topic_id) unless new_record?
self.baked_at = Time.new self.baked_at = Time.new
self.baked_version = BAKED_VERSION
end end
after_save do after_save do

View File

@ -0,0 +1,5 @@
class AddBakedVersionToPost < ActiveRecord::Migration
def change
add_column :posts, :baked_version, :integer
end
end

View File

@ -834,11 +834,16 @@ describe Post do
describe ".rebake_old" do describe ".rebake_old" do
it "will catch posts it needs to rebake" do it "will catch posts it needs to rebake" do
post = create_post post = create_post
post.update_column(:baked_at, Time.new(2000,1,1)) post.update_columns(baked_at: Time.new(2000,1,1), baked_version: -1)
Post.rebake_old(100) Post.rebake_old(100)
post.reload post.reload
post.baked_at.should > 1.day.ago post.baked_at.should be > 1.day.ago
baked = post.baked_at
Post.rebake_old(100)
post.reload
post.baked_at.should == baked
end end
end end