Class: GHTLoad

Inherits:
GHTorrent::Command show all
Includes:
GHTorrent::Persister, GHTorrent::Settings
Defined in:
lib/ghtorrent/commands/ght_load.rb

Constant Summary

Constants included from GHTorrent::Persister

GHTorrent::Persister::ADAPTERS

Constants included from GHTorrent::Settings

GHTorrent::Settings::CONFIGKEYS, GHTorrent::Settings::DEFAULTS

Instance Method Summary collapse

Methods included from GHTorrent::Persister

#connect, #disconnect

Methods included from GHTorrent::Settings

#config, #merge, #merge_config_values, #override_config, #settings

Methods included from GHTorrent::Utils

included, #read_value, #user_type, #write_value

Methods inherited from GHTorrent::Command

#command_name, #override_config, #process_options, #queue_client, run, #version

Methods included from GHTorrent::Logging

#debug, #error, #info, #loggerr, #warn

Instance Method Details

#goObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ghtorrent/commands/ght_load.rb', line 57

def go
  # Num events read
  total_read = 0

  puts "Loading events after #{Time.at(options[:earliest])}" if options[:verbose]
  puts "Loading events before #{Time.at(options[:latest])}" if options[:verbose]
  puts "Loading #{options[:number]} items" if options[:verbose]

  what = case
           when options[:filter].is_a?(Array)
             options[:filter].reduce({}) { |acc,x|
               (k,r) = x.split(/=/)
               acc[k] = Regexp.new(r)
               acc
             }
           when filter == []
             {}
         end

  from = {'_id' => {
      '$gte' => BSON::ObjectId.from_time(Time.at(options[:earliest])),
      '$lte' => BSON::ObjectId.from_time(Time.at(options[:latest]))}
  }

  (puts 'Mongo filter:'; pp what.merge(from)) if options[:verbose]

  conn = Bunny.new(:host => config(:amqp_host),
                   :port => config(:amqp_port),
                   :username => config(:amqp_username),
                   :password => config(:amqp_password))
  conn.start

  channel = conn.create_channel
  puts "Connection to #{config(:amqp_host)} succeded"

  exchange = channel.topic(config(:amqp_exchange),
                           :durable => true, :auto_delete => false)

  stopped = false
  while not stopped
    begin
      persister.get_underlying_connection[:events].find(what.merge(from),
                                                        :snapshot => true).each do |e|
        unq = read_value(e, 'type')
        if unq.class != String or unq.nil? then
          raise Exception.new('Unique value can only be a String')
        end

        exchange.publish e['id'], :persistent => false,
                         :routing_key => "evt.#{e['type']}"

        total_read += 1
        puts "Publish id = #{e['id']} #{e['created_at']} (#{total_read} read)" if options.verbose

        if total_read >= options[:number]
          puts 'Finished reading, exiting'
          return 
        end
      end
      stopped = true
    rescue Interrupt
      puts 'Interrupted'
      stopped = true
    end
  end
end

#persisterObject



16
17
18
19
# File 'lib/ghtorrent/commands/ght_load.rb', line 16

def persister
  @persister ||= connect(:mongo, settings)
  @persister
end

#prepare_options(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ghtorrent/commands/ght_load.rb', line 21

def prepare_options(options)
  options.banner "Loads object ids from a collection to a queue for further processing.\n\n\#{command_name} [options] collection\n\n\#{command_name} options:\n  BANNER\n\n  options.opt :earliest, 'Seconds since epoch of earliest item to load',\n              :short => 'e', :default => 0, :type => :int\n  options.opt :latest, 'Seconds since epoch of latest item to load',\n              :short => 'x', :default => Time.now.to_i + (60 * 60 * 24 * 360 * 20),\n              :type => :int\n  options.opt :number, 'Total number of items to load',\n              :short => 'n', :type => :int, :default => 2**48\n  options.opt :filter,\n              'Filter items by regexp on item attributes: item.attr=regexp',\n              :short => 'f', :type => String, :multi => true\nend\n"

#validateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ghtorrent/commands/ght_load.rb', line 42

def validate
  super
  filter = options[:filter]
  case
    when filter.is_a?(Array)
      options[:filter].each { |x|
        Trollop::die "not a valid filter #{x}" unless is_filter_valid?(x)
      }
    when filter == []
      # Noop
    else
      Trollop::die 'A filter can only be a string'
  end
end