lib/spreadsheet_agent/agent.rb
def run_entry
      entry = get_entry()
      output = '';
      @keys.keys.select { |k| @config['key_fields'][k] && @keys[k] }.each do |key|
        output += [ key, @keys[key] ].join(' ') + " "
      end

      unless entry
        $stderr.puts "#{ output } is not supported on #{ @page_name }" if @debug
        return
      end

      unless entry['ready'] == "1"
        $stderr.puts "#{ output } is not ready to run #{ @agent_name }" if @debug
        return false, entry
      end

      if entry['complete'] == "1"
        $stderr.puts "All goals are completed for #{ output }" if @debug
        return false, entry
      end

      if entry[@agent_name]
        (status, running_hostname) = entry[@agent_name].split(':')

        case status
        when 'r'
          $stderr.puts " #{ output } is already running #{ @agent_name } on #{ running_hostname }" if @debug
          return false, entry
        
        when "1"
          $stderr.puts " #{ output } has already run #{ @agent_name }" if @debug
          return false, entry

        when 'F'
          $stderr.puts " #{ output } has already Failed  #{ @agent_name }" if @debug
          return false, entry
        end
      end

      if @prerequisites
        @prerequisites.each do |prereq_field|
          unless entry[prereq_field] == "1"
            $stderr.puts " #{ output } has not finished #{ prereq_field }" if @debug
            return false, entry
          end
        end
      end

      # first attempt to set the hostname of the machine as the value of the agent
      hostname = Socket.gethostname;
      begin
        entry.update @agent_name => "r:#{ hostname }"
        @worksheet.save

      rescue GoogleDrive::Error
        # this is a collision, which is to be treated as if it is not runnable
        $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
        return false, entry
      end

      sleep 3
      begin
        @worksheet.reload
      rescue GoogleDrive::Error
        # this is a collision, which is to be treated as if it is not runnable
        $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
        return false, entry
      end

      check = entry[@agent_name]
      (status, running_hostname) = check.split(':')
      if hostname == running_hostname
        return true, entry
      end
      $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
      return false, entry
    end
def run_entry
      entry = get_entry()
      output = '';
      @keys.keys.select { |k| @config['key_fields'][k] && @keys[k] }.each do |key|
        output += [ key, @keys[key] ].join(' ') + " "
      end

      unless entry
        $stderr.puts "#{ output } is not supported on #{ @page_name }" if @debug
        return
      end

      unless entry['ready'] == "1"
        $stderr.puts "#{ output } is not ready to run #{ @agent_name }" if @debug
        return false, entry
      end

      if entry['complete'] == "1"
        $stderr.puts "All goals are completed for #{ output }" if @debug
        return false, entry
      end

      if entry[@agent_name]
        (status, running_hostname) = entry[@agent_name].split(':')

        case status
        when 'r'
          $stderr.puts " #{ output } is already running #{ @agent_name } on #{ running_hostname }" if @debug
          return false, entry
        
        when "1"
          $stderr.puts " #{ output } has already run #{ @agent_name }" if @debug
          return false, entry

        when 'F'
          $stderr.puts " #{ output } has already Failed  #{ @agent_name }" if @debug
          return false, entry
        end
      end

      if @prerequisites
        @prerequisites.each do |prereq_field|
          unless entry[prereq_field] == "1"
            $stderr.puts " #{ output } has not finished #{ prereq_field }" if @debug
            return false, entry
          end
        end
      end

      # first attempt to set the hostname of the machine as the value of the agent
      hostname = Socket.gethostname;
      begin
        entry.update @agent_name => "r:#{ hostname }"
        @worksheet.save

      rescue GoogleDrive::Error
        # this is a collision, which is to be treated as if it is not runnable
        $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
        return false, entry
      end

      sleep 3
      begin
        @worksheet.reload
      rescue GoogleDrive::Error
        # this is a collision, which is to be treated as if it is not runnable
        $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
        return false, entry
      end

      check = entry[@agent_name]
      (status, running_hostname) = check.split(':')
      if hostname == running_hostname
        return true, entry
      end
      $stderr.puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug
      return false, entry
    end
def run_entry entry = get_entry ( ) output = '' ; @keys . keys . select { | k | @config [ 'key_fields' ] [ k ] && @keys [ k ] } . each do | key | output += [ key , @keys [ key ] ] . join ( ' ' ) + " " end unless entry $stderr . puts "#{ output } is not supported on #{ @page_name }" if @debug return end unless entry [ 'ready' ] == "1" $stderr . puts "#{ output } is not ready to run #{ @agent_name }" if @debug return false , entry end if entry [ 'complete' ] == "1" $stderr . puts "All goals are completed for #{ output }" if @debug return false , entry end if entry [ @agent_name ] ( status , running_hostname ) = entry [ @agent_name ] . split ( ':' ) case status when 'r' $stderr . puts " #{ output } is already running #{ @agent_name } on #{ running_hostname }" if @debug return false , entry when "1" $stderr . puts " #{ output } has already run #{ @agent_name }" if @debug return false , entry when 'F' $stderr . puts " #{ output } has already Failed  #{ @agent_name }" if @debug return false , entry end end if @prerequisites @prerequisites . each do | prereq_field | unless entry [ prereq_field ] == "1" $stderr . puts " #{ output } has not finished #{ prereq_field }" if @debug return false , entry end end end # first attempt to set the hostname of the machine as the value of the agent hostname = Socket . gethostname ; begin entry . update @agent_name => "r:#{ hostname }" @worksheet . save rescue GoogleDrive :: Error # this is a collision, which is to be treated as if it is not runnable $stderr . puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug return false , entry end sleep 3 begin @worksheet . reload rescue GoogleDrive :: Error # this is a collision, which is to be treated as if it is not runnable $stderr . puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug return false , entry end check = entry [ @agent_name ] ( status , running_hostname ) = check . split ( ':' ) if hostname == running_hostname return true , entry end $stderr . puts " #{ output } lost #{ @agent_name } on #{hostname}" if @debug return false , entry end
this call initiates a race resistant attempt to make sure that there is only 1
 clear 'winner' among N potential agents attempting to run the same goal on the
 same spreadsheet agent's cell
this call initiates a race resistant attempt to make sure that there is only 1 clear winner among N potential agents attempting to run the same goal on the same spreadsheet agent s cell