Changeset 1549

Show
Ignore:
Timestamp:
04/22/08 10:55:58 (3 weeks ago)
Author:
dbryson
Message:

updates

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hive/trunk/worker_bee/lib/worker_bee.rb

    r1483 r1549  
    11module Hive 
    22  class WorkerBee 
    3     attr_reader :hive, :config 
     3    attr_reader :interval, :config 
    44     
    55    def initialize(config) 
    6       begin 
    7         @config = config 
    8       rescue SystemCallError 
    9         raise "Couldn't find or read the conf/worker.yml file. Tried: #{f}" 
    10       end 
    11        
     6      @config = config 
     7             
    128      if @config["access_key"] == "nil" 
    139        raise "Access Key is not set."         
    1410      end 
    1511       
     12      @config["poll_interval"] ||= 5 
     13      @interval = @config["poll_interval"] * 60 
     14 
     15      STDOUT.print "Poll interval set to: #{@interval} minutes" 
     16 
    1617      hive = config["hive_url"] 
    1718      hive << "/" unless hive[/\/$/] 
     
    4243        return true 
    4344      rescue Exception => e 
    44         STDERR.print "Can't reach Global hive @ #{@config["ping_url"]} exception: #{e}\n" 
     45        STDERR.print "Can't reach Community Hive @ #{@config["ping_url"]} exception: #{e}\n" 
    4546        return false 
    4647      end 
    4748    end 
    4849     
    49     # Need to test this 
     50    # Send the data to the community hive (JSON encoded) 
    5051    def ship_data 
    5152      # Grab data from the local DB, serialize 
     
    5455       
    5556      if job 
    56         #STDOUT.print "Job to process!" 
    57          
    5857        encoded_data = ActiveSupport::JSON.encode(job.job_data) 
    5958        begin 
     
    6463          STDOUT.print "Error sending data: #{e.message}" 
    6564        end 
    66          
    6765      end 
    6866    end 
    6967     
    70     # Main loop      
     68    # Main loop  
     69    # Do a ping to update the heartbeat table for Community Hive and 
     70    # Ship data if any available 
    7171    def run 
    7272      loop do 
    7373        if ping? 
    7474          ship_data 
    75         end 
    76          
    77         sleep(5*60) # 5 minutes 
     75        end   
     76        sleep(@interval) 
    7877      end 
    7978    end 
     
    9594    def process_response(res) 
    9695      if %w(200 201).include? res.code 
    97                res.body 
    98            elsif res.code == "403" 
    99                raise "Unauthorized" 
    100            else 
    101                raise "HTTP Error. Status: #{res.code}  #{res.body}" 
    102            end 
     96        res.body 
     97      elsif res.code == "403" 
     98        raise "Unauthorized" 
     99      else 
     100        raise "HTTP Error. Status: #{res.code}  #{res.body}" 
     101      end 
    103102    end 
    104103     
    105104    def parse_url(url) 
    106            url = "http://#{url}" unless url.match(/^http/) 
    107            URI.parse(url) 
    108        end 
     105      url = "http://#{url}" unless url.match(/^http/) 
     106      URI.parse(url) 
     107    end 
    109108         
    110109    def request_method(verb)