def main(options):
    es = ES([options.es_server])
    try:
        es.create_index_if_missing('bzcache')
    except ElasticSearchException:
        # create_index_if_missing is supposed not to raise if the index
        # already existing, but with the ancient pyes / ES server versions
        # we're using it still does.
        pass

    # re-cache all intermittent-failure bugs
    bzcache = BugzillaCache(es_server=options.es_server)
    bzcache.index_bugs_by_keyword('intermittent-failure')
 def __init__(self, es_server, logger):
   self.keys = ['bug.changed.status',
                'bug.changed.summary',
                'bug.added.whiteboard',
                'bug.changed.whiteboard',
                'bug.new']
   self.logger = logger
   self.bzcache = BugzillaCache(es_server=es_server, logger=self.logger)
class MessageHandler(object):

  def __init__(self, es_server, logger):
    self.keys = ['bug.changed.status',
                 'bug.changed.summary',
                 'bug.added.whiteboard',
                 'bug.changed.whiteboard',
                 'bug.new']
    self.logger = logger
    self.bzcache = BugzillaCache(es_server=es_server, logger=self.logger)

  def log(self, msg):
    if self.logger:
      self.logger.info(msg)
    else:
      print msg

  def got_message(self, data, message):
    message.ack()

    key = data['_meta']['routing_key']
    #print key, data['payload']['bug']['id']

    if key in self.keys:
      try:
        bugid = data['payload']['bug']['id']
        status = data['payload']['bug']['status']
        if 'changed.status' in key:
          status = data['payload']['after']
        summary = data['payload']['bug']['summary']
        whiteboard = data['payload']['bug']['whiteboard']
        if 'changed.whiteboard' in key:
          whiteboard = data['payload']['after']
        elif 'added.whiteboard' in key:
          whiteboard = data['payload']['value']
        self.bzcache.add_or_update_bug(bugid, status, summary, whiteboard)
      except KeyError, inst:
        self.log('exception handling message %s' % key)
        self.log(inst)