Changeset 1575

Show
Ignore:
Timestamp:
05/08/08 17:13:49 (3 days ago)
Author:
kindlund
Message:

Initial fix for ticket #164.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hive/trunk/data_webapp/app/controllers/hc_database_controller.rb

    r1574 r1575  
    1616      return 0  
    1717    end 
    18     if obj.valid? 
    19       obj.save 
    20       return obj["id"] 
     18    if (obj.valid? && obj.save) 
     19      return obj.id 
    2120    else 
    22       logger.info "Invalid object:\n" + obj.to_yaml 
    23      
     21      logger.warn "Invalid object:\n" + obj.to_yaml 
     22      return
    2423    end 
    2524  end 
     
    6059      } 
    6160      # Counts the number of urls successfully inserted OR modified 
    62       if insert(QueueUrl,url_obj) > 0 
     61      if insert(QueueUrl,url_obj) 
    6362        count += 1 
    6463      end 
     
    338337    end 
    339338 
    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 
    343356    end 
    344357 
  • hive/trunk/data_webapp/app/models/queue_url.rb

    r1563 r1575  
    11require 'resolv' 
    22class QueueUrl < ActiveRecord::Base 
     3 
     4  # These validations will only work on basic ActiveRecord::Base save and update 
     5  # operations. 
    36  validates_presence_of :url 
    47  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 
    517 
    618  def source_type 
     
    1931      #ip = Resolv.getaddress(uri.host) 
    2032    rescue 
     33      # XXX: Is this still needed? 
    2134      logger.info "Could not parse URL or resolve host" 
    2235      return nil 
     
    4962    end 
    5063  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 
    5183end