Changeset 1575
- Timestamp:
- 05/08/08 17:13:49 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
hive/trunk/data_webapp/app/controllers/hc_database_controller.rb
r1574 r1575 16 16 return 0 17 17 end 18 if obj.valid? 19 obj.save 20 return obj["id"] 18 if (obj.valid? && obj.save) 19 return obj.id 21 20 else 22 logger. info"Invalid object:\n" + obj.to_yaml23 021 logger.warn "Invalid object:\n" + obj.to_yaml 22 return 0 24 23 end 25 24 end … … 60 59 } 61 60 # Counts the number of urls successfully inserted OR modified 62 if insert(QueueUrl,url_obj) > 061 if insert(QueueUrl,url_obj) 63 62 count += 1 64 63 end … … 338 337 end 339 338 340 if urls.length > 0 341 # Lock URLs to prevent duplication of work, by setting host_id (!=0). 342 QueueUrl.update_all('host_id='+host.id.to_s,'id IN ('+urls.map(&:id).join(',')+')') 339 if (urls.length > 0) 340 # Sanity Check: host_id 341 # 342 # For each QueueUrl object, the host_id should be initially 0 upon creation 343 # and set to an integer upon first update. However, every subsequent update 344 # should *NEVER* change the host_id ever again. 345 # 346 # We make sure that the record in the database doesn't have a host_id already 347 # assigned to it. 348 QueueUrl.transaction do 349 # Lock URLs to prevent duplication of work, by setting host_id (!=0). 350 if (QueueUrl.update_all('host_id='+host.id.to_s,'id IN ('+urls.map(&:id).join(',')+') AND (host_id=0)') != urls.length) 351 urls = [] 352 logger.warn "get_new_queue_urls(): collision detected - rolling back" 353 raise ActiveRecord::Rollback 354 end 355 end 343 356 end 344 357 hive/trunk/data_webapp/app/models/queue_url.rb
r1563 r1575 1 1 require 'resolv' 2 2 class QueueUrl < ActiveRecord::Base 3 4 # These validations will only work on basic ActiveRecord::Base save and update 5 # operations. 3 6 validates_presence_of :url 4 7 validates_presence_of :priority 8 validates_presence_of :last_visited_at 9 validates_presence_of :created_at 10 validates_presence_of :count 11 validates_presence_of :host_id 12 validates_presence_of :source_type 13 validates_numericality_of :host_id, :only_integer => true, :greater_than_or_equal_to => 0 14 validates_numericality_of :count, :only_integer => true, :greater_than_or_equal_to => 0 15 validates_numericality_of :last_visited_at, :greater_than_or_equal_to => 0 16 validates_numericality_of :created_at, :greater_than_or_equal_to => 0 5 17 6 18 def source_type … … 19 31 #ip = Resolv.getaddress(uri.host) 20 32 rescue 33 # XXX: Is this still needed? 21 34 logger.info "Could not parse URL or resolve host" 22 35 return nil … … 49 62 end 50 63 end 64 65 # These validations will only work on basic ActiveRecord::Base save and update 66 # operations. 67 protected 68 def validate_on_update 69 # Pull the old version of the record from the database, to compare changes. 70 old_self = QueueUrl.find(:first, :conditions => {:id => id}) 71 72 # host_id: Sanity check. 73 # The host_id should be initially 0 upon creation and set to an integer upon 74 # first update; however, every subsequent update should *NEVER* change the 75 # host_id ever again. 76 # 77 # We make sure that the record in the database doesn't have a host_id already 78 # assigned to it. 79 if ((host_id != old_self.host_id) && (old_self.host_id != 0)) 80 errors.add("host_id", "has already been assigned to this QueueUrl record") 81 end 82 end 51 83 end
